@@ -46,10 +46,8 @@ function Compilation(compiler) {
4646 this . _modules = { } ;
4747 this . cache = null ;
4848 this . records = null ;
49- this . nextFreeModuleId = 0 ;
50- this . nextFreeChunkId = 0 ;
51- this . nextFreeModuleIndex = 0 ;
52- this . nextFreeModuleIndex2 = 0 ;
49+ this . nextFreeModuleIndex = undefined ;
50+ this . nextFreeModuleIndex2 = undefined ;
5351 this . additionalChunkAssets = [ ] ;
5452 this . assets = { } ;
5553 this . errors = [ ] ;
@@ -492,10 +490,6 @@ Compilation.prototype.unseal = function unseal() {
492490 this . applyPlugins ( "unseal" ) ;
493491 this . chunks . length = 0 ;
494492 this . namedChunks = { } ;
495- this . nextFreeChunkId = this . nextFreeChunkIdBeforeSeal ;
496- this . nextFreeModuleId = this . nextFreeModuleIdBeforeSeal ;
497- this . nextFreeModuleIndex = this . nextFreeModuleIndexBeforeSeal ;
498- this . nextFreeModuleIndex2 = this . nextFreeModuleIndex2BeforeSeal ;
499493 this . additionalChunkAssets . length = 0 ;
500494 this . assets = { } ;
501495 this . modules . forEach ( function ( module ) {
@@ -510,10 +504,8 @@ Compilation.prototype.seal = function seal(callback) {
510504 if ( a . name > b . name ) return 1 ;
511505 return 0 ;
512506 } ) ;
513- this . nextFreeChunkIdBeforeSeal = this . nextFreeChunkId ;
514- this . nextFreeModuleIdBeforeSeal = this . nextFreeModuleId ;
515- this . nextFreeModuleIndexBeforeSeal = this . nextFreeModuleIndex ;
516- this . nextFreeModuleIndex2BeforeSeal = this . nextFreeModuleIndex2 ;
507+ this . nextFreeModuleIndex = 0 ;
508+ this . nextFreeModuleIndex2 = 0 ;
517509 this . preparedChunks . forEach ( function ( preparedChunk ) {
518510 var module = preparedChunk . module ;
519511 var chunk = this . addChunk ( preparedChunk . name , module ) ;
@@ -737,17 +729,59 @@ Compilation.prototype.removeChunkFromDependencies = function removeChunkFromDepe
737729} ;
738730
739731Compilation . prototype . applyModuleIds = function applyModuleIds ( ) {
732+ var unusedIds = [ ] ;
733+ var nextFreeModuleId = 0 ;
734+ if ( this . usedModuleIds ) {
735+ var usedIds = Object . keys ( this . usedModuleIds ) . map ( function ( key ) {
736+ return this . usedModuleIds [ key ] ;
737+ } , this ) . sort ( ) ;
738+ var usedNumberIds = usedIds . filter ( function ( id ) {
739+ return typeof id === "number" ;
740+ } ) ;
741+ nextFreeModuleId = usedNumberIds . reduce ( function ( a , b ) {
742+ return Math . max ( a , b ) ;
743+ } , - 1 ) + 1 ;
744+ for ( var i = 0 ; i < nextFreeModuleId ; i ++ ) {
745+ if ( this . usedModuleIds [ i ] !== i )
746+ unusedIds . push ( i ) ;
747+ }
748+ unusedIds . reverse ( ) ;
749+ }
740750 this . modules . forEach ( function ( module ) {
741751 if ( module . id === null ) {
742- module . id = this . nextFreeModuleId ++ ;
752+ if ( unusedIds . length > 0 )
753+ module . id = unusedIds . pop ( ) ;
754+ else
755+ module . id = nextFreeModuleId ++ ;
743756 }
744757 } , this ) ;
745758} ;
746759
747760Compilation . prototype . applyChunkIds = function applyChunkIds ( ) {
761+ var unusedIds = [ ] ;
762+ var nextFreeChunkId = 0 ;
763+ if ( this . usedChunkIds ) {
764+ var usedIds = Object . keys ( this . usedChunkIds ) . map ( function ( key ) {
765+ return this . usedChunkIds [ key ] ;
766+ } , this ) . sort ( ) ;
767+ var usedNumberIds = usedIds . filter ( function ( id ) {
768+ return typeof id === "number" ;
769+ } ) ;
770+ nextFreeChunkId = usedNumberIds . reduce ( function ( a , b ) {
771+ return Math . max ( a , b ) ;
772+ } , - 1 ) + 1 ;
773+ for ( var i = 0 ; i < nextFreeChunkId ; i ++ ) {
774+ if ( this . usedChunkIds [ i ] !== i )
775+ unusedIds . push ( i ) ;
776+ }
777+ unusedIds . reverse ( ) ;
778+ }
748779 this . chunks . forEach ( function ( chunk ) {
749780 if ( chunk . id === null ) {
750- chunk . id = this . nextFreeChunkId ++ ;
781+ if ( unusedIds . length > 0 )
782+ chunk . id = unusedIds . pop ( ) ;
783+ else
784+ chunk . id = nextFreeChunkId ++ ;
751785 }
752786 if ( ! chunk . ids ) {
753787 chunk . ids = [ chunk . id ] ;
@@ -950,9 +984,14 @@ Compilation.prototype.createChildCompiler = function(name, outputOptions) {
950984} ;
951985
952986Compilation . prototype . checkConstraints = function ( ) {
987+ var usedIds = { } ;
988+ this . modules . forEach ( function ( module ) {
989+ if ( usedIds [ module . id ] )
990+ throw new Error ( "checkConstraints: duplicate module id " + module . id ) ;
991+ } ) ;
953992 this . chunks . forEach ( function ( chunk , idx ) {
954993 if ( this . chunks . indexOf ( chunk ) !== idx )
955- console . log ( "checkConstraints: duplicate chunk in compilation" , chunk . debugId ) ;
994+ throw new Error ( "checkConstraints: duplicate chunk in compilation " + chunk . debugId ) ;
956995 chunk . checkConstraints ( ) ;
957996 } . bind ( this ) ) ;
958997} ;
0 commit comments