44*/
55"use strict" ;
66
7+ const util = require ( "util" ) ;
78const DependenciesBlock = require ( "./DependenciesBlock" ) ;
89const ModuleReason = require ( "./ModuleReason" ) ;
910const Template = require ( "./Template" ) ;
@@ -36,7 +37,9 @@ class Module extends DependenciesBlock {
3637 this . used = null ;
3738 this . usedExports = null ;
3839 this . providedExports = null ;
39- this . chunks = [ ] ;
40+ this . _chunks = new Set ( ) ;
41+ this . _chunksIsSorted = true ;
42+ this . _chunksDebugIdent = undefined ;
4043 this . warnings = [ ] ;
4144 this . dependenciesWarnings = [ ] ;
4245 this . errors = [ ] ;
@@ -55,7 +58,9 @@ class Module extends DependenciesBlock {
5558 this . used = null ;
5659 this . usedExports = null ;
5760 this . providedExports = null ;
58- this . chunks . length = 0 ;
61+ this . _chunks . clear ( ) ;
62+ this . _chunksDebugIdent = undefined ;
63+ this . _chunksIsSorted = false ;
5964 super . disconnect ( ) ;
6065 }
6166
@@ -65,26 +70,74 @@ class Module extends DependenciesBlock {
6570 this . index = null ;
6671 this . index2 = null ;
6772 this . depth = null ;
68- this . chunks . length = 0 ;
73+ this . _chunks . clear ( ) ;
74+ this . _chunksDebugIdent = undefined ;
75+ this . _chunksIsSorted = false ;
6976 super . unseal ( ) ;
7077 }
7178
7279 addChunk ( chunk ) {
73- let idx = this . chunks . indexOf ( chunk ) ;
74- if ( idx < 0 )
75- this . chunks . push ( chunk ) ;
80+ this . _chunks . add ( chunk ) ;
81+ this . _chunksDebugIdent = undefined ;
82+ this . _chunksIsSorted = false ;
7683 }
7784
7885 removeChunk ( chunk ) {
79- let idx = this . chunks . indexOf ( chunk ) ;
80- if ( idx >= 0 ) {
81- this . chunks . splice ( idx , 1 ) ;
86+ if ( this . _chunks . delete ( chunk ) ) {
87+ this . _chunksDebugIdent = undefined ;
8288 chunk . removeModule ( this ) ;
8389 return true ;
8490 }
8591 return false ;
8692 }
8793
94+ isInChunk ( chunk ) {
95+ return this . _chunks . has ( chunk ) ;
96+ }
97+
98+ getChunkIdsIdent ( ) {
99+ if ( this . _chunksDebugIdent !== undefined ) return this . _chunksDebugIdent ;
100+ this . _ensureChunksSorted ( ) ;
101+ const chunks = this . _chunks ;
102+ const list = [ ] ;
103+ for ( let chunk of chunks ) {
104+ const debugId = chunk . debugId ;
105+
106+ if ( typeof debugId !== "number" ) {
107+ return this . _chunksDebugIdent = null ;
108+ }
109+
110+ list . push ( debugId ) ;
111+ }
112+
113+ return this . _chunksDebugIdent = list . join ( "," ) ;
114+ }
115+
116+ forEachChunk ( fn ) {
117+ this . _chunks . forEach ( fn ) ;
118+ }
119+
120+ mapChunks ( fn ) {
121+ const chunks = this . _chunks ;
122+ const size = chunks . size ;
123+ const array = new Array ( size ) ;
124+ let idx = 0 ;
125+ for ( let chunk of chunks ) {
126+ array [ idx ++ ] = fn ( chunk , idx , chunks ) ;
127+ }
128+ return array ;
129+ }
130+
131+ getNumberOfChunks ( ) {
132+ return this . _chunks . size ;
133+ }
134+
135+ _ensureChunksSorted ( ) {
136+ if ( this . _chunksIsSorted ) return ;
137+ this . _chunks = new Set ( Array . from ( this . _chunks ) . sort ( byId ) ) ;
138+ this . _chunksIsSorted = true ;
139+ }
140+
88141 addReason ( module , dependency ) {
89142 this . reasons . push ( new ModuleReason ( module , dependency ) ) ;
90143 }
@@ -105,7 +158,7 @@ class Module extends DependenciesBlock {
105158 if ( r . chunks ) {
106159 if ( r . chunks . indexOf ( chunk ) >= 0 )
107160 return true ;
108- } else if ( r . module . chunks . indexOf ( chunk ) >= 0 )
161+ } else if ( r . module . _chunks . has ( chunk ) )
109162 return true ;
110163 }
111164 return false ;
@@ -114,9 +167,9 @@ class Module extends DependenciesBlock {
114167 rewriteChunkInReasons ( oldChunk , newChunks ) {
115168 this . reasons . forEach ( r => {
116169 if ( ! r . chunks ) {
117- if ( r . module . chunks . indexOf ( oldChunk ) < 0 )
170+ if ( ! r . module . _chunks . has ( oldChunk ) )
118171 return ;
119- r . chunks = r . module . chunks ;
172+ r . chunks = Array . from ( r . module . _chunks ) ;
120173 }
121174 r . chunks = r . chunks . reduce ( ( arr , c ) => {
122175 addToSet ( arr , c !== oldChunk ? [ c ] : newChunks ) ;
@@ -160,7 +213,7 @@ class Module extends DependenciesBlock {
160213
161214 sortItems ( ) {
162215 super . sortItems ( ) ;
163- this . chunks . sort ( byId ) ;
216+ this . _ensureChunksSorted ( ) ;
164217 this . reasons . sort ( ( a , b ) => byId ( a . module , b . module ) ) ;
165218 }
166219
@@ -178,6 +231,17 @@ Object.defineProperty(Module.prototype, "entry", {
178231 throw new Error ( "Module.entry was removed. Use Chunk.entryModule" ) ;
179232 }
180233} ) ;
234+
235+ Object . defineProperty ( Module . prototype , "chunks" , {
236+ configurable : false ,
237+ get : util . deprecate ( ( ) => {
238+ return Array . from ( this . _chunks ) ;
239+ } , "Module.chunks: Use Module.forEachChunk/mapChunks/getNumberOfChunks/isInChunk/addChunk/removeChunk instead" ) ,
240+ set ( ) {
241+ throw new Error ( "Readonly. Use Module.addChunk/removeChunk to modify chunks." ) ;
242+ }
243+ } ) ;
244+
181245Module . prototype . identifier = null ;
182246Module . prototype . readableIdentifier = null ;
183247Module . prototype . build = null ;
0 commit comments