Skip to content

Commit

Permalink
feat(mfe): v2->v1 & make version optional (#9787)
Browse files Browse the repository at this point in the history
### Description

V1 config is removed, and V2 config is renamed to V1. Also the version
field is optional, defaulting to v1.
I left a lot of the plumbing in place for multiple versions because I'm
sure more will come as we continue to improve it.

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->
  • Loading branch information
tknickman authored Jan 24, 2025
1 parent b066773 commit ca2e681
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 394 deletions.
118 changes: 10 additions & 108 deletions crates/turborepo-lib/src/microfrontends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use itertools::Itertools;
use tracing::warn;
use turbopath::{AbsoluteSystemPath, RelativeUnixPath, RelativeUnixPathBuf};
use turborepo_microfrontends::{Config as MFEConfig, Error, MICROFRONTENDS_PACKAGES};
use turborepo_microfrontends::{Config as MFEConfig, Error, MICROFRONTENDS_PACKAGE};
use turborepo_repository::package_graph::{PackageGraph, PackageName};

use crate::{
Expand Down Expand Up @@ -194,11 +194,8 @@ impl PackageGraphResult {
// We sort packages to ensure deterministic behavior
let sorted_packages = packages.sorted_by(|(a, _), (b, _)| a.cmp(b));
for (package_name, config) in sorted_packages {
if let Some(pkg) = MICROFRONTENDS_PACKAGES
.iter()
.find(|static_pkg| package_name == **static_pkg)
{
mfe_package = Some(*pkg);
if package_name == MICROFRONTENDS_PACKAGE {
mfe_package = Some(MICROFRONTENDS_PACKAGE);
}

let Some(config) = config.or_else(|err| match err {
Expand Down Expand Up @@ -265,10 +262,7 @@ impl ConfigInfo {
#[cfg(test)]
mod test {
use serde_json::json;
use test_case::test_case;
use turborepo_microfrontends::{
MICROFRONTENDS_PACKAGE_EXTERNAL, MICROFRONTENDS_PACKAGE_INTERNAL,
};
use turborepo_microfrontends::MICROFRONTENDS_PACKAGE;

use super::*;

Expand All @@ -281,7 +275,7 @@ mod test {
for _dev_task in $dev_tasks.as_slice() {
_dev_tasks.insert(crate::run::task_id::TaskName::from(*_dev_task).task_id().unwrap().into_owned());
}
_map.insert($config_owner.to_string(), ConfigInfo { tasks: _dev_tasks, version: "2", path: None });
_map.insert($config_owner.to_string(), ConfigInfo { tasks: _dev_tasks, version: "1", path: None });
)+
_map
}
Expand All @@ -303,7 +297,7 @@ mod test {
pub const fn new(package_name: &'static str) -> Self {
Self {
package_name,
version: "2",
version: "1",
result: None,
}
}
Expand Down Expand Up @@ -357,51 +351,11 @@ mod test {
}
}

const NON_MFE_PKG: PackageUpdateTest = PackageUpdateTest::new("other-pkg");
const MFE_CONFIG_PKG: PackageUpdateTest = PackageUpdateTest::new("z-config-pkg")
.v1()
.proxy_only("z-config-pkg#proxy");
const MFE_CONFIG_PKG_DEV_TASK: PackageUpdateTest =
PackageUpdateTest::new("web").dev("web#dev", "web#proxy");
const DEFAULT_APP_PROXY: PackageUpdateTest =
PackageUpdateTest::new("docs").dev("docs#serve", "web#proxy");
const DEFAULT_APP_PROXY_AND_DEV: PackageUpdateTest =
PackageUpdateTest::new("web").dev("web#dev", "web#proxy");

#[test_case(NON_MFE_PKG)]
#[test_case(MFE_CONFIG_PKG)]
#[test_case(MFE_CONFIG_PKG_DEV_TASK)]
#[test_case(DEFAULT_APP_PROXY)]
#[test_case(DEFAULT_APP_PROXY_AND_DEV)]
fn test_package_turbo_json_update(test: PackageUpdateTest) {
let mut configs = mfe_configs!(
"z-config-pkg" => ["web#dev", "docs#dev"],
"web" => ["web#dev", "docs#serve"]
);
configs.get_mut("z-config-pkg").unwrap().version = "1";
let mfe = MicrofrontendsConfigs {
configs,
mfe_package: None,
};
assert_eq!(
mfe.package_turbo_json_update(&test.package_name()),
test.expected()
);
}

#[test]
fn test_mfe_package_is_found() {
let result = PackageGraphResult::new(
vec![
// These should never be present in the same graph, but if for some reason they
// are, we defer to the external variant.
(MICROFRONTENDS_PACKAGE_EXTERNAL, Ok(None)),
(MICROFRONTENDS_PACKAGE_INTERNAL, Ok(None)),
]
.into_iter(),
)
.unwrap();
assert_eq!(result.mfe_package, Some(MICROFRONTENDS_PACKAGE_EXTERNAL));
let result =
PackageGraphResult::new(vec![(MICROFRONTENDS_PACKAGE, Ok(None))].into_iter()).unwrap();
assert_eq!(result.mfe_package, Some(MICROFRONTENDS_PACKAGE));
}

#[test]
Expand Down Expand Up @@ -464,7 +418,7 @@ mod test {
fn test_dev_task_collection() {
let config = MFEConfig::from_str(
&serde_json::to_string_pretty(&json!({
"version": "2",
"version": "1",
"applications": {
"web": {},
"docs": {
Expand Down Expand Up @@ -508,56 +462,4 @@ mod test {
.unwrap();
assert_eq!(actual.global_deps, &["web/microfrontends.json".to_owned()]);
}

#[test]
fn test_v2_and_v1() {
let config_v2 = MFEConfig::from_str(
&serde_json::to_string_pretty(&json!({
"version": "2",
"applications": {
"web": {},
"docs": {
"development": {
"task": "serve"
}
}
}
}))
.unwrap(),
"something.txt",
)
.unwrap();
let config_v1 = MFEConfig::from_str(
&serde_json::to_string_pretty(&json!({
"version": "1",
"applications": {
"web": {},
"docs": {
"development": {
"task": "serve"
}
}
}
}))
.unwrap(),
"something.txt",
)
.unwrap();
let result = PackageGraphResult::new(
vec![
("web", Ok(Some(config_v2))),
("docs", Ok(None)),
("mfe-config", Ok(Some(config_v1))),
]
.into_iter(),
)
.unwrap();
let mut expected = mfe_configs!(
"web" => ["web#dev", "docs#serve"],
"mfe-config" => ["web#dev", "docs#serve"]
);
expected.get_mut("mfe-config").unwrap().version = "1";

assert_eq!(result.configs, expected,)
}
}
5 changes: 3 additions & 2 deletions crates/turborepo-lib/src/task_graph/visitor/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{collections::HashSet, path::PathBuf};

use turbopath::AbsoluteSystemPath;
use turborepo_env::EnvironmentVariableMap;
use turborepo_microfrontends::MICROFRONTENDS_PACKAGES;
use turborepo_microfrontends::MICROFRONTENDS_PACKAGE;
use turborepo_repository::package_graph::{PackageGraph, PackageInfo, PackageName};

use super::Error;
Expand Down Expand Up @@ -206,7 +206,8 @@ impl<'a> CommandProvider for MicroFrontendProxyProvider<'a> {
let has_mfe_dependency = package_info
.package_json
.all_dependencies()
.any(|(package, _version)| MICROFRONTENDS_PACKAGES.contains(&package.as_str()));
.any(|(package, _version)| package.as_str() == MICROFRONTENDS_PACKAGE);

if !has_mfe_dependency {
let mfe_config_filename = self.mfe_configs.config_filename(task_id.package());
return Err(Error::MissingMFEDependency {
Expand Down
2 changes: 1 addition & 1 deletion crates/turborepo-lib/src/turbo_json/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ mod test {
(
"web",
turborepo_microfrontends::Config::from_str(
r#"{"version": "2", "applications": {"web": {}, "docs": {}}}"#,
r#"{"version": "1", "applications": {"web": {}, "docs": {}}}"#,
"mfe.json",
)
.map(Some),
Expand Down
87 changes: 35 additions & 52 deletions crates/turborepo-microfrontends/fixtures/sample.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"version": "1",
"applications": {
"main-site": {
"default": true,
"development": {
"local": {
"protocol": "http",
Expand All @@ -19,40 +18,35 @@
"host": "main.com"
},
"vercel": {
"projectId": "id1",
"projectName": "main-site"
"projectId": "id1"
}
},
"vercel-marketing": {
"default": false,
"routing": {
"assetPrefix": "market-site",
"matches": [
{
"group": "blog",
"paths": [
"/blog",
"/blog/:slug*",
"/press",
"/changelog",
"/changelog/:slug*",
"/customers/:slug*"
]
},
{
"group": "navbar",
"paths": [
"/",
"/contact",
"/pricing",
"/enterprise",
// Resources
"/customers",
"/solutions/composable-commerce"
]
}
]
},
"routing": [
{
"group": "blog",
"paths": [
"/blog",
"/blog/:slug*",
"/press",
"/changelog",
"/changelog/:slug*",
"/customers/:slug*"
]
},
{
"group": "navbar",
"paths": [
"/",
"/contact",
"/pricing",
"/enterprise",
// Resources
"/customers",
"/solutions/composable-commerce"
]
}
],
"development": {
"local": {
"protocol": "http",
Expand All @@ -69,20 +63,15 @@
"host": "market.main.com"
},
"vercel": {
"projectId": "id2",
"projectName": "market-site"
"projectId": "id2"
}
},
"foo-docs": {
"default": false,
"routing": {
"assetPrefix": "foo",
"matches": [
{
"paths": ["/foo/:path*"]
}
]
},
"routing": [
{
"paths": ["/foo/:path*"]
}
],
"development": {
"fallback": {
"protocol": "https",
Expand All @@ -99,16 +88,11 @@
"host": "foo.main.com"
},
"vercel": {
"projectId": "id3",
"projectName": "foo-docs"
"projectId": "id3"
}
},
"docs": {
"default": false,
"routing": {
"assetPrefix": "docs",
"matches": []
},
"routing": [],
"development": {
"fallback": {
"protocol": "https",
Expand All @@ -125,8 +109,7 @@
"host": "docs.main.com"
},
"vercel": {
"projectId": "id4",
"projectName": "docs-site"
"projectId": "id4"
}
}
}
Expand Down
Loading

0 comments on commit ca2e681

Please sign in to comment.