1
0
Fork 0
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:
Bartek Iwańczuk 2024-09-05 08:59:12 +01:00 committed by GitHub
parent 4554ab6aef
commit dfc5eec43c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 60 additions and 11 deletions

View file

@ -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,

View file

@ -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",

View file

@ -0,0 +1,6 @@
{
"steps": [
{ "args": "run -R index.cjs", "output": "index.out" },
{ "args": "run -R main.ts", "output": "main.out" }
]
}

View file

@ -0,0 +1,7 @@
function foobar() {
console.log("foobar");
}
module.exports = {
foobar,
};

View 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,
};

View file

@ -0,0 +1 @@
[WILDCARD]import_common_js

View file

@ -0,0 +1,5 @@
hello from foo node module
[WILDCARD]import_common_js
cjsModule.cwd() [WILDCARD]import_common_js
foobar
cjsModule.foobar() undefined

View file

@ -0,0 +1,3 @@
import foo from "foo";
foo();

View 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());
}

View file

@ -0,0 +1,3 @@
{
"main": "./index.mjs"
}

View file

@ -0,0 +1,5 @@
{
"dependencies": {
"foo": "*"
}
}

View file

@ -1 +0,0 @@
console.log("Hello World");

View file

@ -1 +0,0 @@
Hello World

View file

@ -1 +0,0 @@
import "./commonjs.cjs";

View file

@ -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,