Skip to content

feat(mfe): v2->v1 & make version optional #9787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 10 additions & 75 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 @@ -266,9 +263,7 @@ impl ConfigInfo {
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 +276,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 +298,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 @@ -391,17 +386,9 @@ mod test {

#[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 +451,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 +495,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
Loading