Skip to content

Commit

Permalink
fix: allow require webpack.config.js in ESM format (#4346)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 13, 2024
1 parent 3642966 commit 5106684
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ test/**/index.js
test/build/config/error-commonjs/syntax-error.js
test/build/config/error-mjs/syntax-error.mjs
test/build/config/error-array/webpack.config.js
test/build/config-format/esm-require-await/webpack.config.js
test/configtest/with-config-path/syntax-error.config.js
1 change: 1 addition & 0 deletions packages/webpack-cli/src/webpack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ class WebpackCLI implements IWebpackCLI {
require("./utils/dynamic-import-loader")();
if (
((error as ImportLoaderError).code === "ERR_REQUIRE_ESM" ||
(error as ImportLoaderError).code === "ERR_REQUIRE_ASYNC_MODULE" ||
process.env.WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG) &&
pathToFileURL &&
dynamicImportLoader
Expand Down
2 changes: 0 additions & 2 deletions test/build/config-format/babel-commonjs/babel-esm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ describe("webpack cli", () => {
it("should support mjs config format", async () => {
const { exitCode, stderr, stdout } = await run(__dirname, ["-c", "webpack.config.babel.js"]);

console.log(stderr);

expect(exitCode).toBe(0);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
Expand Down
12 changes: 0 additions & 12 deletions test/build/config-format/babel-esm/babel-esm.test.js

This file was deleted.

6 changes: 0 additions & 6 deletions test/build/config-format/babel-esm/package.json

This file was deleted.

18 changes: 18 additions & 0 deletions test/build/config-format/esm-require-await/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { run } = require("../../../utils/test-utils");

describe("webpack cli", () => {
it("should support mjs config format using `require`", async () => {
const { exitCode, stdout } = await run(__dirname, ["-c", "webpack.config.js"]);

const [major, minor] = process.versions.node.split(".").map(Number);

if ((major >= 22 && minor >= 11) || major >= 23) {
expect(exitCode).toBe(0);
// stderr contains - Support for loading ES Module in require() is an experimental feature and might change at any time
// expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
} else {
expect(exitCode).toBe(2);
}
});
});
File renamed without changes.
13 changes: 13 additions & 0 deletions test/build/config-format/esm-require-await/webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { fileURLToPath } from "url";
import path from "path";

const mode = await "development";

export default {
mode,
entry: "./main.js",
output: {
path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "dist"),
filename: "foo.bundle.js",
},
};
18 changes: 18 additions & 0 deletions test/build/config-format/esm-require/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { run } = require("../../../utils/test-utils");

describe("webpack cli", () => {
it("should support mjs config format using `require`", async () => {
const { exitCode, stdout } = await run(__dirname, ["-c", "webpack.config.js"]);

const [major, minor] = process.versions.node.split(".").map(Number);

if ((major >= 22 && minor >= 11) || major >= 23) {
expect(exitCode).toBe(0);
// stderr contains - Support for loading ES Module in require() is an experimental feature and might change at any time
// expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();
} else {
expect(exitCode).toBe(2);
}
});
});
1 change: 1 addition & 0 deletions test/build/config-format/esm-require/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log("You know who");

0 comments on commit 5106684

Please sign in to comment.