1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-07 22:58:24 -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 cjihrig
parent 55b85d4992
commit 89ef56a170
No known key found for this signature in database
GPG key ID: 7434390BDBE9B9C5
7 changed files with 41 additions and 7 deletions

View file

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

View file

@ -138,7 +138,10 @@ static SUPPORTED_MODULES: &[NodeModulePolyfill] = &[
}, },
NodeModulePolyfill { NodeModulePolyfill {
name: "module", 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 { NodeModulePolyfill {
name: "net", 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> { 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) { if let Some(module) = find_builtin_node_module(specifier) {
let module_url = NODE_COMPAT_URL.join(module.specifier).unwrap(); let module_url = NODE_COMPAT_URL.join(module.specifier).unwrap();
return Ok(module_url); return Ok(module_url);

View file

@ -327,6 +327,13 @@ itest!(deno_run_non_existent {
exit_code: 1, 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] #[test]
fn ensure_registry_files_local() { fn ensure_registry_files_local() {
// ensures the registry files all point at local tarballs // 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"
}