Skip to content

Commit a9ac533

Browse files
fix: allow configuration freeze (#4428)
1 parent 897ad5e commit a9ac533

File tree

7 files changed

+59
-11
lines changed

7 files changed

+59
-11
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2237,6 +2237,10 @@ class WebpackCLI implements IWebpackCLI {
22372237
}
22382238

22392239
// Output warnings
2240+
if (!Object.isExtensible(item)) {
2241+
return;
2242+
}
2243+
22402244
if (
22412245
options.isWatchingLikeCommand &&
22422246
options.argv &&
@@ -2263,7 +2267,7 @@ class WebpackCLI implements IWebpackCLI {
22632267
};
22642268

22652269
// Setup default cache options
2266-
if (isFileSystemCacheOptions(item)) {
2270+
if (isFileSystemCacheOptions(item) && Object.isExtensible(item.cache)) {
22672271
const configPath = config.path.get(item);
22682272

22692273
if (configPath) {
@@ -2321,22 +2325,26 @@ class WebpackCLI implements IWebpackCLI {
23212325
colors = Boolean(this.colors.isColorSupported);
23222326
}
23232327

2324-
item.stats.colors = colors;
2328+
if (Object.isExtensible(item.stats)) {
2329+
item.stats.colors = colors;
2330+
}
23252331

23262332
// Apply CLI plugin
23272333
if (!item.plugins) {
23282334
item.plugins = [];
23292335
}
23302336

2331-
item.plugins.unshift(
2332-
new CLIPlugin({
2333-
configPath: config.path.get(item),
2334-
helpfulOutput: !options.json,
2335-
progress: options.progress,
2336-
analyze: options.analyze,
2337-
isMultiCompiler: Array.isArray(config.options),
2338-
}),
2339-
);
2337+
if (Object.isExtensible(item.plugins)) {
2338+
item.plugins.unshift(
2339+
new CLIPlugin({
2340+
configPath: config.path.get(item),
2341+
helpfulOutput: !options.json,
2342+
progress: options.progress,
2343+
analyze: options.analyze,
2344+
isMultiCompiler: Array.isArray(config.options),
2345+
}),
2346+
);
2347+
}
23402348
};
23412349

23422350
if (Array.isArray(config.options)) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const { resolve } = require("path");
3+
const { run } = require("../../../utils/test-utils");
4+
5+
describe("config with partial `Object.freeze({})`", () => {
6+
it("should not throw error", async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, [
8+
"-c",
9+
resolve(__dirname, "webpack.config.js"),
10+
]);
11+
12+
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
});
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Percy Weasley");
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
cache: Object.freeze({ type: "filesystem" }),
3+
stats: Object.freeze({}),
4+
plugins: Object.freeze([]),
5+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"use strict";
2+
const { resolve } = require("path");
3+
const { run } = require("../../../utils/test-utils");
4+
5+
describe("config with `Object.freeze({})`", () => {
6+
it("should not throw error", async () => {
7+
const { exitCode, stderr, stdout } = await run(__dirname, [
8+
"-c",
9+
resolve(__dirname, "webpack.config.js"),
10+
]);
11+
12+
expect(exitCode).toBe(0);
13+
expect(stderr).toBeFalsy();
14+
expect(stdout).toBeTruthy();
15+
});
16+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("Percy Weasley");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = Object.freeze({});

0 commit comments

Comments
 (0)