Skip to content

Commit

Permalink
only emit unique subpath import conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam McKee committed Nov 6, 2024
1 parent 69d6eef commit 1f55a0d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 13 deletions.
33 changes: 20 additions & 13 deletions fn_build/src/runtime/node/node_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,20 @@ impl NodeConfig {
let mut result: NodeSubpathImports = Vec::new();
if let Value::Object(imports) = &package_json["imports"] {
for (map_from, map_to) in imports {
if !is_valid_subpath_import_map_from_specifier(map_from) {
continue;
}
if let Some(map_to) = map_to.as_str() {
if is_valid_subpath_import_specifier(map_to) {
if is_valid_subpath_import_map_from_specifier(map_from) {
if let Some(map_to) = map_to.as_str() {
if is_valid_subpath_import_specifier(map_to) {
result.push((
NodeSubpathImportSpecifier::from(map_from.as_str()),
vec![NodeSubpathImportSpecifier::from(map_to)],
));
}
} else if let Some(map_to) = map_to.as_object() {
result.push((
NodeSubpathImportSpecifier::from(map_from.as_str()),
vec![NodeSubpathImportSpecifier::from(map_to)],
Self::collect_subpath_import_conditions(map_to),
));
}
} else if let Some(map_to) = map_to.as_object() {
result.push((
NodeSubpathImportSpecifier::from(map_from.as_str()),
Self::collect_subpath_import_conditions(map_to),
));
}
}
}
Expand All @@ -90,10 +89,18 @@ impl NodeConfig {
{
if let Some(map_to) = map_to.as_str() {
if is_valid_subpath_import_specifier(map_to) {
result.push(NodeSubpathImportSpecifier::from(map_to));
let map_to = NodeSubpathImportSpecifier::from(map_to);
if !result.contains(&map_to) {
result.push(map_to);
}
}
} else if let Some(map_to) = map_to.as_object() {
result.append(&mut Self::collect_subpath_import_conditions(map_to));
result.append(
&mut Self::collect_subpath_import_conditions(map_to)
.into_iter()
.filter(|map_to| !result.contains(map_to))
.collect(),
);
}
}
result
Expand Down
57 changes: 57 additions & 0 deletions fn_build/src/runtime/node/node_config_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,60 @@ pub fn test_parse_node_config_parses_nesting_conditional_subpath_imports() {
.unwrap()
);
}

#[test]
pub fn test_parse_node_config_parses_nesting_conditional_subpath_imports_excludes_duplicates() {
let node_config = NodeConfig::parse_node_config(
r##"{
"imports": {
"#lib": {
"node": {
"import": "./lib/api.js",
"module-sync": "./lib/api.js",
"default": "./lib/api.js"
},
"default": {
"node": {
"import": {
"module-sync": "./lib/api.js"
}
}
}
},
"#lib2": "./lib/api.js"
}
}"##,
)
.unwrap();
assert_eq!(2, node_config.subpath_imports.len());
assert_eq!(
NodeSubpathImportSpecifier::Explicit("#lib".to_string()),
node_config.subpath_imports.first().unwrap().0
);
assert_eq!(1, node_config.subpath_imports.first().unwrap().1.len());
assert_eq!(
&NodeSubpathImportSpecifier::Explicit("./lib/api.js".to_string()),
node_config
.subpath_imports
.first()
.unwrap()
.1
.first()
.unwrap()
);
assert_eq!(
1,
node_config.subpath_imports.iter().nth(1).unwrap().1.len()
);
assert_eq!(
&NodeSubpathImportSpecifier::Explicit("./lib/api.js".to_string()),
node_config
.subpath_imports
.iter()
.nth(1)
.unwrap()
.1
.first()
.unwrap()
);
}

0 comments on commit 1f55a0d

Please sign in to comment.