Skip to content

Commit 29e0d91

Browse files
fix(esm): rehash entry chunks when a chunk they import changes (#22804)
An ESM entry chunk imports split chunks and its dependOn parents' chunks by hashed file name, but its hash ignored their hashes, so the cache served a stale render importing a file no longer emitted. Hash parent entry chunks first so their hashes are final. Fixes #22803
1 parent 20cc9d0 commit 29e0d91

14 files changed

Lines changed: 160 additions & 0 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+
Rehash an ESM entry chunk when a chunk it imports by file name changes.

‎lib/Compilation.js‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5499,6 +5499,26 @@ This prevents using hashes of each other and should be avoided.`);
54995499
this.warnings.push(err);
55005500
for (const i of circularRuntimeChunkInfo) runtimeChunks.push(i.chunk);
55015501
}
5502+
// An ESM entry chunk imports the chunks of the entrypoints it depends on
5503+
// (`dependOn`) by hashed filename, so their entry chunks are hashed first.
5504+
/** @type {Set<Chunk>} */
5505+
const unorderedEntryChunks = new Set(entryChunks);
5506+
entryChunks.length = 0;
5507+
/**
5508+
* Adds an entry chunk after the entry chunks of its parent entrypoints.
5509+
* @param {Chunk} chunk the entry chunk
5510+
* @returns {void}
5511+
*/
5512+
const addEntryChunk = (chunk) => {
5513+
if (!unorderedEntryChunks.delete(chunk)) return;
5514+
for (const group of chunk.groupsIterable) {
5515+
for (const parent of group.parentsIterable) {
5516+
for (const c of parent.chunks) addEntryChunk(c);
5517+
}
5518+
}
5519+
entryChunks.push(chunk);
5520+
};
5521+
for (const chunk of unorderedEntryChunks) addEntryChunk(chunk);
55025522
this.logger.timeEnd("hashing: sort chunks");
55035523

55045524
/** @type {Set<Chunk>} */

‎lib/esm/ModuleChunkFormatPlugin.js‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,33 @@ function getChunkNamedImport(index) {
103103
return `__webpack_chunk_${index}__`;
104104
}
105105

106+
/**
107+
* Returns the chunks, other than the runtime chunk, whose files the startup of an entry chunk imports.
108+
* @param {Chunk} chunk the chunk
109+
* @param {ChunkGraph} chunkGraph the chunk graph
110+
* @returns {Iterable<Chunk>} the imported chunks
111+
*/
112+
function getImportedChunks(chunk, chunkGraph) {
113+
if (chunk.hasRuntime()) {
114+
return chunkGraph.getNumberOfEntryModules(chunk) > 0
115+
? chunkGraph.getChunkEntryDependentChunksIterable(chunk)
116+
: [];
117+
}
118+
const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
119+
/** @type {Set<Chunk>} */
120+
const chunks = new Set();
121+
for (const [, entrypoint] of entries) {
122+
for (const c of getAllChunks(
123+
/** @type {Entrypoint} */ (entrypoint),
124+
chunk,
125+
runtimeChunk
126+
)) {
127+
chunks.add(c);
128+
}
129+
}
130+
return chunks;
131+
}
132+
106133
const PLUGIN_NAME = "ModuleChunkFormatPlugin";
107134

108135
class ModuleChunkFormatPlugin {
@@ -343,6 +370,14 @@ class ModuleChunkFormatPlugin {
343370
return source;
344371
});
345372
hooks.chunkHash.tap(PLUGIN_NAME, createChunkHashHandler(PLUGIN_NAME));
373+
hooks.chunkHash.tap(PLUGIN_NAME, (chunk, hash, { chunkGraph }) => {
374+
// WHY: the startup imports these chunks by their hashed filenames, so a
375+
// cached render keyed by this hash must not outlive one of them;
376+
// `Compilation#createHash` hashes them before this chunk.
377+
for (const c of getImportedChunks(chunk, chunkGraph)) {
378+
if (chunkHasJs(c, chunkGraph)) hash.update(`${c.hash}`);
379+
}
380+
});
346381
});
347382
}
348383
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import "./lib";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { greet } from "./lib";
2+
3+
greet();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import fs from "fs";
2+
import path from "path";
3+
4+
// Output is not cleaned between steps, so a stale import can still resolve on
5+
// disk: check it against the assets of the current compilation instead.
6+
it("should import the current file name of every imported chunk", () => {
7+
const stats = STATS_JSON.children.find((child) => child.name === NAME);
8+
expect(stats.errors).toHaveLength(0);
9+
const assets = new Set(stats.assets.map((asset) => asset.name));
10+
const imports = [];
11+
for (const name of assets) {
12+
if (!name.endsWith(".mjs")) continue;
13+
const source = fs.readFileSync(
14+
path.join(stats.outputPath, name),
15+
"utf8"
16+
);
17+
const importPattern = /from "\.\/([^"]+)"/g;
18+
let match;
19+
while ((match = importPattern.exec(source)) !== null) {
20+
imports.push(match[1]);
21+
expect(assets).toContain(match[1]);
22+
}
23+
}
24+
expect(imports.some((request) => request.startsWith("lib-"))).toBe(true);
25+
expect(imports.some((request) => request.startsWith("parent-"))).toBe(true);
26+
for (const name of assets) {
27+
if (!/^(main|child)-/.test(name)) continue;
28+
const source = fs.readFileSync(path.join(stats.outputPath, name), "utf8");
29+
expect(source).not.toContain("lib-v");
30+
expect(source).not.toContain("parent-v");
31+
}
32+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const greet = () => "lib-v0";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("parent-v0");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const greet = () => "lib-v1";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log("parent-v1");

0 commit comments

Comments
 (0)