@@ -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
598607const 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+
600627const 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 (
0 commit comments