From 790726559016c98655bd93c3f223f8ad5e480d41 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 25 Jul 2024 21:40:10 -0400 Subject: [PATCH] fix(node): cjs pkg dynamically importing esm-only pkg fails (#24730) --- ext/node_resolver/resolution.rs | 6 ++---- .../__test__.jsonc | 7 +++++++ .../main.out | 2 ++ .../main.ts | 4 ++++ .../node_modules/cjs-add/index.d.ts | 3 +++ .../node_modules/cjs-add/index.js | 4 ++++ .../node_modules/cjs-add/package.json | 3 +++ .../node_modules/esm-add/index.d.ts | 1 + .../node_modules/esm-add/index.js | 3 +++ .../node_modules/esm-add/package.json | 21 +++++++++++++++++++ .../package.json | 3 +++ 11 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/__test__.jsonc create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/main.out create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/main.ts create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.d.ts create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.js create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/package.json create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.d.ts create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.js create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/package.json create mode 100644 tests/specs/node/cjs_dynamic_import_esm_with_exports/package.json diff --git a/ext/node_resolver/resolution.rs b/ext/node_resolver/resolution.rs index d7918c75c6..25316c385e 100644 --- a/ext/node_resolver/resolution.rs +++ b/ext/node_resolver/resolution.rs @@ -221,10 +221,8 @@ impl NodeResolver { specifier, referrer, referrer_kind, - match referrer_kind { - NodeModuleKind::Esm => DEFAULT_CONDITIONS, - NodeModuleKind::Cjs => REQUIRE_CONDITIONS, - }, + // even though the referrer may be CJS, if we're here that means we're doing ESM resolution + DEFAULT_CONDITIONS, mode, )?; diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/__test__.jsonc b/tests/specs/node/cjs_dynamic_import_esm_with_exports/__test__.jsonc new file mode 100644 index 0000000000..980245cb9b --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/__test__.jsonc @@ -0,0 +1,7 @@ +{ + "envs": { + "DENO_FUTURE": "1" + }, + "args": "run --check main.ts", + "output": "main.out" +} diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.out b/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.out new file mode 100644 index 0000000000..e85a0148d4 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.out @@ -0,0 +1,2 @@ +Check file:///[WILDLINE]main.ts +3 diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.ts b/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.ts new file mode 100644 index 0000000000..4e25d92f09 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/main.ts @@ -0,0 +1,4 @@ +import { addAsync } from "cjs-add"; + +const value: number = await addAsync(1, 2); +console.log(value); diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.d.ts b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.d.ts new file mode 100644 index 0000000000..c2195de04d --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.d.ts @@ -0,0 +1,3 @@ +import add from "esm-add"; + +export function addAsync(a: number, b: number): Promise>; \ No newline at end of file diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.js b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.js new file mode 100644 index 0000000000..471a427bbf --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/index.js @@ -0,0 +1,4 @@ +module.exports.addAsync = async (a, b) => { + const add = await import("esm-add"); + return add.default(a, b); +}; \ No newline at end of file diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/package.json b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/package.json new file mode 100644 index 0000000000..7671348405 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/cjs-add/package.json @@ -0,0 +1,3 @@ +{ + "name": "cjs-add" +} \ No newline at end of file diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.d.ts b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.d.ts new file mode 100644 index 0000000000..01a61d4d53 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.d.ts @@ -0,0 +1 @@ +export default function add(a: number, b: number): number; diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.js b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.js new file mode 100644 index 0000000000..90e9423246 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/index.js @@ -0,0 +1,3 @@ +export default function add(a, b) { + return a + b; +} diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/package.json b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/package.json new file mode 100644 index 0000000000..879883352f --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/node_modules/esm-add/package.json @@ -0,0 +1,21 @@ +{ + "name": "esm-add", + "version": "0.12.1", + "license": "MIT", + "type": "module", + "main": "index.js", + "types": "index.d.ts", + "exports": { + ".": { + "types": "./index.d.ts", + "import": "./index.js" + }, + "./package.json": "./package.json" + }, + "files": [ + "files", + "index.js", + "index.d.ts", + "package.json" + ] +} diff --git a/tests/specs/node/cjs_dynamic_import_esm_with_exports/package.json b/tests/specs/node/cjs_dynamic_import_esm_with_exports/package.json new file mode 100644 index 0000000000..3dbc1ca591 --- /dev/null +++ b/tests/specs/node/cjs_dynamic_import_esm_with_exports/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +}