From ed3a7ce2f719e64e59cfebb3d131a05a1694523b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 10 Feb 2023 12:40:45 +0100 Subject: refactor: allow to provide polyfills for Node modules from the snapshot (#17706) This commit does preparatory work to allow snapshotting Node.js compatibility layer, that currently lives in `std/node`. The logic was changed to allow loading some modules from the snapshot and some from the remote URL. Additionally "module_es_shim.js" that provides exports for "node:module" is now snapshotted. --- cli/node/mod.rs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) (limited to 'cli/node/mod.rs') diff --git a/cli/node/mod.rs b/cli/node/mod.rs index 9ab593304..dffcb7437 100644 --- a/cli/node/mod.rs +++ b/cli/node/mod.rs @@ -26,6 +26,7 @@ use deno_runtime::deno_node::package_resolve; use deno_runtime::deno_node::path_to_declaration_path; use deno_runtime::deno_node::NodeModuleKind; use deno_runtime::deno_node::NodeModulePolyfill; +use deno_runtime::deno_node::NodeModulePolyfillSpecifier; use deno_runtime::deno_node::NodePermissions; use deno_runtime::deno_node::NodeResolutionMode; use deno_runtime::deno_node::PackageJson; @@ -133,16 +134,18 @@ fn is_builtin_node_module(specifier: &str) -> bool { } pub fn resolve_builtin_node_module(specifier: &str) -> Result { - // 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); + match module.specifier { + // We will load the source code from the `std/node` polyfill. + NodeModulePolyfillSpecifier::StdNode(specifier) => { + let module_url = NODE_COMPAT_URL.join(specifier).unwrap(); + return Ok(module_url); + } + // The module has already been snapshotted and is present in the binary. + NodeModulePolyfillSpecifier::Embedded(specifier) => { + return Ok(ModuleSpecifier::parse(specifier).unwrap()); + } + } } Err(generic_error(format!( -- cgit v1.2.3