Skip to content

Commit 2001732

Browse files
test(config-format): add typescript-esbuild fixture and test suite
1 parent 50bb830 commit 2001732

7 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Rimuru Tempest");
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { existsSync } from "node:fs";
2+
import { dirname, resolve } from "node:path";
3+
import { fileURLToPath } from "node:url";
4+
import { run } from "../../../utils/test-utils.js";
5+
6+
const __filename = fileURLToPath(import.meta.url);
7+
const __dirname = dirname(__filename);
8+
9+
describe("typescript configuration via esbuild fallback", () => {
10+
it("should load a .ts config with ESM syntax without nodeOptions", async () => {
11+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.ts"]);
12+
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
expect(exitCode).toBe(0);
16+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
17+
});
18+
19+
it("should load a .ts config with CJS syntax without nodeOptions", async () => {
20+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.cjs.ts"]);
21+
22+
expect(stderr).toBeFalsy();
23+
expect(stdout).toBeTruthy();
24+
expect(exitCode).toBe(0);
25+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
26+
});
27+
28+
it("should load a .ts config with CJS syntax (require/module.exports) without nodeOptions", async () => {
29+
const { exitCode, stderr, stdout } = await run(__dirname, [
30+
"-c",
31+
"./webpack.config.cjs-require.ts",
32+
]);
33+
34+
expect(stderr).toBeFalsy();
35+
expect(stdout).toBeTruthy();
36+
expect(exitCode).toBe(0);
37+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
38+
});
39+
40+
it("should load a .ts config exporting a function via module.exports without nodeOptions", async () => {
41+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.cjs-fn.ts"]);
42+
43+
expect(stderr).toBeFalsy();
44+
expect(stdout).toBeTruthy();
45+
expect(exitCode).toBe(0);
46+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
47+
});
48+
49+
it("should load a .mts config without nodeOptions", async () => {
50+
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "./webpack.config.mts"]);
51+
52+
expect(stderr).toBeFalsy();
53+
expect(stdout).toBeTruthy();
54+
expect(exitCode).toBe(0);
55+
expect(existsSync(resolve(__dirname, "dist/foo.bundle.js"))).toBeTruthy();
56+
});
57+
58+
it("should show a clean actionable error when esbuild is not installed", async () => {
59+
const { exitCode, stderr } = await run(__dirname, ["-c", "./webpack.config.ts"], {
60+
env: {
61+
...process.env,
62+
// Force rechoir to fail and esbuild resolution to fail.
63+
NODE_PATH: "/nonexistent_xyz",
64+
WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: "1",
65+
},
66+
});
67+
68+
if (exitCode !== 0) {
69+
expect(stderr).toContain("npm install -D esbuild");
70+
expect(stderr).not.toContain("Cannot require() ES Module");
71+
expect(stderr).not.toContain("Unknown file extension");
72+
expect(stderr).not.toContain("This is caused by either a bug in Node.js");
73+
expect(stderr).not.toContain(" at ");
74+
}
75+
});
76+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const path = require("node:path");
2+
3+
// cspell:ignore elopment
4+
module.exports = () => ({
5+
mode: "development",
6+
entry: "./main.ts",
7+
output: {
8+
path: path.resolve(__dirname, "dist"),
9+
filename: "foo.bundle.js",
10+
},
11+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const path = require("node:path");
2+
3+
// cspell:ignore elopment
4+
const config = {
5+
mode: "development",
6+
entry: "./main.ts",
7+
output: {
8+
path: path.resolve(__dirname, "dist"),
9+
filename: "foo.bundle.js",
10+
},
11+
};
12+
13+
module.exports = config;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require("node:path");
2+
3+
/* eslint-disable no-useless-concat */
4+
5+
// cspell:ignore elopment
6+
const mode: string = "dev" + "elopment";
7+
const config = {
8+
mode,
9+
entry: "./main.ts",
10+
output: {
11+
path: path.resolve("dist"),
12+
filename: "foo.bundle.js",
13+
},
14+
};
15+
16+
module.exports = config;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as path from "node:path";
2+
3+
/* eslint-disable no-useless-concat */
4+
5+
// cspell:ignore elopment
6+
const mode: string = "dev" + "elopment";
7+
const config = {
8+
mode,
9+
entry: "./main.ts",
10+
output: {
11+
path: path.resolve("dist"),
12+
filename: "foo.bundle.js",
13+
},
14+
};
15+
16+
export default config;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import * as path from "node:path";
2+
3+
/* eslint-disable no-useless-concat */
4+
5+
// cspell:ignore elopment
6+
const mode: string = "dev" + "elopment";
7+
const config = {
8+
mode,
9+
entry: "./main.ts",
10+
output: {
11+
path: path.resolve("dist"),
12+
filename: "foo.bundle.js",
13+
},
14+
};
15+
16+
export default config;

0 commit comments

Comments
 (0)