Skip to content

Commit be288ba

Browse files
authored
feat!: remove the constraint on packages exports default must be the last one (#171)
closes #160 The spec never mentioned the logic where "default" must be last or it should throw an error. https://nodejs.org/api/esm.html#resolution-and-loading-algorithm `enhanced-resolve` took the meaning from https://nodejs.org/docs/v20.13.1/api/packages.html#conditional-exports "This condition should always come last." This statement is not part of the specification, it is a recommendation.
1 parent e1713c5 commit be288ba

File tree

3 files changed

+4
-14
lines changed

3 files changed

+4
-14
lines changed

src/lib.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,19 +1512,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
15121512
JSONValue::Object(target) => {
15131513
// 1. If exports contains any index property keys, as defined in ECMA-262 6.1.7 Array Index, throw an Invalid Package Configuration error.
15141514
// 2. For each property p of target, in object insertion order as,
1515-
for (i, (key, target_value)) in target.iter().enumerate() {
1516-
// https://nodejs.org/api/packages.html#conditional-exports
1517-
// "default" - the generic fallback that always matches. Can be a CommonJS or ES module file. This condition should always come last.
1518-
// Note: node.js does not throw this but enhanced-resolve does.
1519-
let is_default = key == "default";
1520-
if i < target.len() - 1 && is_default {
1521-
return Err(ResolveError::InvalidPackageConfigDefault(
1522-
package_url.join("package.json"),
1523-
));
1524-
}
1525-
1515+
for (key, target_value) in target {
15261516
// 1. If p equals "default" or conditions contains an entry for p, then
1527-
if is_default || conditions.contains(key) {
1517+
if key == "default" || conditions.contains(key) {
15281518
// 1. Let targetValue be the value of the p property in target.
15291519
// 2. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, targetValue, patternMatch, isImports, conditions).
15301520
let resolved = self.package_target_resolve(

src/tests/exports_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,7 @@ fn test_cases() {
814814
},
815815
TestCase {
816816
name: "Direct mapping #7",
817-
expect: None,
817+
expect: Some(vec!["./src/index.js"]), // `enhanced_resolve` is `None`
818818
exports_field: exports_field(json!({
819819
".": {
820820
"default": "./src/index.js",

src/tests/imports_field.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ fn test_cases() {
507507
},
508508
TestCase {
509509
name: "Direct mapping #7",
510-
expect: None,
510+
expect: Some(vec!["./src/index.js"]), // `enhanced_resolve` is `None`
511511
imports_field: imports_field(json!({
512512
"#a": {
513513
"default": "./src/index.js",

0 commit comments

Comments
 (0)