diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2021-10-06 19:07:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-06 19:07:04 +0200 |
commit | b686907a45bb7d113f863cca7c52754027e449c0 (patch) | |
tree | 9bc296cc2ade609c4276fce1f14128338c0e6f1f /cli/compat.rs | |
parent | b033a7a6d4ac6f1d3e20b5d113cca2fd85cacfc3 (diff) |
feat(compat): inject Node globals (#12342)
This commit adds automatic injection of Node globals when "--compat" flag
is present.
This is done by executing "https://deno.land/std/node/global.ts" as a "side module",
before main module is executed.
This commit makes "--compat" required to be used with "--unstable" flag, as some
of Node globals require unstable Deno APIs.
Diffstat (limited to 'cli/compat.rs')
-rw-r--r-- | cli/compat.rs | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/cli/compat.rs b/cli/compat.rs index eb67f4d12..4b64a501d 100644 --- a/cli/compat.rs +++ b/cli/compat.rs @@ -1,7 +1,10 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. +use deno_core::url::Url; use std::collections::HashMap; +static STD_NODE: &str = "https://deno.land/std/node/"; + static SUPPORTED_MODULES: &[&str] = &[ "assert", "assert/strict", @@ -47,12 +50,20 @@ static SUPPORTED_MODULES: &[&str] = &[ "zlib", ]; +pub fn get_node_globals_url() -> Url { + Url::parse(&format!("{}global.ts", STD_NODE)).unwrap() +} + +/// Create a map that can be used to update import map. +/// +/// Keys are built-in Node modules (and built-ins prefixed with "node:"), while +/// values are URLs pointing to relevant files in deno.land/std/node/ directory. pub fn get_mapped_node_builtins() -> HashMap<String, String> { let mut mappings = HashMap::new(); for module in SUPPORTED_MODULES { // TODO(bartlomieju): this is unversioned, and should be fixed to use latest stable? - let module_url = format!("https://deno.land/std/node/{}.ts", module); + let module_url = format!("{}{}.ts", STD_NODE, module); mappings.insert(module.to_string(), module_url.clone()); // Support for `node:<module_name>` |