summaryrefslogtreecommitdiff
path: root/cli/tests
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2022-02-27 14:38:45 +0100
committerGitHub <noreply@github.com>2022-02-27 14:38:45 +0100
commita65ce33fabb44bb2d9ed04773f7f334ed9c9a6b5 (patch)
treef5c169945377c3f806b514162408b81b5611ad44 /cli/tests
parent4bea1d06c7ddb177ed20e0f32b70d7ff889871ab (diff)
feat(compat): CJS/ESM interoperability (#13553)
This commit adds CJS/ESM interoperability when running in --compat mode. Before executing files, they are analyzed and all CommonJS modules are transformed on the fly to a ES modules. This is done by utilizing analyze_cjs() functionality from deno_ast. After discovering exports and reexports, an ES module is rendered and saved in memory for later use. There's a caveat that all files ending with ".js" extension are considered as CommonJS modules (unless there's a related "package.json" with "type": "module").
Diffstat (limited to 'cli/tests')
-rw-r--r--cli/tests/integration/compat_tests.rs6
-rw-r--r--cli/tests/testdata/compat/import_cjs_from_esm.out1
-rw-r--r--cli/tests/testdata/compat/import_cjs_from_esm/imported.js9
-rw-r--r--cli/tests/testdata/compat/import_cjs_from_esm/main.mjs1
-rw-r--r--cli/tests/testdata/compat/import_cjs_from_esm/reexports.js1
-rw-r--r--cli/tests/testdata/compat/import_cjs_from_esm/reexports2.js2
6 files changed, 20 insertions, 0 deletions
diff --git a/cli/tests/integration/compat_tests.rs b/cli/tests/integration/compat_tests.rs
index 189e1eb41..c8fc1c0a0 100644
--- a/cli/tests/integration/compat_tests.rs
+++ b/cli/tests/integration/compat_tests.rs
@@ -95,6 +95,12 @@ itest!(compat_worker {
output: "compat/worker/worker_test.out",
});
+itest!(cjs_esm_interop {
+ args:
+ "run --compat --unstable -A --quiet --no-check compat/import_cjs_from_esm/main.mjs",
+ output: "compat/import_cjs_from_esm.out",
+});
+
#[test]
fn globals_in_repl() {
let (out, _err) = util::run_and_collect_output_with_args(
diff --git a/cli/tests/testdata/compat/import_cjs_from_esm.out b/cli/tests/testdata/compat/import_cjs_from_esm.out
new file mode 100644
index 000000000..ffaa5e406
--- /dev/null
+++ b/cli/tests/testdata/compat/import_cjs_from_esm.out
@@ -0,0 +1 @@
+{ a: "A", b: "B", foo: "foo", bar: "bar", fizz: { buzz: "buzz", fizz: "FIZZ" } }
diff --git a/cli/tests/testdata/compat/import_cjs_from_esm/imported.js b/cli/tests/testdata/compat/import_cjs_from_esm/imported.js
new file mode 100644
index 000000000..49ab4c782
--- /dev/null
+++ b/cli/tests/testdata/compat/import_cjs_from_esm/imported.js
@@ -0,0 +1,9 @@
+exports = {
+ a: "A",
+ b: "B",
+};
+exports.foo = "foo";
+exports.bar = "bar";
+exports.fizz = require("./reexports.js");
+
+console.log(exports);
diff --git a/cli/tests/testdata/compat/import_cjs_from_esm/main.mjs b/cli/tests/testdata/compat/import_cjs_from_esm/main.mjs
new file mode 100644
index 000000000..6fbed1b7c
--- /dev/null
+++ b/cli/tests/testdata/compat/import_cjs_from_esm/main.mjs
@@ -0,0 +1 @@
+import "./imported.js";
diff --git a/cli/tests/testdata/compat/import_cjs_from_esm/reexports.js b/cli/tests/testdata/compat/import_cjs_from_esm/reexports.js
new file mode 100644
index 000000000..62edb7708
--- /dev/null
+++ b/cli/tests/testdata/compat/import_cjs_from_esm/reexports.js
@@ -0,0 +1 @@
+module.exports = require("./reexports2.js");
diff --git a/cli/tests/testdata/compat/import_cjs_from_esm/reexports2.js b/cli/tests/testdata/compat/import_cjs_from_esm/reexports2.js
new file mode 100644
index 000000000..183d833b0
--- /dev/null
+++ b/cli/tests/testdata/compat/import_cjs_from_esm/reexports2.js
@@ -0,0 +1,2 @@
+exports.buzz = "buzz";
+exports.fizz = "FIZZ";