mirror of
https://github.com/denoland/deno.git
synced 2024-11-21 15:04:11 -05:00
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>
This commit is contained in:
parent
4554ab6aef
commit
dfc5eec43c
15 changed files with 60 additions and 11 deletions
|
@ -327,7 +327,9 @@ impl NpmModuleLoader {
|
||||||
specifier: &ModuleSpecifier,
|
specifier: &ModuleSpecifier,
|
||||||
maybe_referrer: Option<&ModuleSpecifier>,
|
maybe_referrer: Option<&ModuleSpecifier>,
|
||||||
) -> Option<Result<ModuleCodeStringSource, AnyError>> {
|
) -> 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)
|
Some(self.load(specifier, maybe_referrer).await)
|
||||||
} else {
|
} else {
|
||||||
None
|
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
|
// translate cjs to esm if it's cjs and inject node globals
|
||||||
let code = match String::from_utf8_lossy(&code) {
|
let code = match String::from_utf8_lossy(&code) {
|
||||||
Cow::Owned(code) => code,
|
Cow::Owned(code) => code,
|
||||||
|
|
|
@ -1926,11 +1926,6 @@ itest!(es_private_fields {
|
||||||
output: "run/es_private_fields.js.out",
|
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 {
|
itest!(ts_import_from_js {
|
||||||
args: "run --quiet --reload run/ts_import_from_js/main.js",
|
args: "run --quiet --reload run/ts_import_from_js/main.js",
|
||||||
output: "run/ts_import_from_js/main.out",
|
output: "run/ts_import_from_js/main.out",
|
||||||
|
|
6
tests/specs/run/import_common_js/__test__.jsonc
Normal file
6
tests/specs/run/import_common_js/__test__.jsonc
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
{
|
||||||
|
"steps": [
|
||||||
|
{ "args": "run -R index.cjs", "output": "index.out" },
|
||||||
|
{ "args": "run -R main.ts", "output": "main.out" }
|
||||||
|
]
|
||||||
|
}
|
7
tests/specs/run/import_common_js/a.js
Normal file
7
tests/specs/run/import_common_js/a.js
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
function foobar() {
|
||||||
|
console.log("foobar");
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
foobar,
|
||||||
|
};
|
9
tests/specs/run/import_common_js/index.cjs
Normal file
9
tests/specs/run/import_common_js/index.cjs
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
const process = require("process");
|
||||||
|
const a = require("./a");
|
||||||
|
|
||||||
|
console.log(process.cwd());
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
cwd: process.cwd,
|
||||||
|
foobar: a.foobar,
|
||||||
|
};
|
1
tests/specs/run/import_common_js/index.out
Normal file
1
tests/specs/run/import_common_js/index.out
Normal file
|
@ -0,0 +1 @@
|
||||||
|
[WILDCARD]import_common_js
|
5
tests/specs/run/import_common_js/main.out
Normal file
5
tests/specs/run/import_common_js/main.out
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
hello from foo node module
|
||||||
|
[WILDCARD]import_common_js
|
||||||
|
cjsModule.cwd() [WILDCARD]import_common_js
|
||||||
|
foobar
|
||||||
|
cjsModule.foobar() undefined
|
3
tests/specs/run/import_common_js/main.ts
Normal file
3
tests/specs/run/import_common_js/main.ts
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
import foo from "foo";
|
||||||
|
|
||||||
|
foo();
|
14
tests/specs/run/import_common_js/node_modules/foo/index.mjs
generated
vendored
Normal file
14
tests/specs/run/import_common_js/node_modules/foo/index.mjs
generated
vendored
Normal file
|
@ -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());
|
||||||
|
}
|
3
tests/specs/run/import_common_js/node_modules/foo/package.json
generated
vendored
Normal file
3
tests/specs/run/import_common_js/node_modules/foo/package.json
generated
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"main": "./index.mjs"
|
||||||
|
}
|
5
tests/specs/run/import_common_js/package.json
Normal file
5
tests/specs/run/import_common_js/package.json
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
{
|
||||||
|
"dependencies": {
|
||||||
|
"foo": "*"
|
||||||
|
}
|
||||||
|
}
|
1
tests/testdata/run/cjs_imports/commonjs.cjs
vendored
1
tests/testdata/run/cjs_imports/commonjs.cjs
vendored
|
@ -1 +0,0 @@
|
||||||
console.log("Hello World");
|
|
1
tests/testdata/run/cjs_imports/main.out
vendored
1
tests/testdata/run/cjs_imports/main.out
vendored
|
@ -1 +0,0 @@
|
||||||
Hello World
|
|
1
tests/testdata/run/cjs_imports/main.ts
vendored
1
tests/testdata/run/cjs_imports/main.ts
vendored
|
@ -1 +0,0 @@
|
||||||
import "./commonjs.cjs";
|
|
|
@ -221,7 +221,7 @@ async function ensureNoNewITests() {
|
||||||
"pm_tests.rs": 0,
|
"pm_tests.rs": 0,
|
||||||
"publish_tests.rs": 0,
|
"publish_tests.rs": 0,
|
||||||
"repl_tests.rs": 0,
|
"repl_tests.rs": 0,
|
||||||
"run_tests.rs": 349,
|
"run_tests.rs": 348,
|
||||||
"shared_library_tests.rs": 0,
|
"shared_library_tests.rs": 0,
|
||||||
"task_tests.rs": 30,
|
"task_tests.rs": 30,
|
||||||
"test_tests.rs": 75,
|
"test_tests.rs": 75,
|
||||||
|
|
Loading…
Reference in a new issue