From e0a4a0991cdfccdaf18e7eb479f87bc2ee4e2e5f Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Mon, 20 Mar 2023 02:46:47 +0300 Subject: [PATCH] fix: error message for missing default export in configuration (#3685) --- packages/webpack-cli/src/webpack-cli.ts | 5 +++++ test/build/config/named-export/mjs.test.js | 16 ++++++++++++++++ .../build/config/named-export/webpack.config.mjs | 1 + 3 files changed, 22 insertions(+) create mode 100644 test/build/config/named-export/mjs.test.js create mode 100644 test/build/config/named-export/webpack.config.mjs diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 777a811cb8f..c5ab34cd6bb 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -1772,6 +1772,11 @@ class WebpackCLI implements IWebpackCLI { process.exit(2); } + if (!options) { + this.logger.error(`Failed to load '${configPath}' config. Unable to find default export.`); + process.exit(2); + } + if (Array.isArray(options)) { // reassign the value to assert type const optionsArray: ConfigOptions[] = options; diff --git a/test/build/config/named-export/mjs.test.js b/test/build/config/named-export/mjs.test.js new file mode 100644 index 00000000000..ab4336a35fc --- /dev/null +++ b/test/build/config/named-export/mjs.test.js @@ -0,0 +1,16 @@ +const { run } = require("../../../utils/test-utils"); + +describe("webpack cli", () => { + it("should support mjs config format", async () => { + const { exitCode, stderr } = await run(__dirname, ["-c", "webpack.config.mjs"], { + env: { WEBPACK_CLI_FORCE_LOAD_ESM_CONFIG: true }, + }); + + if (/Error: Not supported/.test(stderr)) { + expect(exitCode).toBe(2); + } else { + expect(exitCode).toBe(2); + expect(stderr).toMatch(/Unable to find default export./); + } + }); +}); diff --git a/test/build/config/named-export/webpack.config.mjs b/test/build/config/named-export/webpack.config.mjs new file mode 100644 index 00000000000..ee6a98eac9a --- /dev/null +++ b/test/build/config/named-export/webpack.config.mjs @@ -0,0 +1 @@ +export let config = {};