@@ -116,7 +116,7 @@ Compilation.prototype.findModule = function(identifier) {
116116
117117Compilation . prototype . buildModule = function ( module , optional , origin , dependencies , thisCallback ) {
118118 var _this = this ;
119- _this . applyPlugins ( "build-module" , module ) ;
119+ _this . applyPlugins1 ( "build-module" , module ) ;
120120 if ( module . building ) return module . building . push ( thisCallback ) ;
121121 var building = module . building = [ thisCallback ] ;
122122
@@ -142,10 +142,10 @@ Compilation.prototype.buildModule = function(module, optional, origin, dependenc
142142 } , this ) ;
143143 module . dependencies . sort ( Dependency . compare ) ;
144144 if ( err ) {
145- _this . applyPlugins ( "failed-module" , module , err ) ;
145+ _this . applyPlugins2 ( "failed-module" , module , err ) ;
146146 return callback ( err ) ;
147147 }
148- _this . applyPlugins ( "succeed-module" , module ) ;
148+ _this . applyPlugins1 ( "succeed-module" , module ) ;
149149 return callback ( ) ;
150150 } ) ;
151151} ;
@@ -509,7 +509,7 @@ Compilation.prototype.finish = function finish() {
509509} ;
510510
511511Compilation . prototype . unseal = function unseal ( ) {
512- this . applyPlugins ( "unseal" ) ;
512+ this . applyPlugins0 ( "unseal" ) ;
513513 this . chunks . length = 0 ;
514514 this . namedChunks = { } ;
515515 this . additionalChunkAssets . length = 0 ;
@@ -533,13 +533,8 @@ Compilation.prototype.seal = function seal(callback) {
533533 chunk . addModule ( module ) ;
534534 module . addChunk ( chunk ) ;
535535 chunk . entryModule = module ;
536- if ( typeof module . index !== "number" ) {
537- module . index = self . nextFreeModuleIndex ++ ;
538- }
536+ self . assignIndex ( module ) ;
539537 self . processDependenciesBlockForChunk ( module , chunk ) ;
540- if ( typeof module . index2 !== "number" ) {
541- module . index2 = self . nextFreeModuleIndex2 ++ ;
542- }
543538 } , self ) ;
544539 self . sortModules ( self . modules ) ;
545540 self . applyPlugins0 ( "optimize" ) ;
@@ -583,7 +578,7 @@ Compilation.prototype.seal = function seal(callback) {
583578 self . applyPlugins1 ( "optimize-chunk-ids" , self . chunks ) ;
584579 self . applyPlugins1 ( "after-optimize-chunk-ids" , self . chunks ) ;
585580
586- self . sortItemswithChunkIds ( ) ;
581+ self . sortItemsWithChunkIds ( ) ;
587582
588583 if ( shouldRecord )
589584 self . applyPlugins2 ( "record-modules" , self . modules , self . records ) ;
@@ -676,47 +671,112 @@ Compilation.prototype.addChunk = function addChunk(name, module, loc) {
676671 return chunk ;
677672} ;
678673
679- Compilation . prototype . processDependenciesBlockForChunk = function processDependenciesBlockForChunk ( block , chunk ) {
680- if ( block . variables ) {
681- block . variables . forEach ( function ( v ) {
682- v . dependencies . forEach ( iteratorDependency , this ) ;
683- } , this ) ;
674+ Compilation . prototype . assignIndex = function assignIndex ( module ) {
675+ var _this = this ;
676+ function assignIndexToModule ( module ) {
677+ // enter module
678+ if ( typeof module . index !== "number" ) {
679+ module . index = _this . nextFreeModuleIndex ++ ;
680+
681+ queue . push ( function ( ) {
682+ // leave module
683+ module . index2 = _this . nextFreeModuleIndex2 ++ ;
684+ } ) ;
685+
686+ // enter it as block
687+ assignIndexToDependencyBlock ( module ) ;
688+ }
684689 }
685- if ( block . dependencies ) {
686- block . dependencies . forEach ( iteratorDependency , this ) ;
690+ function assignIndexToDependency ( dependency ) {
691+ if ( dependency . module ) {
692+ queue . push ( function ( ) {
693+ assignIndexToModule ( dependency . module ) ;
694+ } ) ;
695+ }
687696 }
688- if ( block . blocks ) {
689- block . blocks . forEach ( function ( b ) {
690- var c ;
691- if ( ! b . chunks ) {
692- c = this . addChunk ( b . chunkName , b . module , b . loc ) ;
693- b . chunks = [ c ] ;
694- c . addBlock ( b ) ;
695- } else {
696- c = b . chunks [ 0 ] ;
697- }
698- chunk . addChunk ( c ) ;
699- c . addParent ( chunk ) ;
700- this . processDependenciesBlockForChunk ( b , c ) ;
701- } , this ) ;
697+ function assignIndexToDependencyBlock ( block ) {
698+ var allDependencies = [ ] ;
699+
700+ function iteratorDependency ( d ) {
701+ allDependencies . push ( d ) ;
702+ }
703+ function iteratorBlock ( b ) {
704+ queue . push ( function ( ) {
705+ assignIndexToDependencyBlock ( b ) ;
706+ } ) ;
707+ }
708+
709+ if ( block . variables ) {
710+ block . variables . forEach ( function ( v ) {
711+ v . dependencies . forEach ( iteratorDependency ) ;
712+ } ) ;
713+ }
714+ if ( block . dependencies ) {
715+ block . dependencies . forEach ( iteratorDependency ) ;
716+ }
717+ if ( block . blocks ) {
718+ block . blocks . slice ( ) . reverse ( ) . forEach ( iteratorBlock , this ) ;
719+ }
720+
721+ allDependencies . reverse ( ) ;
722+ allDependencies . forEach ( function ( d ) {
723+ queue . push ( function ( ) {
724+ assignIndexToDependency ( d ) ;
725+ } ) ;
726+ } ) ;
727+ }
728+
729+ var queue = [ function ( ) {
730+ assignIndexToModule ( module ) ;
731+ } ] ;
732+ while ( queue . length ) {
733+ queue . pop ( ) ( ) ;
734+ }
735+ } ;
736+
737+ Compilation . prototype . processDependenciesBlockForChunk = function processDependenciesBlockForChunk ( block , chunk ) {
738+ var queue = [ [ block , chunk ] ] ;
739+ while ( queue . length ) {
740+ var queueItem = queue . pop ( ) ;
741+ block = queueItem [ 0 ] ;
742+ chunk = queueItem [ 1 ] ;
743+ if ( block . variables ) {
744+ block . variables . forEach ( function ( v ) {
745+ v . dependencies . forEach ( iteratorDependency , this ) ;
746+ } , this ) ;
747+ }
748+ if ( block . dependencies ) {
749+ block . dependencies . forEach ( iteratorDependency , this ) ;
750+ }
751+ if ( block . blocks ) {
752+ block . blocks . forEach ( iteratorBlock , this ) ;
753+ }
754+ }
755+
756+ function iteratorBlock ( b ) {
757+ var c ;
758+ if ( ! b . chunks ) {
759+ c = this . addChunk ( b . chunkName , b . module , b . loc ) ;
760+ b . chunks = [ c ] ;
761+ c . addBlock ( b ) ;
762+ } else {
763+ c = b . chunks [ 0 ] ;
764+ }
765+ chunk . addChunk ( c ) ;
766+ c . addParent ( chunk ) ;
767+ queue . push ( [ b , c ] ) ;
702768 }
703769
704770 function iteratorDependency ( d ) {
705771 if ( ! d . module ) {
706772 return ;
707773 }
708- if ( typeof d . module . index !== "number" ) {
709- d . module . index = this . nextFreeModuleIndex ++ ;
710- }
711774 if ( d . weak ) {
712775 return ;
713776 }
714777 if ( chunk . addModule ( d . module ) ) {
715778 d . module . addChunk ( chunk ) ;
716- this . processDependenciesBlockForChunk ( d . module , chunk ) ;
717- }
718- if ( typeof d . module . index2 !== "number" ) {
719- d . module . index2 = this . nextFreeModuleIndex2 ++ ;
779+ queue . push ( [ d . module , chunk ] ) ;
720780 }
721781 }
722782} ;
@@ -842,11 +902,14 @@ Compilation.prototype.sortItemsWithModuleIds = function sortItemsWithModuleIds()
842902 } ) ;
843903} ;
844904
845- Compilation . prototype . sortItemswithChunkIds = function sortItemswithChunkIds ( ) {
905+ Compilation . prototype . sortItemsWithChunkIds = function sortItemsWithChunkIds ( ) {
846906 this . chunks . sort ( byId ) ;
847907 this . modules . forEach ( function ( module ) {
848908 module . sortItems ( ) ;
849909 } ) ;
910+ this . chunks . forEach ( function ( chunk ) {
911+ chunk . sortItems ( ) ;
912+ } ) ;
850913} ;
851914
852915Compilation . prototype . summarizeDependencies = function summarizeDependencies ( ) {
@@ -924,7 +987,7 @@ Compilation.prototype.createHash = function createHash() {
924987 } else {
925988 this . chunkTemplate . updateHashForChunk ( chunkHash ) ;
926989 }
927- this . applyPlugins ( "chunk-hash" , chunk , chunkHash ) ;
990+ this . applyPlugins2 ( "chunk-hash" , chunk , chunkHash ) ;
928991 chunk . hash = chunkHash . digest ( hashDigest ) ;
929992 hash . update ( chunk . hash ) ;
930993 chunk . renderedHash = chunk . hash . substr ( 0 , hashDigestLength ) ;
@@ -949,7 +1012,7 @@ Compilation.prototype.createModuleAssets = function createModuleAssets() {
9491012 var cacheAssetsAndApplyPlugins = function cacheAssetsAndApplyPlugins ( name ) {
9501013 var file = this . getPath ( name ) ;
9511014 this . assets [ file ] = module . assets [ name ] ;
952- this . applyPlugins ( "module-asset" , module , file ) ;
1015+ this . applyPlugins2 ( "module-asset" , module , file ) ;
9531016 }
9541017
9551018 for ( var i = 0 ; i < this . modules . length ; i ++ ) {
@@ -999,7 +1062,7 @@ Compilation.prototype.createChunkAssets = function createChunkAssets() {
9991062 throw new Error ( "Conflict: Multiple assets emit to the same filename '" + file + "'" ) ;
10001063 this . assets [ file ] = source ;
10011064 chunk . files . push ( file ) ;
1002- this . applyPlugins ( "chunk-asset" , chunk , file ) ;
1065+ this . applyPlugins2 ( "chunk-asset" , chunk , file ) ;
10031066 } catch ( err ) {
10041067 this . errors . push ( new ChunkRenderError ( chunk , file || filenameTemplate , err ) ) ;
10051068 }
0 commit comments