diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index 48a89175e67..ad70058ac2b 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -55,7 +55,7 @@ import { type stringifyChunked } from "@discoveryjs/json-ext"; import { type Help, type ParseOptions } from "commander"; import { type CLIPlugin as CLIPluginClass } from "./plugins/cli-plugin"; -import * as console from "node:console"; + const fs = require("fs"); const { Readable } = require("stream"); const path = require("path"); @@ -1933,19 +1933,23 @@ class WebpackCLI implements IWebpackCLI { ), ); - config.options = []; - - loadedConfigs.forEach((loadedConfig) => { - if (Array.isArray(loadedConfig.options)) { - for (const item of loadedConfig.options) { - (config.options as WebpackConfiguration[]).push(item); - config.path.set(options, [loadedConfig.path]); + if (loadedConfigs.length === 1) { + config.options = loadedConfigs[0].options; + config.path.set(loadedConfigs[0].options, [loadedConfigs[0].path]); + } else { + config.options = []; + loadedConfigs.forEach((loadedConfig) => { + if (Array.isArray(loadedConfig.options)) { + for (const item of loadedConfig.options) { + (config.options as WebpackConfiguration[]).push(item); + config.path.set(options, [loadedConfig.path]); + } + } else { + (config.options as WebpackConfiguration[]).push(loadedConfig.options); + config.path.set(loadedConfig.options, [loadedConfig.path]); } - } else { - (config.options as WebpackConfiguration[]).push(loadedConfig.options); - config.path.set(loadedConfig.options, [loadedConfig.path]); - } - }); + }); + } } else { // Prioritize popular extensions first to avoid unnecessary fs calls const extensions = new Set([ diff --git a/test/api/CLI.test.js b/test/api/CLI.test.js index bb4b4601505..fb88bd62c90 100644 --- a/test/api/CLI.test.js +++ b/test/api/CLI.test.js @@ -4,9 +4,15 @@ describe("CLI API", () => { let cli; beforeEach(() => { + jest.spyOn(console, "error").mockImplementation(() => {}); + cli = new CLI(); }); + afterAll(() => { + console.error.mockRestore(); + }); + describe("makeCommand", () => { it("should make command", async () => { expect.assertions(1); diff --git a/test/api/get-default-package-manager.test.js b/test/api/get-default-package-manager.test.js index 0e567a30782..0153726cead 100644 --- a/test/api/get-default-package-manager.test.js +++ b/test/api/get-default-package-manager.test.js @@ -101,6 +101,7 @@ describe("getPackageManager", () => { }); it("should throw error if no package manager is found", () => { + cwdSpy.mockReturnValue(noLockPath); syncMock.mockImplementation(() => { throw new Error(); }); diff --git a/test/build/cache/cache.test.js b/test/build/cache/cache.test.js index d7b8eaeb9b5..8ab13394506 100644 --- a/test/build/cache/cache.test.js +++ b/test/build/cache/cache.test.js @@ -31,11 +31,17 @@ describe("cache", () => { it("should work in multi compiler mode", async () => { fs.rmSync( - path.join(__dirname, "../../../node_modules/.cache/webpack/cache-test-first-development"), + path.join( + __dirname, + "../../../node_modules/.cache/webpack/cache-test-first-development__compiler1__", + ), { recursive: true, force: true }, ); fs.rmSync( - path.join(__dirname, "../../../node_modules/.cache/webpack/cache-test-second-development"), + path.join( + __dirname, + "../../../node_modules/.cache/webpack/cache-test-second-development__compiler2__", + ), { recursive: true, force: true }, ); @@ -59,7 +65,10 @@ describe("cache", () => { it("should work in multi compiler mode with the `--config-name` argument", async () => { fs.rmSync( - path.join(__dirname, "../../../node_modules/.cache/webpack/cache-test-third-development"), + path.join( + __dirname, + "../../../node_modules/.cache/webpack/cache-test-third-development__compiler1__", + ), { recursive: true, force: true }, ); diff --git a/test/build/config-name/config-name.test.js b/test/build/config-name/config-name.test.js index 22a3515b595..3014c29cf8e 100644 --- a/test/build/config-name/config-name.test.js +++ b/test/build/config-name/config-name.test.js @@ -120,6 +120,9 @@ describe("--config-name flag", () => { false, ); + console.log(stdout); + console.log(stderr); + expect(exitCode).toBe(0); expect(stderr).toBeFalsy(); expect(stdout).toContain("first");