Skip to content

Commit 945531c

Browse files
fix(optimize): support concatenateModules with cacheUnaffected (#22746)
* fix(optimize): support concatenateModules with cacheUnaffected * chore: shorten changeset * perf: skip the source type override lookup until one is set * test: cover an error from the optimizeChunkModules hook
1 parent 36f36bc commit 945531c

13 files changed

Lines changed: 65 additions & 57 deletions

File tree

‎.changeset/130-concatenation-cache-unaffected-guard.md‎

Lines changed: 0 additions & 5 deletions
This file was deleted.
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+
Support `concatenateModules` with `cacheUnaffected`.

‎lib/Compilation.js‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ const reduceAffectType = (connections) => {
657657
return affected;
658658
};
659659

660-
/** @typedef {{ id: ModuleId, modules?: Map<Module, string>, blocks?: (ChunkId | null)[], sourceTypes?: SourceTypes }} References */
660+
/** @typedef {{ id: ModuleId, modules?: Map<Module, string>, blocks?: (ChunkId | null)[], sourceTypes?: SourceTypes, chunkSourceTypes?: string }} References */
661661
/** @typedef {Map<Module, WeakTupleMap<EXPECTED_ANY[], EXPECTED_ANY>>} ModuleMemCaches */
662662
/** @typedef {{ chunk: Chunk, load: () => Promise<EXPECTED_ANY>, create: (loaded: EXPECTED_ANY) => RuntimeModule, chunkGraph: ChunkGraph }} PendingRuntimeModule */
663663

@@ -3193,6 +3193,17 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
31933193
}
31943194
return key;
31953195
};
3196+
/**
3197+
* Key the source types the chunk graph overrides for the module, which module concatenation narrows for an inner module that keeps other source types.
3198+
* @param {Module} module module
3199+
* @returns {string | undefined} key, undefined when the module's own source types stand
3200+
*/
3201+
const chunkSourceTypesKey = (module) => {
3202+
const sourceTypes = chunkGraph.getModuleSourceTypes(module);
3203+
return sourceTypes === module.getSourceTypes()
3204+
? undefined
3205+
: [...sourceTypes].join();
3206+
};
31963207
/**
31973208
* Compute references.
31983209
* @param {Module} module module
@@ -3235,7 +3246,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
32353246
id,
32363247
modules,
32373248
blocks,
3238-
sourceTypes: module.getReferencedSourceTypes()
3249+
sourceTypes: module.getReferencedSourceTypes(),
3250+
chunkSourceTypes: chunkSourceTypesKey(module)
32393251
};
32403252
};
32413253
/**
@@ -3246,11 +3258,12 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
32463258
* @param {Map<Module, string>=} references.modules modules
32473259
* @param {(string | number | null)[]=} references.blocks blocks
32483260
* @param {SourceTypes=} references.sourceTypes connection-dependent source types
3261+
* @param {string=} references.chunkSourceTypes key of the source types the chunk graph overrides
32493262
* @returns {boolean} ok?
32503263
*/
32513264
const compareReferences = (
32523265
module,
3253-
{ id, modules, blocks, sourceTypes }
3266+
{ id, modules, blocks, sourceTypes, chunkSourceTypes }
32543267
) => {
32553268
if (id !== chunkGraph.getModuleId(module)) return false;
32563269
if (modules !== undefined) {
@@ -3289,6 +3302,7 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
32893302
) {
32903303
return false;
32913304
}
3305+
if (chunkSourceTypesKey(module) !== chunkSourceTypes) return false;
32923306
return true;
32933307
};
32943308

‎lib/graph/ChunkGraph.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,9 @@ class ChunkGraph {
361361

362362
/** @type {HashFunction} */
363363
this._hashFunction = hashFunction;
364+
// lets the per-module override lookup bail out until something sets one
365+
/** @type {boolean} */
366+
this._hasOverwrittenSourceTypes = false;
364367

365368
this._getGraphRoots = this._getGraphRoots.bind(this);
366369
}
@@ -804,6 +807,7 @@ class ChunkGraph {
804807
cgc.sourceTypesByModule = new WeakMap();
805808
}
806809
cgc.sourceTypesByModule.set(module, sourceTypes);
810+
this._hasOverwrittenSourceTypes = true;
807811
// Update cgc._modulesBySourceType to invalidate the cache
808812
cgc._modulesBySourceType = modulesBySourceType(cgc.sourceTypesByModule);
809813
}
@@ -839,6 +843,7 @@ class ChunkGraph {
839843
* @returns {SourceTypes | undefined} source types
840844
*/
841845
_getOverwrittenModuleSourceTypes(module) {
846+
if (!this._hasOverwrittenSourceTypes) return;
842847
let newSet = false;
843848
/** @type {Set<SourceType> | undefined} */
844849
let sourceTypes;

‎lib/optimize/ModuleConcatenationPlugin.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,6 @@ class ModuleConcatenationPlugin {
194194
},
195195
(allChunks, modules, callback) => {
196196
// 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-
}
204197
const logger = compilation.getLogger(
205198
"webpack.ModuleConcatenationPlugin"
206199
);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"use strict";
2+
3+
// seal stops at the failing hook and hands the error to the caller
4+
module.exports = [[/^failed in optimizeChunkModules$/]];
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
it("should not be executed", function () {
2+
throw new Error("should not be executed");
3+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration} */
4+
module.exports = {
5+
plugins: [
6+
{
7+
apply(compiler) {
8+
compiler.hooks.compilation.tap(
9+
"FailOptimizeChunkModules",
10+
(compilation) => {
11+
compilation.hooks.optimizeChunkModules.tapAsync(
12+
"FailOptimizeChunkModules",
13+
(chunks, modules, callback) => {
14+
callback(new Error("failed in optimizeChunkModules"));
15+
}
16+
);
17+
}
18+
);
19+
}
20+
}
21+
]
22+
};

‎test/unitCases/Compiler.unittest.js‎

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -918,31 +918,4 @@ 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-
});
948921
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import png from "./images/file.png";
2+
import svg from "./images/file.svg";
3+
import jpg from "./images/file.jpg";
4+
5+
it("should generate assets correctly", () => {
6+
expect(png).toMatch(/^data:image\/png;base64,/);
7+
expect(svg).toMatch(/^data:image\/svg\+xml;base64,/);
8+
expect(jpg).toMatch(/\.jpg$/);
9+
});

0 commit comments

Comments
 (0)