Skip to content

Commit

Permalink
fix(cli): fix cts support (#5575)
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist authored Feb 2, 2024
1 parent 31b8b70 commit 9e92b5c
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/rspack-cli/src/utils/isEsmFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const isEsmFile = (filePath: string, cwd = process.cwd()) => {
const ext = path.extname(filePath);
if (/\.(mjs|mts)$/.test(ext)) {
return true;
} else if (/\.(cjs|cts)/.test(ext)) {
return false;
} else {
const packageJson = readPackageUp(cwd);
return packageJson?.type === "module";
Expand Down
1 change: 1 addition & 0 deletions packages/rspack-cli/tests/build/config/cjs_in_esm/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("Main cjs file");
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require("path");

module.exports = {
mode: "production",
entry: path.resolve(__dirname, "main.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: "cjs.bundle.js"
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const path = require("path");

module.exports = {
mode: "production",
entry: path.resolve(__dirname, "main.ts"),
output: {
path: path.resolve(__dirname, "dist"),
filename: "cts.bundle.js"
}
};
27 changes: 27 additions & 0 deletions packages/rspack-cli/tests/build/config/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,34 @@ describe("rspack cli", () => {
expect(stderr).toMatch(/not found/);
});
});
describe("should respect cjs in esm folder", () => {
const cwd = resolve(__dirname, "./cjs_in_esm");
it("should load config.cjs file", async () => {
const { exitCode, stderr, stdout } = await run(cwd, [
"-c",
"rspack.config.cjs"
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(
readFile(resolve(cwd, "./dist/cjs.bundle.js"), { encoding: "utf-8" })
).resolves.toMatch(/Main cjs file/);
});

it("should load config.cts file", async () => {
const { exitCode, stderr, stdout } = await run(cwd, [
"-c",
"rspack.config.cts"
]);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
expect(exitCode).toBe(0);
expect(
readFile(resolve(cwd, "./dist/cts.bundle.js"), { encoding: "utf-8" })
).resolves.toMatch(/Main cjs file/);
});
});
describe("should load cjs config", () => {
const cwd = resolve(__dirname, "./cjs");

Expand Down

1 comment on commit 9e92b5c

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-02-02 abd21bf) Current Change
10000_development-mode + exec 1.72 s ± 37 ms 1.72 s ± 27 ms +0.43 %
10000_development-mode_hmr + exec 947 ms ± 9.1 ms 952 ms ± 12 ms +0.56 %
10000_production-mode + exec 2.63 s ± 30 ms 2.66 s ± 16 ms +1.12 %
arco-pro_development-mode + exec 2.68 s ± 41 ms 2.67 s ± 48 ms -0.26 %
arco-pro_development-mode_hmr + exec 1.1 s ± 21 ms 1.1 s ± 21 ms -0.26 %
arco-pro_production-mode + exec 4.39 s ± 72 ms 4.41 s ± 58 ms +0.58 %
threejs_development-mode_10x + exec 1.99 s ± 25 ms 2.03 s ± 15 ms +1.91 %
threejs_development-mode_10x_hmr + exec 1.23 s ± 6 ms 1.23 s ± 9.9 ms -0.33 %
threejs_production-mode_10x + exec 5.9 s ± 119 ms 5.9 s ± 135 ms -0.13 %

Please sign in to comment.