Skip to content

Commit 0bd77d3

Browse files
feat: add compilation.hooks.chunkHashDependencies (#22810)
* test: cover declared chunk hash dependencies Cases for a chunk naming another chunk's file: a runtime chunk renamed by the compilation hash, a plugin declaring the dependency, the hashing order and the warnings for dependencies that cannot be honored. * feat: add compilation.hooks.chunkHashDependencies A chunk declares the chunks whose hashes its output reads. createHash hashes them first, in a shared order RuntimeTemplate reads too, and JavascriptModulesPlugin mixes their hashes into the chunk's. The ESM and CommonJS chunk formats declare what their startup imports or requires. * chore: add changeset for chunkHashDependencies * chore: regenerate types for chunkHashDependencies * refactor: share one chunk hash order and cover other chunk kinds Positions run across the hashing rounds, so RuntimeTemplate compares them instead of classifying rounds itself; the walk positions chunks as it goes and JavascriptModulesPlugin mixes dependency hashes in one tap. A watch case covers HTML, CSS and worker chunks naming other chunks' files. * style: format chunk hash order loop * chore: restore schema key order * perf: look up chunk hash rounds once per chunk Recomputing the round per dependency edge walks the chunk's groups in canBeInitial; a map filled while sorting chunks into rounds is cheaper. * test: load fs as an ESM external in the chunk kinds case node-commonjs externals import createRequire, which Node 10 lacks. * chore: drop a ChunkGraph cast main's narrower types made redundant Claude-Session: https://claude.ai/code/session_01GVKiUEUTFEzHrHQoE1ryyx * test: import the ParserOptions type the SyntaxTree tests use Same change as #22875; main's lint:types-test fails without it. Claude-Session: https://claude.ai/code/session_01GVKiUEUTFEzHrHQoE1ryyx
1 parent b16184e commit 0bd77d3

48 files changed

Lines changed: 707 additions & 104 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Add `compilation.hooks.chunkHashDependencies` for chunks naming other chunks.

‎lib/Compilation.js‎

Lines changed: 160 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,15 @@ const { toError } = ErrorHelpers;
292292
* @property {ChunkGraph} chunkGraph the chunk graph
293293
*/
294294

295+
/**
296+
* The order `createHash` hashes chunks in, round by round. `createHash` reorders the
297+
* runtime round by references between its chunks, so positions there are only by id.
298+
* @typedef {object} ChunkHashOrder
299+
* @property {Chunk[][]} rounds the chunks of each round, in order
300+
* @property {Map<Chunk, number>} position where each chunk is hashed, across all rounds
301+
* @property {WebpackError[]} warnings dependencies that cannot be hashed first
302+
*/
303+
295304
/**
296305
* Defines the runtime requirements context type used by this module.
297306
* @typedef {object} RuntimeRequirementsContext
@@ -597,6 +606,24 @@ const defineRemovedModuleTemplates = (moduleTemplates) => {
597606

598607
const byId = compareSelect((c) => c.id, compareIds);
599608

609+
// The rounds `createHash` hashes chunks in, so a hash is read only from an earlier one.
610+
const RUNTIME_HASH_ROUND = 2;
611+
612+
/**
613+
* Returns the round `createHash` hashes a chunk in: async, non-entry initial, runtime, entry.
614+
* @param {Chunk} chunk the chunk
615+
* @param {ChunkGraph} chunkGraph the chunk graph
616+
* @returns {number} the round
617+
*/
618+
const getChunkHashRound = (chunk, chunkGraph) => {
619+
if (chunk.hasRuntime()) return RUNTIME_HASH_ROUND;
620+
if (chunkGraph.getNumberOfEntryModules(chunk) > 0) return 3;
621+
return chunk.canBeInitial() ? 1 : 0;
622+
};
623+
624+
/** @type {ReadonlySet<Chunk>} */
625+
const EMPTY_CHUNK_SET = new Set();
626+
600627
const byNameOrHash = concatComparators(
601628
compareSelect((c) => c.name, compareIds),
602629
compareSelect((c) => c.fullHash, compareIds)
@@ -1217,6 +1244,14 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
12171244
fullHash: new SyncHook(["hash"]),
12181245
/** @type {SyncHook<[Chunk, Hash, ChunkHashContext]>} */
12191246
chunkHash: new SyncHook(["chunk", "chunkHash", "ChunkHashContext"]),
1247+
/**
1248+
* Called once per chunk, after chunk ids are assigned, to add the chunks whose
1249+
* hashes its output reads, e.g. because it names their files. `createHash`
1250+
* hashes those first and their hashes go into this chunk's JavaScript hashes.
1251+
* @type {SyncHook<[Chunk, Set<Chunk>]>}
1252+
* @since 5.113.0
1253+
*/
1254+
chunkHashDependencies: new SyncHook(["chunk", "dependencies"]),
12201255

12211256
/** @type {SyncHook<[Module, string]>} */
12221257
moduleAsset: new SyncHook(["module", "filename"]),
@@ -1452,6 +1487,10 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
14521487
this._assetToChunkIndex = undefined;
14531488
/** @type {Map<string, Set<Chunk>> | undefined} */
14541489
this._assetToChunkAuxiliaryIndex = undefined;
1490+
/** @type {Map<Chunk, ReadonlySet<Chunk>> | undefined} */
1491+
this._chunkHashDependencies = undefined;
1492+
/** @type {ChunkHashOrder | undefined} */
1493+
this._chunkHashOrder = undefined;
14551494
/** @type {Error[]} */
14561495
this.errors = [];
14571496
/** @type {Error[]} */
@@ -3755,6 +3794,8 @@ BREAKING CHANGE: Asset processing hooks in Compilation has been merged into a si
37553794
this.assetsInfo.clear();
37563795
this._assetToChunkIndex = undefined;
37573796
this._assetToChunkAuxiliaryIndex = undefined;
3797+
this._chunkHashDependencies = undefined;
3798+
this._chunkHashOrder = undefined;
37583799
this.moduleGraph.removeAllModuleAttributes();
37593800
this.moduleGraph.unfreeze();
37603801
this.moduleMemCaches2 = undefined;
@@ -5359,6 +5400,103 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
53595400
return moduleHashDigest;
53605401
}
53615402

5403+
/**
5404+
* Returns the chunks whose hashes the output of a chunk reads, as declared through
5405+
* `hooks.chunkHashDependencies`; `createHash` hashes them before it.
5406+
* @param {Chunk} chunk the chunk
5407+
* @returns {ReadonlySet<Chunk>} the chunks it depends on
5408+
*/
5409+
getChunkHashDependencies(chunk) {
5410+
if (this._chunkHashDependencies === undefined) {
5411+
this._chunkHashDependencies = new Map();
5412+
}
5413+
let dependencies = this._chunkHashDependencies.get(chunk);
5414+
if (dependencies === undefined) {
5415+
/** @type {Set<Chunk>} */
5416+
const declared = new Set();
5417+
this.hooks.chunkHashDependencies.call(chunk, declared);
5418+
declared.delete(chunk);
5419+
dependencies = declared.size === 0 ? EMPTY_CHUNK_SET : declared;
5420+
this._chunkHashDependencies.set(chunk, dependencies);
5421+
}
5422+
return dependencies;
5423+
}
5424+
5425+
/**
5426+
* Sorts the chunks into the rounds `createHash` hashes them in, ordering each round
5427+
* outside the runtime one by declared hash dependencies and by id otherwise.
5428+
* Memoized until hashing ends, so code generation and `createHash` agree on it.
5429+
* @returns {ChunkHashOrder} the order
5430+
*/
5431+
_getChunkHashOrder() {
5432+
if (this._chunkHashOrder !== undefined) return this._chunkHashOrder;
5433+
const chunkGraph = this.chunkGraph;
5434+
/** @type {Chunk[][]} */
5435+
const rounds = [[], [], [], []];
5436+
/** @type {Map<Chunk, number>} */
5437+
const roundOf = new Map();
5438+
for (const chunk of this.chunks) {
5439+
const round = getChunkHashRound(chunk, chunkGraph);
5440+
rounds[round].push(chunk);
5441+
roundOf.set(chunk, round);
5442+
}
5443+
/** @type {Map<Chunk, number>} */
5444+
const position = new Map();
5445+
/** @type {WebpackError[]} */
5446+
const warnings = [];
5447+
/** @type {Chunk[]} */
5448+
const path = [];
5449+
/**
5450+
* Positions a chunk after the dependencies it has in its round.
5451+
* @param {Chunk} chunk the chunk
5452+
* @param {number} round its round
5453+
* @param {Chunk[]} ordered the round's chunks in order
5454+
* @returns {void}
5455+
*/
5456+
const visit = (chunk, round, ordered) => {
5457+
if (position.has(chunk)) return;
5458+
const index = path.indexOf(chunk);
5459+
if (index !== -1) {
5460+
const cycle = path.slice(index).sort(byId);
5461+
const warning =
5462+
new WebpackError(`Circular hash dependency between chunks (${cycle
5463+
.map((c) => c.name || c.id)
5464+
.join(", ")})
5465+
This prevents using hashes of each other and should be avoided.`);
5466+
warning.chunk = cycle[0];
5467+
warnings.push(warning);
5468+
return;
5469+
}
5470+
path.push(chunk);
5471+
for (const dependency of this.getChunkHashDependencies(chunk)) {
5472+
const dependencyRound = /** @type {number} */ (roundOf.get(dependency));
5473+
if (dependencyRound === round && round !== RUNTIME_HASH_ROUND) {
5474+
visit(dependency, round, ordered);
5475+
} else if (dependencyRound > round) {
5476+
const warning = new WebpackError(
5477+
`Chunk ${chunk.name || chunk.id} cannot read the hash of chunk ${
5478+
dependency.name || dependency.id
5479+
}, which is hashed after it.\nIts hash will not change with that chunk's.`
5480+
);
5481+
warning.chunk = chunk;
5482+
warnings.push(warning);
5483+
}
5484+
}
5485+
path.pop();
5486+
position.set(chunk, position.size);
5487+
ordered.push(chunk);
5488+
};
5489+
for (let round = 0; round < rounds.length; round++) {
5490+
/** @type {Chunk[]} */
5491+
const ordered = [];
5492+
for (const chunk of rounds[round].sort(byId)) {
5493+
visit(chunk, round, ordered);
5494+
}
5495+
rounds[round] = ordered;
5496+
}
5497+
return (this._chunkHashOrder = { rounds, position, warnings });
5498+
}
5499+
53625500
createHash() {
53635501
this.logger.time("hashing: initialize hash");
53645502
const chunkGraph = this.chunkGraph;
@@ -5395,32 +5533,10 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
53955533
}
53965534

53975535
this.logger.time("hashing: sort chunks");
5398-
// Hashed as async, then non-entry initial, then runtime, then entry chunks,
5399-
// so hash dependencies flow one way between those groups. A cycle among the
5400-
// runtime chunks is still possible, and is warned about below.
5401-
/** @type {Chunk[]} */
5402-
const unorderedRuntimeChunks = [];
5403-
/** @type {Chunk[]} */
5404-
const initialChunks = [];
5405-
/** @type {Chunk[]} */
5406-
const entryChunks = [];
5407-
/** @type {Chunk[]} */
5408-
const asyncChunks = [];
5409-
for (const c of this.chunks) {
5410-
if (c.hasRuntime()) {
5411-
unorderedRuntimeChunks.push(c);
5412-
} else if (chunkGraph.getNumberOfEntryModules(c) > 0) {
5413-
entryChunks.push(c);
5414-
} else if (c.canBeInitial()) {
5415-
initialChunks.push(c);
5416-
} else {
5417-
asyncChunks.push(c);
5418-
}
5419-
}
5420-
unorderedRuntimeChunks.sort(byId);
5421-
entryChunks.sort(byId);
5422-
initialChunks.sort(byId);
5423-
asyncChunks.sort(byId);
5536+
const {
5537+
rounds: [asyncChunks, initialChunks, unorderedRuntimeChunks, entryChunks],
5538+
warnings: chunkHashWarnings
5539+
} = this._getChunkHashOrder();
54245540

54255541
/** @typedef {{ chunk: Chunk, referencedBy: RuntimeChunkInfo[], remaining: number }} RuntimeChunkInfo */
54265542
/** @type {Map<Chunk, RuntimeChunkInfo>} */
@@ -5434,11 +5550,15 @@ Or do you want to use the entrypoints '${name}' and '${runtime}' independently o
54345550
}
54355551
let remaining = 0;
54365552
for (const info of runtimeChunksMap.values()) {
5437-
for (const other of new Set(
5553+
const others = new Set(
54385554
[...info.chunk.getAllReferencedAsyncEntrypoints()].map(
54395555
(e) => e.chunks[e.chunks.length - 1]
54405556
)
5441-
)) {
5557+
);
5558+
for (const dependency of this.getChunkHashDependencies(info.chunk)) {
5559+
others.add(dependency);
5560+
}
5561+
for (const other of others) {
54425562
const otherInfo = runtimeChunksMap.get(other);
54435563
// other may be a non-runtime chunk (e.g. worker chunk)
54445564
// when you have a worker chunk in your app.js (new Worker(...)) and as a separate entry point
@@ -5504,26 +5624,7 @@ This prevents using hashes of each other and should be avoided.`);
55045624
this.warnings.push(err);
55055625
for (const i of circularRuntimeChunkInfo) runtimeChunks.push(i.chunk);
55065626
}
5507-
// An ESM entry chunk imports the chunks of the entrypoints it depends on
5508-
// (`dependOn`) by hashed filename, so their entry chunks are hashed first.
5509-
/** @type {Set<Chunk>} */
5510-
const unorderedEntryChunks = new Set(entryChunks);
5511-
entryChunks.length = 0;
5512-
/**
5513-
* Adds an entry chunk after the entry chunks of its parent entrypoints.
5514-
* @param {Chunk} chunk the entry chunk
5515-
* @returns {void}
5516-
*/
5517-
const addEntryChunk = (chunk) => {
5518-
if (!unorderedEntryChunks.delete(chunk)) return;
5519-
for (const group of chunk.groupsIterable) {
5520-
for (const parent of group.parentsIterable) {
5521-
for (const c of parent.chunks) addEntryChunk(c);
5522-
}
5523-
}
5524-
entryChunks.push(chunk);
5525-
};
5526-
for (const chunk of unorderedEntryChunks) addEntryChunk(chunk);
5627+
for (const warning of chunkHashWarnings) this.warnings.push(warning);
55275628
this.logger.timeEnd("hashing: sort chunks");
55285629

55295630
/** @type {Set<Chunk>} */
@@ -5596,6 +5697,14 @@ This prevents using hashes of each other and should be avoided.`);
55965697
hash.update(chunkHashDigest);
55975698
chunk.hash = chunkHashDigest;
55985699
chunk.renderedHash = chunk.hash.slice(0, hashDigestLength);
5700+
// A hash read from a chunk settled after the compilation hash moves then,
5701+
// so this chunk settles after it too, once that hash is final.
5702+
for (const dependency of this.getChunkHashDependencies(chunk)) {
5703+
if (fullHashChunks.has(dependency)) {
5704+
chunkGraph.attachFullHashModules(chunk, []);
5705+
break;
5706+
}
5707+
}
55995708
const fullHashModules =
56005709
chunkGraph.getChunkFullHashModulesIterable(chunk);
56015710
if (fullHashModules) {
@@ -5652,6 +5761,9 @@ This prevents using hashes of each other and should be avoided.`);
56525761
this.hooks.contentHash.call(chunk);
56535762
}
56545763
this.logger.timeEnd("hashing: process full hash modules");
5764+
// Only asked again by a module generated after hashing, which recomputes it.
5765+
this._chunkHashDependencies = undefined;
5766+
this._chunkHashOrder = undefined;
56555767

56565768
if (errors.length > 0) {
56575769
errors.sort(

‎lib/esm/ModuleChunkFormatPlugin.js‎

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -104,30 +104,32 @@ function getChunkNamedImport(index) {
104104
}
105105

106106
/**
107-
* Returns the chunks, other than the runtime chunk, whose files the startup of an entry chunk imports.
107+
* Adds the chunks whose files the startup of a chunk imports, the runtime chunk among them.
108108
* @param {Chunk} chunk the chunk
109109
* @param {ChunkGraph} chunkGraph the chunk graph
110-
* @returns {Iterable<Chunk>} the imported chunks
110+
* @param {Set<Chunk>} imported the set to add the imported chunks to
111+
* @returns {void}
111112
*/
112-
function getImportedChunks(chunk, chunkGraph) {
113+
function addImportedChunks(chunk, chunkGraph, imported) {
113114
if (chunk.hasRuntime()) {
114-
return chunkGraph.getNumberOfEntryModules(chunk) > 0
115-
? chunkGraph.getChunkEntryDependentChunksIterable(chunk)
116-
: [];
115+
if (chunkGraph.getNumberOfEntryModules(chunk) === 0) return;
116+
for (const c of chunkGraph.getChunkEntryDependentChunksIterable(chunk)) {
117+
if (chunkHasJs(c, chunkGraph)) imported.add(c);
118+
}
119+
return;
117120
}
118121
const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
119-
/** @type {Set<Chunk>} */
120-
const chunks = new Set();
122+
if (runtimeChunk === null) return;
123+
imported.add(runtimeChunk);
121124
for (const [, entrypoint] of entries) {
122125
for (const c of getAllChunks(
123126
/** @type {Entrypoint} */ (entrypoint),
124127
chunk,
125128
runtimeChunk
126129
)) {
127-
chunks.add(c);
130+
if (chunkHasJs(c, chunkGraph)) imported.add(c);
128131
}
129132
}
130-
return chunks;
131133
}
132134

133135
const PLUGIN_NAME = "ModuleChunkFormatPlugin";
@@ -370,14 +372,12 @@ class ModuleChunkFormatPlugin {
370372
return source;
371373
});
372374
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}`);
375+
compilation.hooks.chunkHashDependencies.tap(
376+
PLUGIN_NAME,
377+
(chunk, dependencies) => {
378+
addImportedChunks(chunk, compilation.chunkGraph, dependencies);
379379
}
380-
});
380+
);
381381
});
382382
}
383383
}

‎lib/javascript/ChunkHelpers.js‎

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,17 +83,9 @@ function createChunkHashHandler(name) {
8383
*/
8484
return (chunk, hash, { chunkGraph }) => {
8585
if (chunk.hasRuntime()) return;
86-
const { entries, runtimeChunk } = getChunkInfo(chunk, chunkGraph);
86+
const { entries } = getChunkInfo(chunk, chunkGraph);
8787
hash.update(name);
8888
hash.update("1");
89-
if (runtimeChunk && runtimeChunk.hash) {
90-
// WHY: any change to the runtime chunk must move this hash, and its
91-
// internal implementation is not ours to depend on or inspect — the import
92-
// names its hashed filename:
93-
// import __webpack_require__ from "./runtime-main.e9400aee33633a3973bd.js";
94-
// https://github.com/webpack/webpack/issues/19439
95-
hash.update(runtimeChunk.hash);
96-
}
9789

9890
const { updateHashForEntryStartup } = require("./StartupHelpers");
9991

‎lib/javascript/CommonJsChunkFormatPlugin.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ class CommonJsChunkFormatPlugin {
136136
});
137137

138138
hooks.chunkHash.tap(PLUGIN_NAME, createChunkHashHandler(PLUGIN_NAME));
139+
// The startup requires the runtime chunk by its hashed filename.
140+
compilation.hooks.chunkHashDependencies.tap(
141+
PLUGIN_NAME,
142+
(chunk, dependencies) => {
143+
if (chunk.hasRuntime()) return;
144+
const { runtimeChunk } = getChunkInfo(chunk, compilation.chunkGraph);
145+
if (runtimeChunk) dependencies.add(runtimeChunk);
146+
}
147+
);
139148
});
140149
}
141150
}

0 commit comments

Comments
 (0)