@@ -30,7 +30,7 @@ class Chunk {
3030 this . name = name ;
3131 this . _modules = new SortableSet ( undefined , sortByIdentifier ) ;
3232 this . entrypoints = [ ] ;
33- this . chunks = [ ] ;
33+ this . _chunks = new SortableSet ( undefined , sortById ) ;
3434 this . parents = [ ] ;
3535 this . blocks = [ ] ;
3636 this . origins = [ ] ;
@@ -61,6 +61,21 @@ class Chunk {
6161 throw new Error ( "Chunk.initial was removed. Use isInitial()" ) ;
6262 }
6363
64+ /**
65+ * @return {Array } - an array containing the chunks
66+ */
67+ getChunks ( ) {
68+ return Array . from ( this . _chunks ) ;
69+ }
70+
71+ getNumberOfChunks ( ) {
72+ return this . _chunks . size ;
73+ }
74+
75+ get chunksIterable ( ) {
76+ return this . _chunks ;
77+ }
78+
6479 hasRuntime ( ) {
6580 if ( this . entrypoints . length === 0 ) return false ;
6681 return this . entrypoints [ 0 ] . getRuntimeChunk ( ) === this ;
@@ -88,7 +103,11 @@ class Chunk {
88103 }
89104
90105 addChunk ( chunk ) {
91- return this . addToCollection ( this . chunks , chunk ) ;
106+ if ( this . _chunks . has ( chunk ) ) {
107+ return false ;
108+ }
109+ this . _chunks . add ( chunk ) ;
110+ return true ;
92111 }
93112
94113 addParent ( parentChunk ) {
@@ -116,13 +135,13 @@ class Chunk {
116135 }
117136
118137 removeChunk ( chunk ) {
119- const idx = this . chunks . indexOf ( chunk ) ;
120- if ( idx >= 0 ) {
121- this . chunks . splice ( idx , 1 ) ;
122- chunk . removeParent ( this ) ;
123- return true ;
138+ if ( ! this . _chunks . has ( chunk ) ) {
139+ return false ;
124140 }
125- return false ;
141+
142+ this . _chunks . delete ( chunk ) ;
143+ chunk . removeParent ( this ) ;
144+ return true ;
126145 }
127146
128147 removeParent ( chunk ) {
@@ -208,13 +227,10 @@ class Chunk {
208227 // cleanup parents
209228 this . parents . forEach ( parentChunk => {
210229 // remove this chunk from its parents
211- const idx = parentChunk . chunks . indexOf ( this ) ;
212- if ( idx >= 0 ) {
213- parentChunk . chunks . splice ( idx , 1 ) ;
214- }
230+ parentChunk . _chunks . delete ( this ) ;
215231
216232 // cleanup "sub chunks"
217- this . chunks . forEach ( chunk => {
233+ this . _chunks . forEach ( chunk => {
218234 /**
219235 * remove this chunk as "intermediary" and connect
220236 * it "sub chunks" and parents directly
@@ -232,7 +248,7 @@ class Chunk {
232248 * This can not be done in the above loop
233249 * as it is not garuanteed that `this.parents` contains anything.
234250 */
235- this . chunks . forEach ( chunk => {
251+ this . _chunks . forEach ( chunk => {
236252 // remove this as parent of every "sub chunk"
237253 const idx = chunk . parents . indexOf ( this ) ;
238254 if ( idx >= 0 ) {
@@ -261,10 +277,7 @@ class Chunk {
261277 }
262278
263279 replaceChunk ( oldChunk , newChunk ) {
264- const idx = this . chunks . indexOf ( oldChunk ) ;
265- if ( idx >= 0 ) {
266- this . chunks . splice ( idx , 1 ) ;
267- }
280+ this . _chunks . delete ( oldChunk ) ;
268281 if ( this !== newChunk && newChunk . addParent ( this ) ) {
269282 this . addChunk ( newChunk ) ;
270283 }
@@ -293,8 +306,8 @@ class Chunk {
293306 otherChunk . parents . forEach ( parentChunk => parentChunk . replaceChunk ( otherChunk , this ) ) ;
294307 otherChunk . parents . length = 0 ;
295308
296- otherChunk . chunks . forEach ( chunk => chunk . replaceParentChunk ( otherChunk , this ) ) ;
297- otherChunk . chunks . length = 0 ;
309+ otherChunk . _chunks . forEach ( chunk => chunk . replaceParentChunk ( otherChunk , this ) ) ;
310+ otherChunk . _chunks . clear ( ) ;
298311
299312 otherChunk . blocks . forEach ( b => {
300313 b . chunks = b . chunks ? b . chunks . map ( c => {
@@ -315,9 +328,8 @@ class Chunk {
315328 origin . reasons . unshift ( reason ) ;
316329 }
317330 } ) ;
318- this . chunks = this . chunks . filter ( chunk => {
319- return chunk !== otherChunk && chunk !== this ;
320- } ) ;
331+ this . _chunks . delete ( otherChunk ) ;
332+ this . _chunks . delete ( this ) ;
321333 this . parents = this . parents . filter ( parentChunk => {
322334 return parentChunk !== otherChunk && parentChunk !== this ;
323335 } ) ;
@@ -329,12 +341,12 @@ class Chunk {
329341 newChunk . blocks . push ( block ) ;
330342 block . chunks . push ( newChunk ) ;
331343 } ) ;
332- this . chunks . forEach ( chunk => {
333- newChunk . chunks . push ( chunk ) ;
344+ this . _chunks . forEach ( chunk => {
345+ newChunk . addChunk ( chunk ) ;
334346 chunk . parents . push ( newChunk ) ;
335347 } ) ;
336348 this . parents . forEach ( parentChunk => {
337- parentChunk . chunks . push ( newChunk ) ;
349+ parentChunk . addChunk ( newChunk ) ;
338350 newChunk . parents . push ( parentChunk ) ;
339351 } ) ;
340352 this . entrypoints . forEach ( entrypoint => {
@@ -413,7 +425,7 @@ class Chunk {
413425 if ( chunk . name )
414426 chunkNameMap [ chunk . id ] = chunk . name ;
415427 }
416- chunk . chunks . forEach ( addChunk ) ;
428+ chunk . _chunks . forEach ( addChunk ) ;
417429 } ( this ) ) ;
418430 return {
419431 hash : chunkHashMap ,
@@ -439,7 +451,7 @@ class Chunk {
439451 origin . reasons . sort ( ) ;
440452 } ) ;
441453 this . parents . sort ( sortById ) ;
442- this . chunks . sort ( sortById ) ;
454+ this . _chunks . sort ( ) ;
443455 }
444456
445457 toString ( ) {
@@ -448,16 +460,19 @@ class Chunk {
448460
449461 checkConstraints ( ) {
450462 const chunk = this ;
451- chunk . chunks . forEach ( ( child , idx ) => {
452- if ( chunk . chunks . indexOf ( child ) !== idx )
463+ const chunks = Array . from ( chunk . _chunks ) ;
464+ chunks . forEach ( ( child , idx ) => {
465+ if ( chunks . indexOf ( child ) !== idx )
453466 throw new Error ( `checkConstraints: duplicate child in chunk ${ chunk . debugId } ${ child . debugId } ` ) ;
454467 if ( child . parents . indexOf ( chunk ) < 0 )
455468 throw new Error ( `checkConstraints: child missing parent ${ chunk . debugId } -> ${ child . debugId } ` ) ;
456469 } ) ;
457470 chunk . parents . forEach ( ( parentChunk , idx ) => {
458471 if ( chunk . parents . indexOf ( parentChunk ) !== idx )
459472 throw new Error ( `checkConstraints: duplicate parent in chunk ${ chunk . debugId } ${ parentChunk . debugId } ` ) ;
460- if ( parentChunk . chunks . indexOf ( chunk ) < 0 )
473+
474+ const chunks = Array . from ( parentChunk . _chunks ) ;
475+ if ( chunks . indexOf ( chunk ) < 0 )
461476 throw new Error ( `checkConstraints: parent missing child ${ parentChunk . debugId } <- ${ chunk . debugId } ` ) ;
462477 } ) ;
463478 }
@@ -466,11 +481,21 @@ class Chunk {
466481Object . defineProperty ( Chunk . prototype , "modules" , {
467482 configurable : false ,
468483 get : util . deprecate ( function ( ) {
469- return Array . from ( this . _modules ) ;
484+ return this . _modules . getFrozenArray ( ) ;
470485 } , "Chunk.modules is deprecated. Use Chunk.getNumberOfModules/mapModules/forEachModule/containsModule instead." ) ,
471486 set : util . deprecate ( function ( value ) {
472487 this . setModules ( value ) ;
473488 } , "Chunk.modules is deprecated. Use Chunk.addModule/removeModule instead." )
474489} ) ;
475490
491+ Object . defineProperty ( Chunk . prototype , "chunks" , {
492+ configurable : false ,
493+ get : util . deprecate ( function ( ) {
494+ return this . _chunks . getFrozenArray ( ) ;
495+ } , "Chunk.chunks: Use Chunk.getChunks() instead" ) ,
496+ set ( ) {
497+ throw new Error ( "Readonly. Use Chunk.addChunk/removeChunk/getChunks to access/modify chunks." ) ;
498+ }
499+ } ) ;
500+
476501module . exports = Chunk ;
0 commit comments