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 | |
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>
-rw-r--r-- | cli/resolver.rs | 8 | ||||
-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 | ||||
-rwxr-xr-x | tools/lint.js | 2 |
15 files changed, 60 insertions, 11 deletions
diff --git a/cli/resolver.rs b/cli/resolver.rs index 987e23ee1..5b657b895 100644 --- a/cli/resolver.rs +++ b/cli/resolver.rs @@ -327,7 +327,9 @@ impl NpmModuleLoader { specifier: &ModuleSpecifier, maybe_referrer: Option<&ModuleSpecifier>, ) -> Option<Result<ModuleCodeStringSource, AnyError>> { - if self.node_resolver.in_npm_package(specifier) { + if self.node_resolver.in_npm_package(specifier) + || (specifier.scheme() == "file" && specifier.path().ends_with(".cjs")) + { Some(self.load(specifier, maybe_referrer).await) } else { None @@ -376,7 +378,9 @@ impl NpmModuleLoader { } })?; - let code = if self.cjs_resolutions.contains(specifier) { + let code = if self.cjs_resolutions.contains(specifier) + || (specifier.scheme() == "file" && specifier.path().ends_with(".cjs")) + { // translate cjs to esm if it's cjs and inject node globals let code = match String::from_utf8_lossy(&code) { Cow::Owned(code) => code, 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"; diff --git a/tools/lint.js b/tools/lint.js index 4bffead0c..aa12900ad 100755 --- a/tools/lint.js +++ b/tools/lint.js @@ -221,7 +221,7 @@ async function ensureNoNewITests() { "pm_tests.rs": 0, "publish_tests.rs": 0, "repl_tests.rs": 0, - "run_tests.rs": 349, + "run_tests.rs": 348, "shared_library_tests.rs": 0, "task_tests.rs": 30, "test_tests.rs": 75, |