Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow importing .cjs files #25426

Merged
merged 12 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions cli/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 0 additions & 5 deletions tests/integration/run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 6 additions & 0 deletions tests/specs/run/import_common_js/__test__.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"steps": [
{ "args": "run -R index.cjs", "output": "index.out" },
{ "args": "run -R main.ts", "output": "main.out" }
]
}
7 changes: 7 additions & 0 deletions tests/specs/run/import_common_js/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function foobar() {
console.log("foobar");
}

module.exports = {
foobar,
};
9 changes: 9 additions & 0 deletions tests/specs/run/import_common_js/index.cjs
Original file line number Diff line number Diff line change
@@ -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 change: 1 addition & 0 deletions tests/specs/run/import_common_js/index.out
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[WILDCARD]import_common_js
5 changes: 5 additions & 0 deletions tests/specs/run/import_common_js/main.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
hello from foo node module
[WILDCARD]import_common_js
cjsModule.cwd() [WILDCARD]import_common_js
foobar
cjsModule.foobar() undefined
3 changes: 3 additions & 0 deletions tests/specs/run/import_common_js/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from "foo";

foo();
14 changes: 14 additions & 0 deletions tests/specs/run/import_common_js/node_modules/foo/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions tests/specs/run/import_common_js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"foo": "*"
}
}
1 change: 0 additions & 1 deletion tests/testdata/run/cjs_imports/commonjs.cjs

This file was deleted.

1 change: 0 additions & 1 deletion tests/testdata/run/cjs_imports/main.out

This file was deleted.

1 change: 0 additions & 1 deletion tests/testdata/run/cjs_imports/main.ts

This file was deleted.

2 changes: 1 addition & 1 deletion tools/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down