@@ -81,35 +81,22 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
8181 } ;
8282 Object . keys ( records . chunkHashs ) . forEach ( function ( chunkId ) {
8383 chunkId = + chunkId ;
84- var newModules = [ ] ;
85- var removedModules = records . chunkModuleIds [ chunkId ] . slice ( ) ;
8684 var currentChunk = this . chunks . filter ( function ( chunk ) {
8785 return chunk . id === chunkId ;
8886 } ) [ 0 ] ;
8987 if ( currentChunk ) {
90- currentChunk . modules . forEach ( function ( module ) {
91- var idx = removedModules . indexOf ( module . id ) ;
92- if ( idx >= 0 ) {
93- removedModules . splice ( idx , 1 ) ;
94- if ( ! module . hotUpdate ) return ;
95- }
96- newModules . push ( module ) ;
88+ var newModules = currentChunk . modules . filter ( function ( module ) {
89+ return module . hotUpdate ;
9790 } ) ;
98- }
99- newModules = removedModules . concat ( newModules ) ;
100- if ( newModules . length > 0 ) {
101- var source = hotUpdateChunkTemplate . render ( chunkId , newModules , this . hash , this . moduleTemplate , this . dependencyTemplates ) ;
102- var filename = hotUpdateChunkFilename
103- . replace ( Template . REGEXP_HASH , records . hash )
104- . replace ( Template . REGEXP_ID , chunkId ) ;
105- this . assets [ filename ] = source ;
106- hotUpdateMainContent . c . push ( chunkId ) ;
107- if ( currentChunk ) {
91+ if ( newModules . length > 0 ) {
92+ var source = hotUpdateChunkTemplate . render ( chunkId , newModules , this . hash , this . moduleTemplate , this . dependencyTemplates ) ;
93+ var filename = hotUpdateChunkFilename
94+ . replace ( Template . REGEXP_HASH , records . hash )
95+ . replace ( Template . REGEXP_ID , chunkId ) ;
96+ this . assets [ filename ] = source ;
97+ hotUpdateMainContent . c . push ( chunkId ) ;
10898 currentChunk . files . push ( filename ) ;
10999 this . applyPlugins ( "chunk-asset" , currentChunk , filename ) ;
110- } else {
111- this . additionalChunkAssets . push ( filename ) ;
112- this . applyPlugins ( "chunk-asset" , null , filename ) ;
113100 }
114101 }
115102 } , this ) ;
@@ -197,10 +184,7 @@ HotModuleReplacementPlugin.prototype.apply = function(compiler) {
197184var hotInitCode = function ( ) {
198185 function webpackHotUpdateCallback ( chunkId , moreModules ) {
199186 for ( var moduleId in moreModules ) {
200- if ( moreModules [ moduleId ] )
201- hotUpdate [ moduleId ] = moreModules [ moduleId ] ;
202- else if ( ! hotUpdate [ moduleId ] )
203- hotUpdate [ moduleId ] = false ;
187+ hotUpdate [ moduleId ] = moreModules [ moduleId ] ;
204188 }
205189 if ( -- hotWaitingFiles === 0 && hotChunksLoading === 0 ) {
206190 hotUpdateDownloaded ( ) ;
@@ -412,6 +396,9 @@ var hotInitCode = function() {
412396 function hotApply ( callback ) {
413397 callback = callback || function ( err ) { if ( err ) throw err } ;
414398 if ( hotStatus !== "ready" ) throw new Error ( "apply() is only allowed in ready status" ) ;
399+
400+ // at begin all updates modules are outdated
401+ // the "outdated" status can propagate to parents if they don't accept the children
415402 var outdatedDependencies = { } ;
416403 var outdatedModules = [ ] ;
417404 for ( var id in hotUpdate ) {
@@ -451,39 +438,51 @@ var hotInitCode = function() {
451438 }
452439 }
453440
441+ // Store self accepted outdated modules to require them later by the module system
454442 var outdatedSelfAcceptedModules = [ ] ;
455443 for ( var i = 0 ; i < outdatedModules . length ; i ++ ) {
456444 var moduleId = outdatedModules [ i ] ;
457445 if ( installedModules [ moduleId ] && installedModules [ moduleId ] . hot . _selfAccepted )
458446 outdatedSelfAcceptedModules . push ( moduleId ) ;
459447 }
460448
449+ // Now in "dispose" phase
461450 hotSetStatus ( "dispose" ) ;
462- hotCurrentModuleData = { } ;
463- for ( var i = 0 ; i < outdatedModules . length ; i ++ ) {
464- var moduleId = outdatedModules [ i ] ;
465- var data = { } ;
451+ var queue = outdatedModules . slice ( ) ;
452+ while ( queue . length > 0 ) {
453+ var moduleId = queue . pop ( ) ;
466454 var module = installedModules [ moduleId ] ;
467455 if ( ! module ) continue ;
456+
457+ var data = { } ;
458+
459+ // Call dispose handlers
468460 var disposeHandlers = module . hot . _disposeHandlers ;
469461 for ( var j = 0 ; j < disposeHandlers . length ; j ++ ) {
470462 var cb = disposeHandlers [ j ]
471463 cb ( data ) ;
472464 }
473465 hotCurrentModuleData [ moduleId ] = data ;
474- }
475- for ( var i = 0 ; i < outdatedModules . length ; i ++ ) {
476- var moduleId = outdatedModules [ i ] ;
477- var module = installedModules [ moduleId ] ;
478- if ( ! module ) continue ;
466+
467+ // remove module from cache
479468 delete installedModules [ moduleId ] ;
469+
470+ // remove "parents" references from all children
480471 for ( var j = 0 ; j < module . children . length ; j ++ ) {
481472 var child = installedModules [ module . children [ j ] ] ;
482473 if ( ! child ) continue ;
483474 var idx = child . parents . indexOf ( moduleId ) ;
484- if ( idx >= 0 ) child . parents . splice ( idx , 1 ) ;
475+ if ( idx >= 0 ) {
476+ child . parents . splice ( idx , 1 ) ;
477+ if ( child . parents . length === 0 && child . hot && child . hot . _disposeHandlers && child . hot . _disposeHandlers . length > 0 ) {
478+ // Child has dispose handlers and no more references, dispose it too
479+ queue . push ( child . id ) ;
480+ }
481+ }
485482 }
486483 }
484+
485+ // remove outdated dependency from module children
487486 for ( var moduleId in outdatedDependencies ) {
488487 var module = installedModules [ moduleId ] ;
489488 var moduleOutdatedDependencies = outdatedDependencies [ moduleId ] ;
@@ -494,15 +493,14 @@ var hotInitCode = function() {
494493 }
495494 }
496495
496+ // Not in "apply" phase
497497 hotSetStatus ( "apply" ) ;
498498
499499 hotCurrentHash = hotUpdateNewHash ;
500500
501501 // insert new code
502502 for ( var moduleId in hotUpdate ) {
503- var fn = hotUpdate [ moduleId ] ;
504- if ( fn ) modules [ moduleId ] = fn ;
505- else delete modules [ moduleId ] ;
503+ modules [ moduleId ] = hotUpdate [ moduleId ] ;
506504 }
507505
508506 // call accept handlers
@@ -532,7 +530,7 @@ var hotInitCode = function() {
532530 return callback ( error ) ;
533531 }
534532
535- // Load self accepted
533+ // Load self accepted modules
536534 for ( var i = 0 ; i < outdatedSelfAcceptedModules . length ; i ++ ) {
537535 var moduleId = outdatedSelfAcceptedModules [ i ] ;
538536 hotCurrentParent = moduleId ;
0 commit comments