Skip to content

Commit 5106684

Browse files
fix: allow require webpack.config.js in ESM format (#4346)
1 parent 3642966 commit 5106684

File tree

11 files changed

+52
-20
lines changed

11 files changed

+52
-20
lines changed

.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ test/**/index.js
1010
test/build/config/error-commonjs/syntax-error.js
1111
test/build/config/error-mjs/syntax-error.mjs
1212
test/build/config/error-array/webpack.config.js
13+
test/build/config-format/esm-require-await/webpack.config.js
1314
test/configtest/with-config-path/syntax-error.config.js

packages/webpack-cli/src/webpack-cli.ts

+1
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,7 @@ class WebpackCLI implements IWebpackCLI {
351351
require("./utils/dynamic-import-loader")();
352352
if (
353353
((error as ImportLoaderError).code === "ERR_REQUIRE_ESM" ||
354+
(error as ImportLoaderError).code === "ERR_REQUIRE_ASYNC_MODULE" ||
354355
process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) &&
355356
pathToFileURL &&
356357
dynamicImportLoader

test/build/config-format/babel-commonjs/babel-esm.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ describe("webpack cli", () => {
55
it("should support mjs config format", async () => {
66
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "webpack.config.babel.js"]);
77

8-
console.log(stderr);
9-
108
expect(exitCode).toBe(0);
119
expect(stderr).toBeFalsy();
1210
expect(stdout).toBeTruthy();

test/build/config-format/babel-esm/babel-esm.test.js

-12
This file was deleted.

test/build/config-format/babel-esm/package.json

-6
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { run } = require("../../../utils/test-utils");
2+
3+
describe("webpack cli", () => {
4+
it("should support mjs config format using `require`", async () => {
5+
const { exitCode, stdout } = await run(__dirname, ["-c", "webpack.config.js"]);
6+
7+
const [major, minor] = process.versions.node.split(".").map(Number);
8+
9+
if ((major >= 22 && minor >= 11) || major >= 23) {
10+
expect(exitCode).toBe(0);
11+
// stderr contains - Support for loading ES Module in require() is an experimental feature and might change at any time
12+
// expect(stderr).toBeFalsy();
13+
expect(stdout).toBeTruthy();
14+
} else {
15+
expect(exitCode).toBe(2);
16+
}
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { fileURLToPath } from "url";
2+
import path from "path";
3+
4+
const mode = await "development";
5+
6+
export default {
7+
mode,
8+
entry: "./main.js",
9+
output: {
10+
path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "dist"),
11+
filename: "foo.bundle.js",
12+
},
13+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const { run } = require("../../../utils/test-utils");
2+
3+
describe("webpack cli", () => {
4+
it("should support mjs config format using `require`", async () => {
5+
const { exitCode, stdout } = await run(__dirname, ["-c", "webpack.config.js"]);
6+
7+
const [major, minor] = process.versions.node.split(".").map(Number);
8+
9+
if ((major >= 22 && minor >= 11) || major >= 23) {
10+
expect(exitCode).toBe(0);
11+
// stderr contains - Support for loading ES Module in require() is an experimental feature and might change at any time
12+
// expect(stderr).toBeFalsy();
13+
expect(stdout).toBeTruthy();
14+
} else {
15+
expect(exitCode).toBe(2);
16+
}
17+
});
18+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("You know who");

0 commit comments

Comments
 (0)