1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

fix(npm): use shim from deno_node crate for 'module' built-in module (#15881)

This commit is contained in:
Bartek Iwańczuk 2022-09-13 00:11:32 +02:00 committed by GitHub
parent 98454c1eb8
commit 233d5422fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 41 additions and 7 deletions

View file

@ -63,6 +63,7 @@ impl CliModuleLoader {
fn load_prepared_module(
&self,
specifier: &ModuleSpecifier,
maybe_referrer: Option<ModuleSpecifier>,
) -> Result<ModuleCodeSource, AnyError> {
if specifier.as_str() == "node:module" {
return Ok(ModuleCodeSource {
@ -121,10 +122,13 @@ impl CliModuleLoader {
media_type: *media_type,
})
}
_ => Err(anyhow!(
"Loading unprepared module: {}",
specifier.to_string()
)),
_ => {
let mut msg = format!("Loading unprepared module: {}", specifier);
if let Some(referrer) = maybe_referrer {
msg = format!("{}, imported from: {}", msg, referrer.as_str());
}
Err(anyhow!(msg))
}
}
}
@ -164,7 +168,7 @@ impl CliModuleLoader {
media_type: MediaType::from(specifier),
}
} else {
self.load_prepared_module(specifier)?
self.load_prepared_module(specifier, maybe_referrer)?
};
let code = if self.ps.options.is_inspecting() {
// we need the code with the source map in order for
@ -261,7 +265,7 @@ impl SourceMapGetter for CliModuleLoader {
"wasm" | "file" | "http" | "https" | "data" | "blob" => (),
_ => return None,
}
let source = self.load_prepared_module(&specifier).ok()?;
let source = self.load_prepared_module(&specifier, None).ok()?;
source_map_from_code(&source.code)
}

View file

@ -138,7 +138,10 @@ static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[
},
NodeModulePolyfill {
name: "module",
specifier: "node/module.ts",
// NOTE(bartlomieju): `module` is special, because we don't want to use
// `deno_std/node/module.ts`, but instead use a special shim that we
// provide in `ext/node`.
specifier: "[USE `deno_node::MODULE_ES_SHIM` to get this module]",
},
NodeModulePolyfill {
name: "net",
@ -265,6 +268,13 @@ fn is_builtin_node_module(specifier: &str) -> bool {
}
pub fn resolve_builtin_node_module(specifier: &str) -> Result<Url, AnyError> {
// NOTE(bartlomieju): `module` is special, because we don't want to use
// `deno_std/node/module.ts`, but instead use a special shim that we
// provide in `ext/node`.
if specifier == "module" {
return Ok(Url::parse("node:module").unwrap());
}
if let Some(module) = find_builtin_node_module(specifier) {
let module_url = NODE_COMPAT_URL.join(module.specifier).unwrap();
return Ok(module_url);

View file

@ -402,6 +402,13 @@ itest!(deno_run_non_existent {
exit_code: 1,
});
itest!(builtin_module_module {
args: "run --allow-read --quiet --unstable npm/builtin_module_module/main.js",
output: "npm/builtin_module_module/main.out",
envs: env_vars(),
http_server: true,
});
#[test]
fn ensure_registry_files_local() {
// ensures the registry files all point at local tarballs

View file

@ -0,0 +1 @@
import "npm:@denotest/builtin-module-module";

View file

@ -0,0 +1,2 @@
function
function

View file

@ -0,0 +1,5 @@
import m1 from "node:module";
import m2 from "module";
console.log(typeof m1.Module);
console.log(typeof m2.Module);

View file

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