Skip to content

Commit 36f36bc

Browse files
fix(optimize): reject concatenateModules with cacheUnaffected again (#22744)
1 parent 3d774f2 commit 36f36bc

6 files changed

Lines changed: 47 additions & 5 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Reject `concatenateModules` with `cacheUnaffected` instead of a broken bundle.

‎lib/optimize/ModuleConcatenationPlugin.js‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,6 @@ class ModuleConcatenationPlugin {
114114
apply(compiler) {
115115
const { _backCompat: backCompat } = compiler;
116116
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => {
117-
if (compilation.moduleMemCaches) {
118-
throw new Error(
119-
"optimization.concatenateModules can't be used with cacheUnaffected as module concatenation is a global effect"
120-
);
121-
}
122117
const moduleGraph = compilation.moduleGraph;
123118
/** @type {Map<Module, string | ((requestShortener: RequestShortener) => string)>} */
124119
const bailoutReasonMap = new Map();
@@ -198,6 +193,14 @@ class ModuleConcatenationPlugin {
198193
stage: STAGE_DEFAULT
199194
},
200195
(allChunks, modules, callback) => {
196+
// set once the modules are built, so not yet when the compilation starts
197+
if (compilation.moduleMemCaches) {
198+
return callback(
199+
new Error(
200+
"optimization.concatenateModules can't be used with cacheUnaffected as module concatenation is a global effect"
201+
)
202+
);
203+
}
201204
const logger = compilation.getLogger(
202205
"webpack.ModuleConcatenationPlugin"
203206
);

‎test/unitCases/Compiler.unittest.js‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,4 +918,31 @@ describe("Compiler", () => {
918918
})
919919
]);
920920
});
921+
922+
it("should refuse optimization.concatenateModules with cacheUnaffected", (done) => {
923+
const webpack = require("../..");
924+
925+
compiler = webpack({
926+
context: path.join(testDirectory, "fixtures"),
927+
mode: "development",
928+
entry: "./c",
929+
cache: { type: "memory" },
930+
experiments: { cacheUnaffected: true },
931+
optimization: { concatenateModules: true },
932+
output: {
933+
path: "/directory",
934+
filename: "bundle.js"
935+
}
936+
});
937+
compiler.outputFileSystem = /** @type {import("../../").OutputFileSystem} */ (
938+
/** @type {unknown} */ (createFsFromVolume(new Volume()))
939+
);
940+
compiler.run((err) => {
941+
expect(err).toBeInstanceOf(Error);
942+
expect(/** @type {Error} */ (err).message).toBe(
943+
"optimization.concatenateModules can't be used with cacheUnaffected as module concatenation is a global effect"
944+
);
945+
done();
946+
});
947+
});
921948
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
22

3+
// The case needs optimization.concatenateModules, which cacheUnaffected refuses.
34
module.exports = (config) =>
45
!(config.experiments && config.experiments.cacheUnaffected);

test/watchCases/cache/add-defines/test.filter.js renamed to test/watchCases/plugins/dll-reference-plugin/test.filter.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"use strict";
22

3+
// The case needs optimization.concatenateModules, which cacheUnaffected refuses.
34
module.exports = (config) =>
45
!(config.experiments && config.experiments.cacheUnaffected);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"use strict";
2+
3+
// The case needs optimization.concatenateModules, which cacheUnaffected refuses.
4+
module.exports = (config) =>
5+
!(config.experiments && config.experiments.cacheUnaffected);

0 commit comments

Comments
 (0)