From 617eeabe8369d7bfca7951d1cd55ac58ede1f9fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Mon, 18 Oct 2021 19:36:28 +0200 Subject: feat(unstable): Node CJS and ESM resolvers for compat mode (#12424) This commit adds CJS and ESM Node resolvers to the "--compat" mode. The functionality is spread across "cli/compat" module and Node compatibility layer in "deno_std/node"; this stems from the fact that ES module resolution can only be implemented in Rust as it needs to directly integrated with "deno_core"; however "deno_std/node" already provided CJS module resolution. Currently this resolution is only active when running a files using "deno run --compat --unstable ", and is not available in other subcommands, which will be changed in follow up commits. --- cli/tests/integration/compat_tests.rs | 10 ++-------- cli/tests/testdata/compat/existing_import_map.json | 5 ----- cli/tests/testdata/compat/existing_import_map.out | 7 ------- cli/tests/testdata/compat/fs_promises.js | 3 --- cli/tests/testdata/compat/fs_promises.mjs | 3 +++ cli/tests/testdata/compat/globals.out | 4 +++- cli/tests/testdata/compat/node_fs_promises.js | 3 --- cli/tests/testdata/compat/node_fs_promises.mjs | 3 +++ 8 files changed, 11 insertions(+), 27 deletions(-) delete mode 100644 cli/tests/testdata/compat/existing_import_map.json delete mode 100644 cli/tests/testdata/compat/existing_import_map.out delete mode 100644 cli/tests/testdata/compat/fs_promises.js create mode 100644 cli/tests/testdata/compat/fs_promises.mjs delete mode 100644 cli/tests/testdata/compat/node_fs_promises.js create mode 100644 cli/tests/testdata/compat/node_fs_promises.mjs (limited to 'cli/tests') diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs index 6b6ab81b5..17388a78e 100644 --- a/cli/tests/integration/compat_tests.rs +++ b/cli/tests/integration/compat_tests.rs @@ -9,21 +9,15 @@ itest!(globals { }); itest!(fs_promises { - args: "run --compat --unstable -A compat/fs_promises.js", + args: "run --compat --unstable -A compat/fs_promises.mjs", output: "compat/fs_promises.out", }); itest!(node_prefix_fs_promises { - args: "run --compat --unstable -A compat/node_fs_promises.js", + args: "run --compat --unstable -A compat/node_fs_promises.mjs", output: "compat/fs_promises.out", }); -itest!(existing_import_map { - args: "run --compat --unstable --import-map compat/existing_import_map.json compat/fs_promises.js", - output: "compat/existing_import_map.out", - exit_code: 1, -}); - #[test] fn globals_in_repl() { let (out, _err) = util::run_and_collect_output_with_args( diff --git a/cli/tests/testdata/compat/existing_import_map.json b/cli/tests/testdata/compat/existing_import_map.json deleted file mode 100644 index db59c0cc2..000000000 --- a/cli/tests/testdata/compat/existing_import_map.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "imports": { - "fs/promises": "./non_existent_file.js" - } -} diff --git a/cli/tests/testdata/compat/existing_import_map.out b/cli/tests/testdata/compat/existing_import_map.out deleted file mode 100644 index 46125d411..000000000 --- a/cli/tests/testdata/compat/existing_import_map.out +++ /dev/null @@ -1,7 +0,0 @@ -[WILDCARD] -Some Node built-ins were not added to the import map: - - "fs/promises" already exists and is mapped to "file://[WILDCARD]/non_existent_file.js" -If you want to use Node built-ins provided by Deno remove listed specifiers from "imports" mapping in the import map file. -[WILDCARD] -error: Cannot load module "file://[WILDCARD]/non_existent_file.js". - at file://[WILDCARD]/fs_promises.js:1:16 diff --git a/cli/tests/testdata/compat/fs_promises.js b/cli/tests/testdata/compat/fs_promises.js deleted file mode 100644 index 3f7b4c935..000000000 --- a/cli/tests/testdata/compat/fs_promises.js +++ /dev/null @@ -1,3 +0,0 @@ -import fs from "fs/promises"; -const data = await fs.readFile("compat/test.txt", "utf-8"); -console.log(data); diff --git a/cli/tests/testdata/compat/fs_promises.mjs b/cli/tests/testdata/compat/fs_promises.mjs new file mode 100644 index 000000000..3f7b4c935 --- /dev/null +++ b/cli/tests/testdata/compat/fs_promises.mjs @@ -0,0 +1,3 @@ +import fs from "fs/promises"; +const data = await fs.readFile("compat/test.txt", "utf-8"); +console.log(data); diff --git a/cli/tests/testdata/compat/globals.out b/cli/tests/testdata/compat/globals.out index 0bc09137b..32230fda3 100644 --- a/cli/tests/testdata/compat/globals.out +++ b/cli/tests/testdata/compat/globals.out @@ -2,6 +2,8 @@ process { [WILDCARD] } -[Function: Buffer] +[Function: Buffer] { +[WILDCARD] +} [Function: setImmediate] [Function: clearTimeout] diff --git a/cli/tests/testdata/compat/node_fs_promises.js b/cli/tests/testdata/compat/node_fs_promises.js deleted file mode 100644 index b2174b22b..000000000 --- a/cli/tests/testdata/compat/node_fs_promises.js +++ /dev/null @@ -1,3 +0,0 @@ -import fs from "node:fs/promises"; -const data = await fs.readFile("compat/test.txt", "utf-8"); -console.log(data); diff --git a/cli/tests/testdata/compat/node_fs_promises.mjs b/cli/tests/testdata/compat/node_fs_promises.mjs new file mode 100644 index 000000000..b2174b22b --- /dev/null +++ b/cli/tests/testdata/compat/node_fs_promises.mjs @@ -0,0 +1,3 @@ +import fs from "node:fs/promises"; +const data = await fs.readFile("compat/test.txt", "utf-8"); +console.log(data); -- cgit v1.2.3