Skip to content

Commit

Permalink
feat(compat): add support for node: prefixed built-ins (denoland#12337)
Browse files Browse the repository at this point in the history
Adds support for "node:" prefix for Node built-ins in "--compat" mode.

As per https://nodejs.org/api/esm.html#esm_node_imports
  • Loading branch information
bartlomieju committed Oct 10, 2021
1 parent d5e5925 commit 8b32e81
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
7 changes: 6 additions & 1 deletion cli/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,12 @@ pub fn get_mapped_node_builtins() -> HashMap<String, String> {
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);
mappings.insert(module.to_string(), module_url);
mappings.insert(module.to_string(), module_url.clone());

// Support for `node:<module_name>`
// https://nodejs.org/api/esm.html#esm_node_imports
let node_prefixed = format!("node:{}", module);
mappings.insert(node_prefixed, module_url);
}

mappings
Expand Down
5 changes: 5 additions & 0 deletions cli/tests/integration/compat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ itest!(fs_promises {
output: "compat/fs_promises.out",
});

itest!(node_prefix_fs_promises {
args: "run --compat --unstable -A compat/node_fs_promises.js",
output: "compat/fs_promises.out",
});

itest!(existing_import_map {
args: "run --compat --import-map compat/existing_import_map.json compat/fs_promises.js",
output: "compat/existing_import_map.out",
Expand Down
3 changes: 3 additions & 0 deletions cli/tests/testdata/compat/node_fs_promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import fs from "node:fs/promises";
const data = await fs.readFile("compat/test.txt", "utf-8");
console.log(data);

0 comments on commit 8b32e81

Please sign in to comment.