diff --git a/cli/tests/integration/npm_tests.rs b/cli/tests/integration/npm_tests.rs index 2f998e3a25..f1ba0cdfa0 100644 --- a/cli/tests/integration/npm_tests.rs +++ b/cli/tests/integration/npm_tests.rs @@ -107,9 +107,14 @@ itest!(cjs_require_esm_mjs_error { itest!(require_esm_error { args: "run --allow-read --quiet node/require_esm_error/main.ts", output: "node/require_esm_error/main.out", + exit_code: 1, +}); + +itest!(dynamic_import_deno_ts_from_npm { + args: "run --allow-read --quiet npm/dynamic_import_deno_ts_from_npm/main.ts", + output: "npm/dynamic_import_deno_ts_from_npm/main.out", envs: env_vars_for_npm_tests(), http_server: true, - exit_code: 1, }); itest!(translate_cjs_to_esm { diff --git a/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/add.ts b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/add.ts new file mode 100644 index 0000000000..3b399665dc --- /dev/null +++ b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/add.ts @@ -0,0 +1,3 @@ +export function add(a: number, b: number) { + return a + b; +} diff --git a/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.out b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.out new file mode 100644 index 0000000000..81d7aba8ae --- /dev/null +++ b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.out @@ -0,0 +1,2 @@ +3 +-1 diff --git a/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.ts b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.ts new file mode 100644 index 0000000000..4d5deec481 --- /dev/null +++ b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.ts @@ -0,0 +1,8 @@ +import { dynamicImport } from "npm:@denotest/dynamic-import"; + +const { add } = await dynamicImport(new URL("./add.ts", import.meta.url)); +console.log(add(1, 2)); +const { subtract } = await dynamicImport( + new URL("./subtract.mts", import.meta.url), +); +console.log(subtract(1, 2)); diff --git a/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/subtract.mts b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/subtract.mts new file mode 100644 index 0000000000..4bf634170f --- /dev/null +++ b/cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/subtract.mts @@ -0,0 +1,3 @@ +export function subtract(a: number, b: number) { + return a - b; +} diff --git a/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/index.js b/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/index.js new file mode 100644 index 0000000000..4dc3831f9f --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/index.js @@ -0,0 +1,3 @@ +export function dynamicImport(url) { + return import(url); +} diff --git a/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/package.json b/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/package.json new file mode 100644 index 0000000000..fa970177c8 --- /dev/null +++ b/cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/package.json @@ -0,0 +1,5 @@ +{ + "name": "@denotest/dynamic-import", + "type": "module", + "version": "1.0.0" +} diff --git a/ext/node/resolution.rs b/ext/node/resolution.rs index 8470eba6b6..7485067892 100644 --- a/ext/node/resolution.rs +++ b/ext/node/resolution.rs @@ -435,10 +435,14 @@ impl NodeResolver { } } else if url_str.ends_with(".mjs") || url_str.ends_with(".d.mts") { Ok(NodeResolution::Esm(url)) - } else if url_str.ends_with(".ts") { - Err(generic_error(format!( - "TypeScript files are not supported in npm packages: {url}" - ))) + } else if url_str.ends_with(".ts") || url_str.ends_with(".mts") { + if self.in_npm_package(&url) { + Err(generic_error(format!( + "TypeScript files are not supported in npm packages: {url}" + ))) + } else { + Ok(NodeResolution::Esm(url)) + } } else { Ok(NodeResolution::CommonJs(url)) }