summaryrefslogtreecommitdiff
path: root/cli/node/mod.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2023-02-10 12:40:45 +0100
committerGitHub <noreply@github.com>2023-02-10 11:40:45 +0000
commited3a7ce2f719e64e59cfebb3d131a05a1694523b (patch)
treea355c3a0f17ec7dfb8bcdba964deb8dd241b6734 /cli/node/mod.rs
parent9ea899afa76949d2e1a2a6eabbbec14f139a7592 (diff)
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.
Diffstat (limited to 'cli/node/mod.rs')
-rw-r--r--cli/node/mod.rs21
1 files changed, 12 insertions, 9 deletions
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<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);
+ 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!(