Skip to content

Commit

Permalink
feat!: remove the constraint on packages exports default must be th…
Browse files Browse the repository at this point in the history
…e 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.
  • Loading branch information
Boshen committed May 27, 2024
1 parent e1713c5 commit 10110b2
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 14 deletions.
14 changes: 2 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1512,19 +1512,9 @@ impl<Fs: FileSystem> ResolverGeneric<Fs> {
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(
Expand Down
2 changes: 1 addition & 1 deletion src/tests/exports_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/tests/imports_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 10110b2

Please sign in to comment.