diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2024-09-05 08:59:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-09-05 07:59:12 +0000 |
commit | dfc5eec43c481b1eeaa0ad069aeba8b7559d4440 (patch) | |
tree | 84e3bc63388a90163ccd4008165bd09761be12a7 /tests | |
parent | 4554ab6aefb158f2d0a8a5ae5bb3d748e123fb00 (diff) |
feat: Allow importing .cjs files (#25426)
This commit adds support for executing top-level `.cjs` files,
as well as import `.cjs` files from within npm packages.
This works only for `.cjs` files, the contents of sibling `package.json`
are not consulted for the `"type"` field.
Closes https://github.com/denoland/deno/issues/25384
---------
Signed-off-by: David Sherret <dsherret@users.noreply.github.com>
Co-authored-by: Luca Casonato <hello@lcas.dev>
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/run_tests.rs | 5 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/__test__.jsonc | 6 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/a.js | 7 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/index.cjs | 9 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/index.out | 1 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/main.out | 5 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/main.ts | 3 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/node_modules/foo/index.mjs | 14 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/node_modules/foo/package.json | 3 | ||||
-rw-r--r-- | tests/specs/run/import_common_js/package.json | 5 | ||||
-rw-r--r-- | tests/testdata/run/cjs_imports/commonjs.cjs | 1 | ||||
-rw-r--r-- | tests/testdata/run/cjs_imports/main.out | 1 | ||||
-rw-r--r-- | tests/testdata/run/cjs_imports/main.ts | 1 |
13 files changed, 53 insertions, 8 deletions
diff --git a/tests/integration/run_tests.rs b/tests/integration/run_tests.rs index 117e5709e..16faba438 100644 --- a/tests/integration/run_tests.rs +++ b/tests/integration/run_tests.rs @@ -1926,11 +1926,6 @@ itest!(es_private_fields { output: "run/es_private_fields.js.out", }); -itest!(cjs_imports { - args: "run --quiet --reload run/cjs_imports/main.ts", - output: "run/cjs_imports/main.out", -}); - itest!(ts_import_from_js { args: "run --quiet --reload run/ts_import_from_js/main.js", output: "run/ts_import_from_js/main.out", diff --git a/tests/specs/run/import_common_js/__test__.jsonc b/tests/specs/run/import_common_js/__test__.jsonc new file mode 100644 index 000000000..a09929cdd --- /dev/null +++ b/tests/specs/run/import_common_js/__test__.jsonc @@ -0,0 +1,6 @@ +{ + "steps": [ + { "args": "run -R index.cjs", "output": "index.out" }, + { "args": "run -R main.ts", "output": "main.out" } + ] +} diff --git a/tests/specs/run/import_common_js/a.js b/tests/specs/run/import_common_js/a.js new file mode 100644 index 000000000..c465ab588 --- /dev/null +++ b/tests/specs/run/import_common_js/a.js @@ -0,0 +1,7 @@ +function foobar() { + console.log("foobar"); +} + +module.exports = { + foobar, +}; diff --git a/tests/specs/run/import_common_js/index.cjs b/tests/specs/run/import_common_js/index.cjs new file mode 100644 index 000000000..18caf81e9 --- /dev/null +++ b/tests/specs/run/import_common_js/index.cjs @@ -0,0 +1,9 @@ +const process = require("process"); +const a = require("./a"); + +console.log(process.cwd()); + +module.exports = { + cwd: process.cwd, + foobar: a.foobar, +}; diff --git a/tests/specs/run/import_common_js/index.out b/tests/specs/run/import_common_js/index.out new file mode 100644 index 000000000..3650631b7 --- /dev/null +++ b/tests/specs/run/import_common_js/index.out @@ -0,0 +1 @@ +[WILDCARD]import_common_js diff --git a/tests/specs/run/import_common_js/main.out b/tests/specs/run/import_common_js/main.out new file mode 100644 index 000000000..03301b362 --- /dev/null +++ b/tests/specs/run/import_common_js/main.out @@ -0,0 +1,5 @@ +hello from foo node module +[WILDCARD]import_common_js +cjsModule.cwd() [WILDCARD]import_common_js +foobar +cjsModule.foobar() undefined diff --git a/tests/specs/run/import_common_js/main.ts b/tests/specs/run/import_common_js/main.ts new file mode 100644 index 000000000..65b75b729 --- /dev/null +++ b/tests/specs/run/import_common_js/main.ts @@ -0,0 +1,3 @@ +import foo from "foo"; + +foo(); diff --git a/tests/specs/run/import_common_js/node_modules/foo/index.mjs b/tests/specs/run/import_common_js/node_modules/foo/index.mjs new file mode 100644 index 000000000..cc93554c7 --- /dev/null +++ b/tests/specs/run/import_common_js/node_modules/foo/index.mjs @@ -0,0 +1,14 @@ +import process from "node:process"; +import path from "node:path"; +import url from "node:url"; + +export default async function () { + console.log("hello from foo node module"); + + const cjsFileToImport = path.join(process.cwd(), "index.cjs"); + + const cjsModule = await import(url.pathToFileURL(cjsFileToImport)); + + console.log("cjsModule.cwd()", cjsModule.cwd()); + console.log("cjsModule.foobar()", cjsModule.foobar()); +} diff --git a/tests/specs/run/import_common_js/node_modules/foo/package.json b/tests/specs/run/import_common_js/node_modules/foo/package.json new file mode 100644 index 000000000..ac525b7b8 --- /dev/null +++ b/tests/specs/run/import_common_js/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "./index.mjs" +}
\ No newline at end of file diff --git a/tests/specs/run/import_common_js/package.json b/tests/specs/run/import_common_js/package.json new file mode 100644 index 000000000..03457387a --- /dev/null +++ b/tests/specs/run/import_common_js/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "foo": "*" + } +} diff --git a/tests/testdata/run/cjs_imports/commonjs.cjs b/tests/testdata/run/cjs_imports/commonjs.cjs deleted file mode 100644 index accefceba..000000000 --- a/tests/testdata/run/cjs_imports/commonjs.cjs +++ /dev/null @@ -1 +0,0 @@ -console.log("Hello World"); diff --git a/tests/testdata/run/cjs_imports/main.out b/tests/testdata/run/cjs_imports/main.out deleted file mode 100644 index 557db03de..000000000 --- a/tests/testdata/run/cjs_imports/main.out +++ /dev/null @@ -1 +0,0 @@ -Hello World diff --git a/tests/testdata/run/cjs_imports/main.ts b/tests/testdata/run/cjs_imports/main.ts deleted file mode 100644 index d8b77c22e..000000000 --- a/tests/testdata/run/cjs_imports/main.ts +++ /dev/null @@ -1 +0,0 @@ -import "./commonjs.cjs"; |