diff --git a/packages/webpack-cli/src/webpack-cli.ts b/packages/webpack-cli/src/webpack-cli.ts index ad70058ac2b..b8ce02c8c6e 100644 --- a/packages/webpack-cli/src/webpack-cli.ts +++ b/packages/webpack-cli/src/webpack-cli.ts @@ -2237,6 +2237,10 @@ class WebpackCLI implements IWebpackCLI { } // Output warnings + if (!Object.isExtensible(item)) { + return; + } + if ( options.isWatchingLikeCommand && options.argv && @@ -2263,7 +2267,7 @@ class WebpackCLI implements IWebpackCLI { }; // Setup default cache options - if (isFileSystemCacheOptions(item)) { + if (isFileSystemCacheOptions(item) && Object.isExtensible(item.cache)) { const configPath = config.path.get(item); if (configPath) { @@ -2321,22 +2325,26 @@ class WebpackCLI implements IWebpackCLI { colors = Boolean(this.colors.isColorSupported); } - item.stats.colors = colors; + if (Object.isExtensible(item.stats)) { + item.stats.colors = colors; + } // Apply CLI plugin if (!item.plugins) { item.plugins = []; } - item.plugins.unshift( - new CLIPlugin({ - configPath: config.path.get(item), - helpfulOutput: !options.json, - progress: options.progress, - analyze: options.analyze, - isMultiCompiler: Array.isArray(config.options), - }), - ); + if (Object.isExtensible(item.plugins)) { + item.plugins.unshift( + new CLIPlugin({ + configPath: config.path.get(item), + helpfulOutput: !options.json, + progress: options.progress, + analyze: options.analyze, + isMultiCompiler: Array.isArray(config.options), + }), + ); + } }; if (Array.isArray(config.options)) { diff --git a/test/build/config/object-freeze-partial/index.test.js b/test/build/config/object-freeze-partial/index.test.js new file mode 100644 index 00000000000..ba9db4c32c9 --- /dev/null +++ b/test/build/config/object-freeze-partial/index.test.js @@ -0,0 +1,16 @@ +"use strict"; +const { resolve } = require("path"); +const { run } = require("../../../utils/test-utils"); + +describe("config with partial `Object.freeze({})`", () => { + it("should not throw error", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "-c", + resolve(__dirname, "webpack.config.js"), + ]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); +}); diff --git a/test/build/config/object-freeze-partial/src/index.js b/test/build/config/object-freeze-partial/src/index.js new file mode 100644 index 00000000000..eb334e35b09 --- /dev/null +++ b/test/build/config/object-freeze-partial/src/index.js @@ -0,0 +1 @@ +console.log("Percy Weasley"); diff --git a/test/build/config/object-freeze-partial/webpack.config.js b/test/build/config/object-freeze-partial/webpack.config.js new file mode 100644 index 00000000000..e7e7ef50c10 --- /dev/null +++ b/test/build/config/object-freeze-partial/webpack.config.js @@ -0,0 +1,5 @@ +module.exports = { + cache: Object.freeze({ type: "filesystem" }), + stats: Object.freeze({}), + plugins: Object.freeze([]), +}; diff --git a/test/build/config/object-freeze/index.test.js b/test/build/config/object-freeze/index.test.js new file mode 100644 index 00000000000..05444da928f --- /dev/null +++ b/test/build/config/object-freeze/index.test.js @@ -0,0 +1,16 @@ +"use strict"; +const { resolve } = require("path"); +const { run } = require("../../../utils/test-utils"); + +describe("config with `Object.freeze({})`", () => { + it("should not throw error", async () => { + const { exitCode, stderr, stdout } = await run(__dirname, [ + "-c", + resolve(__dirname, "webpack.config.js"), + ]); + + expect(exitCode).toBe(0); + expect(stderr).toBeFalsy(); + expect(stdout).toBeTruthy(); + }); +}); diff --git a/test/build/config/object-freeze/src/index.js b/test/build/config/object-freeze/src/index.js new file mode 100644 index 00000000000..eb334e35b09 --- /dev/null +++ b/test/build/config/object-freeze/src/index.js @@ -0,0 +1 @@ +console.log("Percy Weasley"); diff --git a/test/build/config/object-freeze/webpack.config.js b/test/build/config/object-freeze/webpack.config.js new file mode 100644 index 00000000000..d02cb649579 --- /dev/null +++ b/test/build/config/object-freeze/webpack.config.js @@ -0,0 +1 @@ +module.exports = Object.freeze({});