diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2022-08-29 19:15:20 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-29 19:15:20 +0200 |
commit | 2851a980723f27c245e6e3790a53dd26136f225f (patch) | |
tree | 0ce2420293ba50266dc24be7f4f0ea2e0508c036 | |
parent | ea838d27a2b1bd7b396843b645aff544f84265c1 (diff) |
fix(npm): conditional exports with wildcards (#15652)
10 files changed, 59 insertions, 2 deletions
diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index c59b18be3..85256df3f 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -68,6 +68,13 @@ itest!(compare_globals { http_server: true, }); +itest!(conditional_exports { + args: "run --allow-read --unstable npm/conditional_exports/main.js", + output: "npm/conditional_exports/main.out", + envs: env_vars(), + http_server: true, +}); + itest!(dynamic_import { args: "run --allow-read --allow-env --unstable npm/dynamic_import/main.ts", output: "npm/dynamic_import/main.out", diff --git a/cli/tests/testdata/npm/conditional_exports/main.js b/cli/tests/testdata/npm/conditional_exports/main.js new file mode 100644 index 000000000..3d0dd661a --- /dev/null +++ b/cli/tests/testdata/npm/conditional_exports/main.js @@ -0,0 +1,9 @@ +import mod from "npm:@denotest/conditional-exports"; +import client from "npm:@denotest/conditional-exports/client"; +import clientFoo from "npm:@denotest/conditional-exports/client/foo"; +import clientBar from "npm:@denotest/conditional-exports/client/bar"; + +console.log(mod); +console.log(client); +console.log(clientFoo); +console.log(clientBar); diff --git a/cli/tests/testdata/npm/conditional_exports/main.out b/cli/tests/testdata/npm/conditional_exports/main.out new file mode 100644 index 000000000..0f2a4479e --- /dev/null +++ b/cli/tests/testdata/npm/conditional_exports/main.out @@ -0,0 +1,6 @@ +Download http://localhost:4545/npm/registry/@denotest/conditional-exports +Download http://localhost:4545/npm/registry/@denotest/conditional-exports/1.0.0.tgz +{ hello: "from esm" } +{ hello: "from esm client" } +{ hello: "from esm client foo" } +{ hello: "from esm client bar" } diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/cjs/index.cjs b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/cjs/index.cjs new file mode 100644 index 000000000..16895e48c --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/cjs/index.cjs @@ -0,0 +1,3 @@ +module.exports = { + hello: "from cjs" +};
\ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/bar.js b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/bar.js new file mode 100644 index 000000000..12352639d --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/bar.js @@ -0,0 +1,3 @@ +export default { + hello: "from esm client bar", +}
\ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/foo.js b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/foo.js new file mode 100644 index 000000000..1ab5baf1b --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/foo.js @@ -0,0 +1,3 @@ +export default { + hello: "from esm client foo", +}
\ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/index.js b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/index.js new file mode 100644 index 000000000..86f246be4 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/client/index.js @@ -0,0 +1,3 @@ +export default { + hello: "from esm client", +}
\ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/index.js b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/index.js new file mode 100644 index 000000000..38dae7d93 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/esm/index.js @@ -0,0 +1,3 @@ +export default { + hello: "from esm", +}
\ No newline at end of file diff --git a/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/package.json new file mode 100644 index 000000000..c02015835 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/conditional-exports/1.0.0/package.json @@ -0,0 +1,20 @@ +{ + "name": "@denotest/conditional-exports", + "version": "1.0.0", + "type": "module", + "exports": { + ".": { + "types": "./types/src/index.d.ts", + "require": "./cjs/index.cjs", + "import": "./esm/index.js" + }, + "./client": { + "types": "./types/src/client/index.d.ts", + "import": "./esm/client/index.js" + }, + "./client/*": { + "types": "./types/src/client/*.d.ts", + "import": "./esm/client/*.js" + } + } +} diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index b839d4144..b77e6f690 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -451,7 +451,7 @@ pub fn package_exports_resolve( for key in package_exports.keys() { let pattern_index = key.find('*'); if let Some(pattern_index) = pattern_index { - let key_sub = &key[0..=pattern_index]; + let key_sub = &key[0..pattern_index]; if package_subpath.starts_with(key_sub) { // When this reaches EOL, this can throw at the top of the whole function: // @@ -472,7 +472,7 @@ pub fn package_exports_resolve( best_match = key; best_match_subpath = Some( package_subpath - [pattern_index..=(package_subpath.len() - pattern_trailer.len())] + [pattern_index..(package_subpath.len() - pattern_trailer.len())] .to_string(), ); } |