1
0
Fork 0
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:
David Sherret 2023-07-17 17:17:58 -04:00 committed by Bartek Iwańczuk
parent d267eaa3b9
commit e771bff44b
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
8 changed files with 38 additions and 5 deletions

View file

@ -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 {

View file

@ -0,0 +1,3 @@
export function add(a: number, b: number) {
return a + b;
}

View file

@ -0,0 +1,2 @@
3
-1

View 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));

View file

@ -0,0 +1,3 @@
export function subtract(a: number, b: number) {
return a - b;
}

View file

@ -0,0 +1,3 @@
export function dynamicImport(url) {
return import(url);
}

View file

@ -0,0 +1,5 @@
{
"name": "@denotest/dynamic-import",
"type": "module",
"version": "1.0.0"
}

View file

@ -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))
}