@@ -94,6 +94,10 @@ var $hmrDownloadManifest$;
9494var $hmrDownloadUpdateHandlers$ ;
9595/** @type {Record<string, InvalidateModuleHandler> } */
9696var $hmrInvalidateModuleHandlers$ ;
97+ /** @type {Record<ModuleId, string> | undefined } */
98+ var $hmrChunkVersions$ ;
99+ /** @type {Record<ModuleId, string> } */
100+ var $hmrLoadedChunkVersions$ ;
97101/** @type {(id: ModuleId) => unknown } */
98102var __webpack_require__ ;
99103/* eslint-enable no-unassigned-vars */
@@ -125,6 +129,12 @@ module.exports = function () {
125129 /** @type {ModuleId[] | undefined } */
126130 var queuedInvalidatedModules ;
127131
132+ /**
133+ * Chunk loads waiting for the update in progress to settle.
134+ * @type {(() => void)[] }
135+ */
136+ var settledWaiters = [ ] ;
137+
128138 $hmrModuleData$ = currentModuleData ;
129139
130140 $interceptModuleExecution$ . push ( function ( options ) {
@@ -139,6 +149,7 @@ module.exports = function () {
139149
140150 $hmrDownloadUpdateHandlers$ = { } ;
141151 $hmrInvalidateModuleHandlers$ = { } ;
152+ $hmrLoadedChunkVersions$ = { } ;
142153
143154 /**
144155 * @param {WebpackRequire } require the original require function
@@ -206,15 +217,21 @@ module.exports = function () {
206217 // half of `.e`. A runtime that loads no chunks has neither to wrap.
207218 if ( require . e ) {
208219 /** @type {WebpackRequire } */ ( fn ) . e = function ( chunkId , fetchPriority ) {
209- return trackBlockingPromise (
210- /** @type {EnsureChunk } */ ( require . e ) ( chunkId , fetchPriority )
220+ return checkChunkVersion (
221+ chunkId ,
222+ trackBlockingPromise (
223+ /** @type {EnsureChunk } */ ( require . e ) ( chunkId , fetchPriority )
224+ )
211225 ) ;
212226 } ;
213227 }
214228 if ( require . ei ) {
215229 /** @type {WebpackRequire } */ ( fn ) . ei = function ( chunkId , importFn ) {
216- return trackBlockingPromise (
217- /** @type {AnalyzableChunkImport } */ ( require . ei ) ( chunkId , importFn )
230+ return checkChunkVersion (
231+ chunkId ,
232+ trackBlockingPromise (
233+ /** @type {AnalyzableChunkImport } */ ( require . ei ) ( chunkId , importFn )
234+ )
218235 ) ;
219236 } ;
220237 }
@@ -346,6 +363,12 @@ module.exports = function () {
346363 for ( var i = 0 ; i < registeredStatusHandlers . length ; i ++ )
347364 results [ i ] = registeredStatusHandlers [ i ] . call ( null , newStatus ) ;
348365
366+ if ( newStatus === "idle" || newStatus === "abort" || newStatus === "fail" ) {
367+ var waiters = settledWaiters ;
368+ settledWaiters = [ ] ;
369+ for ( var j = 0 ; j < waiters . length ; j ++ ) waiters [ j ] ( ) ;
370+ }
371+
349372 return Promise . all ( results ) . then ( function ( ) { } ) ;
350373 }
351374
@@ -397,6 +420,86 @@ module.exports = function () {
397420 ) ;
398421 }
399422
423+ /**
424+ * Whether a loaded chunk came from the build the runtime runs, as far as it can tell.
425+ * @param {ModuleId } chunkId chunk id
426+ * @returns {boolean } true when its modules fit the running code
427+ */
428+ function isCurrentChunkVersion ( chunkId ) {
429+ var loaded = $hmrLoadedChunkVersions$ [ chunkId ] ;
430+ return (
431+ loaded === undefined ||
432+ ! $hmrChunkVersions$ ||
433+ loaded === $hmrChunkVersions$ [ chunkId ]
434+ ) ;
435+ }
436+
437+ /**
438+ * Holds a chunk load until the chunk fits the running code; one already loaded and
439+ * fitting resolves as fast as before.
440+ * @template T
441+ * @param {ModuleId } chunkId chunk id
442+ * @param {Promise<T> } promise the chunk load
443+ * @returns {Promise<T> } the same result, once the chunk fits
444+ */
445+ function checkChunkVersion ( chunkId , promise ) {
446+ if (
447+ ! $hmrChunkVersions$ ||
448+ $hmrChunkVersions$ [ chunkId ] === undefined ||
449+ $hmrLoadedChunkVersions$ [ chunkId ] === $hmrChunkVersions$ [ chunkId ]
450+ ) {
451+ return promise ;
452+ }
453+ return promise . then ( function ( result ) {
454+ if ( isCurrentChunkVersion ( chunkId ) ) return result ;
455+ return waitForChunkVersion ( chunkId ) . then ( function ( ) {
456+ return result ;
457+ } ) ;
458+ } ) ;
459+ }
460+
461+ /**
462+ * Holds a loaded chunk from another build until the updates leading to it are applied,
463+ * since its modules may need chunks or modules the running build doesn't have.
464+ * @param {ModuleId } chunkId chunk id
465+ * @returns {Promise<void> } resolves when the chunk fits the running code
466+ */
467+ function waitForChunkVersion ( chunkId ) {
468+ if ( isCurrentChunkVersion ( chunkId ) ) return Promise . resolve ( ) ;
469+ if ( currentStatus === "abort" || currentStatus === "fail" ) {
470+ return Promise . reject (
471+ new Error (
472+ "Loaded chunk " +
473+ chunkId +
474+ " is from another build, and the update failed (" +
475+ currentStatus +
476+ ")"
477+ )
478+ ) ;
479+ }
480+ if ( currentStatus !== "idle" ) {
481+ return /** @type {Promise<void> } */ (
482+ new Promise ( function ( resolve ) {
483+ settledWaiters . push ( function ( ) {
484+ resolve ( ) ;
485+ } ) ;
486+ } )
487+ ) . then ( function ( ) {
488+ return waitForChunkVersion ( chunkId ) ;
489+ } ) ;
490+ }
491+ return hotCheck ( true ) . then ( function ( updatedModules ) {
492+ if ( ! updatedModules && ! isCurrentChunkVersion ( chunkId ) ) {
493+ throw new Error (
494+ "Loaded chunk " +
495+ chunkId +
496+ " is from another build, and no update leads to it"
497+ ) ;
498+ }
499+ return waitForChunkVersion ( chunkId ) ;
500+ } ) ;
501+ }
502+
400503 /**
401504 * @param {boolean | ApplyOptions= } applyOnUpdate apply the update right away
402505 * @returns {Promise<ModuleId[] | null> } updated module ids or null
0 commit comments