mirror of
https://github.com/denoland/deno.git
synced 2025-01-05 13:59:01 -05:00
fix(npm): support dynamic import of Deno TS from npm package (#19858)
Closes #19843
This commit is contained in:
parent
d267eaa3b9
commit
e771bff44b
8 changed files with 38 additions and 5 deletions
|
@ -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 {
|
||||
|
|
3
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/add.ts
vendored
Normal file
3
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/add.ts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function add(a: number, b: number) {
|
||||
return a + b;
|
||||
}
|
2
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.out
vendored
Normal file
2
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.out
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
3
|
||||
-1
|
8
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.ts
vendored
Normal file
8
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/main.ts
vendored
Normal file
|
@ -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));
|
3
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/subtract.mts
vendored
Normal file
3
cli/tests/testdata/npm/dynamic_import_deno_ts_from_npm/subtract.mts
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function subtract(a: number, b: number) {
|
||||
return a - b;
|
||||
}
|
3
cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/index.js
vendored
Normal file
3
cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/index.js
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
export function dynamicImport(url) {
|
||||
return import(url);
|
||||
}
|
5
cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/package.json
vendored
Normal file
5
cli/tests/testdata/npm/registry/@denotest/dynamic-import/1.0.0/package.json
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"name": "@denotest/dynamic-import",
|
||||
"type": "module",
|
||||
"version": "1.0.0"
|
||||
}
|
|
@ -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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue