From b686907a45bb7d113f863cca7c52754027e449c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 6 Oct 2021 19:07:04 +0200 Subject: 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. --- cli/tests/integration/compat_tests.rs | 7 ++++++- cli/tests/testdata/compat/existing_import_map.out | 3 ++- cli/tests/testdata/compat/globals.out | 7 +++++++ cli/tests/testdata/compat/globals.ts | 8 ++++++++ 4 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 cli/tests/testdata/compat/globals.out create mode 100644 cli/tests/testdata/compat/globals.ts (limited to 'cli/tests') diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs index e1da32caf..6743dada1 100644 --- a/cli/tests/integration/compat_tests.rs +++ b/cli/tests/integration/compat_tests.rs @@ -2,6 +2,11 @@ use crate::itest; +itest!(globals { + args: "run --compat --unstable --allow-read --allow-env compat/globals.ts", + output: "compat/globals.out", +}); + itest!(fs_promises { args: "run --compat --unstable -A compat/fs_promises.js", output: "compat/fs_promises.out", @@ -13,7 +18,7 @@ itest!(node_prefix_fs_promises { }); itest!(existing_import_map { - args: "run --compat --import-map compat/existing_import_map.json compat/fs_promises.js", + args: "run --compat --unstable --import-map compat/existing_import_map.json compat/fs_promises.js", output: "compat/existing_import_map.out", exit_code: 1, }); diff --git a/cli/tests/testdata/compat/existing_import_map.out b/cli/tests/testdata/compat/existing_import_map.out index 0e319b115..cbff0cc51 100644 --- a/cli/tests/testdata/compat/existing_import_map.out +++ b/cli/tests/testdata/compat/existing_import_map.out @@ -2,4 +2,5 @@ Some Node built-ins were not added to the import map: - "fs/promises" already exists and is mapped to "[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. -error: Cannot resolve module [WILDCARD] \ No newline at end of file +[WILDCARD] +error: Cannot resolve module [WILDCARD] diff --git a/cli/tests/testdata/compat/globals.out b/cli/tests/testdata/compat/globals.out new file mode 100644 index 000000000..0bc09137b --- /dev/null +++ b/cli/tests/testdata/compat/globals.out @@ -0,0 +1,7 @@ +[WILDCARD] +process { +[WILDCARD] +} +[Function: Buffer] +[Function: setImmediate] +[Function: clearTimeout] diff --git a/cli/tests/testdata/compat/globals.ts b/cli/tests/testdata/compat/globals.ts new file mode 100644 index 000000000..39a555cbf --- /dev/null +++ b/cli/tests/testdata/compat/globals.ts @@ -0,0 +1,8 @@ +if (global != window) { + throw new Error("global is not equal to window"); +} + +console.log(process); +console.log(Buffer); +console.log(setImmediate); +console.log(clearImmediate); -- cgit v1.2.3