From 10110b2ab06274298f4326ebb8c7d28430773203 Mon Sep 17 00:00:00 2001 From: Boshen Date: Mon, 27 May 2024 22:28:30 +0800 Subject: [PATCH] feat!: remove the constraint on packages exports `default` must be the last one 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. --- src/lib.rs | 14 ++------------ src/tests/exports_field.rs | 2 +- src/tests/imports_field.rs | 2 +- 3 files changed, 4 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 1d542251..37bc2b13 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1512,19 +1512,9 @@ impl ResolverGeneric { JSONValue::Object(target) => { // 1. If exports contains any index property keys, as defined in ECMA-262 6.1.7 Array Index, throw an Invalid Package Configuration error. // 2. For each property p of target, in object insertion order as, - for (i, (key, target_value)) in target.iter().enumerate() { - // https://nodejs.org/api/packages.html#conditional-exports - // "default" - the generic fallback that always matches. Can be a CommonJS or ES module file. This condition should always come last. - // Note: node.js does not throw this but enhanced-resolve does. - let is_default = key == "default"; - if i < target.len() - 1 && is_default { - return Err(ResolveError::InvalidPackageConfigDefault( - package_url.join("package.json"), - )); - } - + for (key, target_value) in target { // 1. If p equals "default" or conditions contains an entry for p, then - if is_default || conditions.contains(key) { + if key == "default" || conditions.contains(key) { // 1. Let targetValue be the value of the p property in target. // 2. Let resolved be the result of PACKAGE_TARGET_RESOLVE( packageURL, targetValue, patternMatch, isImports, conditions). let resolved = self.package_target_resolve( diff --git a/src/tests/exports_field.rs b/src/tests/exports_field.rs index 669c1adf..fd68c1f7 100644 --- a/src/tests/exports_field.rs +++ b/src/tests/exports_field.rs @@ -814,7 +814,7 @@ fn test_cases() { }, TestCase { name: "Direct mapping #7", - expect: None, + expect: Some(vec!["./src/index.js"]), // `enhanced_resolve` is `None` exports_field: exports_field(json!({ ".": { "default": "./src/index.js", diff --git a/src/tests/imports_field.rs b/src/tests/imports_field.rs index 9f9a9726..c928627a 100644 --- a/src/tests/imports_field.rs +++ b/src/tests/imports_field.rs @@ -507,7 +507,7 @@ fn test_cases() { }, TestCase { name: "Direct mapping #7", - expect: None, + expect: Some(vec!["./src/index.js"]), // `enhanced_resolve` is `None` imports_field: imports_field(json!({ "#a": { "default": "./src/index.js",