@@ -60,7 +60,8 @@ The available options are:
6060 * that webpack will take care of loading this file.
6161 */
6262 if ( options . async && options . filename ) {
63- throw new Error ( "If you set the \"async\" option webpack takes care of loading the chunk. Therefore you can not specify a filename." ) ;
63+ throw new Error ( `You can not specify a filename if you use the \"async\" option.
64+ You can however specify the name of the async chunk by passing the desired string as the \"async\" option.` ) ;
6465 }
6566
6667 const chunkNames = options . name ? [ options . name ] : options . names ;
@@ -89,78 +90,83 @@ The available options are:
8990 compilation [ ident ] = true ;
9091
9192 /**
92- * Creates a list of common chunks based on the options.
93+ * Creates a list of " common"" chunks based on the options.
9394 * The list is made up of preexisting or newly created chunks.
9495 * - If chunk has the name as specified in the chunkNames it is put in the list
9596 * - If no chunk with the name as given in chunkNames exists a new chunk is created and added to the list
97+ *
98+ * These chunks are the "targets" for extracted modules.
9699 */
97- const commonChunks = this . getCommonChunks ( chunks , compilation , this . chunkNames , this . children , this . async ) ;
100+ const targetChunks = this . getTargetChunks ( chunks , compilation , this . chunkNames , this . children , this . async ) ;
98101
99102 // iterate over all our new chunks
100- commonChunks . forEach ( ( commonChunk , idx ) => {
101- // get chunks that are actually used
102- // TODO: clarify what that means
103- const usedChunks = this . getUsedChunks ( compilation , chunks , commonChunk , commonChunks , idx , this . selectedChunks , this . async , this . children ) ;
103+ targetChunks . forEach ( ( targetChunk , idx ) => {
104104
105- // bail as this is an erronous state
106- if ( ! usedChunks ) {
105+ /**
106+ * These chunks are subject to get "common" modules extracted and moved to the common chunk
107+ */
108+ const affectedChunks = this . getAffectedChunks ( compilation , chunks , targetChunk , targetChunks , idx , this . selectedChunks , this . async , this . children ) ;
109+
110+ // bail if no chunk is affected
111+ if ( ! affectedChunks ) {
107112 return ;
108113 }
109114
110115 // If we are async create an async chunk now
111116 // override the "commonChunk" with the newly created async one and use it as commonChunk from now on
112117 let asyncChunk ;
113118 if ( asyncOption ) {
114- asyncChunk = this . createAsyncChunk ( compilation , this . async , commonChunk ) ;
115- commonChunk . addChunk ( asyncChunk ) ;
116- commonChunk = asyncChunk ;
119+ asyncChunk = this . createAsyncChunk ( compilation , this . async , targetChunk ) ;
120+ targetChunk . addChunk ( asyncChunk ) ;
121+ targetChunk = asyncChunk ;
117122 }
118123
119- // get all modules that suffice the filter e.g. are used at least in "n" chunks
120- // TODO: what does "really used modules mean here"?
121- const reallyUsedModules = this . getReallyUsedModules ( minChunks , usedChunks , commonChunk ) ;
124+ /**
125+ * Check which modules are "common" and could be extracted to a "common" chunk
126+ */
127+ const extractableModules = this . getExtractableModules ( minChunks , affectedChunks , targetChunk ) ;
122128
123129 // If the minSize option is set check if the size extracted from the chunk is reached
124130 // else bail out here.
125131 // As all modules/commons are interlinked with each other, common modules would be extracted
126132 // if we reach this mark at a later common chunk. (quirky I guess).
127133 if ( minSize ) {
128- const modulesSize = this . getModulesSize ( reallyUsedModules ) ;
134+ const modulesSize = this . calculateModuleSize ( extractableModules ) ;
129135 // if too small, bail
130136 if ( modulesSize < minSize )
131137 return ;
132138 }
133139
134140 // Remove modules that are moved to commons chunk from their original chunks
135141 // return all chunks that are affected by having modules removed - we need them later (apparently)
136- const reallyUsedChunks = this . removeModulesFromUsedChunksAndReturnUsedChunks ( reallyUsedModules , usedChunks ) ;
142+ const chunksWithExtractedModules = this . extractModulesAndReturnAffectedChunks ( extractableModules , affectedChunks ) ;
137143
138144 // connect all extracted modules with the common chunk
139- this . connectModulesWithCommonChunk ( commonChunk , reallyUsedModules ) ;
145+ this . addExtractedModulesToTargetChunk ( targetChunk , extractableModules ) ;
140146
141147 // set filenameTemplate for chunk
142148 if ( filenameTemplate )
143- commonChunk . filenameTemplate = filenameTemplate ;
149+ targetChunk . filenameTemplate = filenameTemplate ;
144150
145151 // if we are async connect the blocks of the "reallyUsedChunk" - the ones that had modules removed -
146152 // with the commonChunk and get the origins for the asyncChunk (remember "asyncChunk === commonChunk" at this moment).
147153 // bail out
148154 if ( asyncOption ) {
149- this . connectChunkBlocksWithCommonChunk ( reallyUsedChunks , commonChunk ) ;
150- asyncChunk . origins = this . getAsyncChunkOrigin ( reallyUsedChunks ) ;
155+ this . moveExtractedChunkBlocksToTargetChunk ( chunksWithExtractedModules , targetChunk ) ;
156+ asyncChunk . origins = this . extractOriginsOfChunksWithExtractedModules ( chunksWithExtractedModules ) ;
151157 return ;
152158 }
153159
154160 // we are not in "async" mode
155161 // connect used chunks with commonChunk - shouldnt this be reallyUsedChunks here?
156- this . addCommonChunkAsParentOfAffectedChunks ( usedChunks , commonChunk ) ;
162+ this . makeTargetChunkParentOfAffectedChunks ( affectedChunks , targetChunk ) ;
157163 } ) ;
158164 return true ;
159165 } ) ;
160166 } ) ;
161167 }
162168
163- getCommonChunks ( allChunks , compilation , chunkNames , children , asyncOption ) {
169+ getTargetChunks ( allChunks , compilation , chunkNames , children , asyncOption ) {
164170 const asyncOrNoSelectedChunk = children || asyncOption ;
165171
166172 // we have specified chunk names
@@ -191,24 +197,24 @@ The available options are:
191197 throw new Error ( "Invalid chunkNames argument" ) ;
192198 }
193199
194- getUsedChunks ( compilation , allChunks , commonChunk , commonChunks , currentIndex , selectedChunks , asyncOption , children ) {
200+ getAffectedChunks ( compilation , allChunks , targetChunk , targetChunks , currentIndex , selectedChunks , asyncOption , children ) {
195201 const asyncOrNoSelectedChunk = children || asyncOption ;
196202
197203 if ( Array . isArray ( selectedChunks ) ) {
198204 return allChunks . filter ( chunk => {
199- const notCommmonChunk = chunk !== commonChunk ;
205+ const notCommmonChunk = chunk !== targetChunk ;
200206 const isSelectedChunk = selectedChunks . indexOf ( chunk . name ) > - 1 ;
201207 return notCommmonChunk && isSelectedChunk ;
202208 } ) ;
203209 }
204210
205211 if ( asyncOrNoSelectedChunk ) {
206212 // nothing to do here
207- if ( ! commonChunk . chunks ) {
213+ if ( ! targetChunk . chunks ) {
208214 return [ ] ;
209215 }
210216
211- return commonChunk . chunks . filter ( ( chunk ) => {
217+ return targetChunk . chunks . filter ( ( chunk ) => {
212218 // we can only move modules from this chunk if the "commonChunk" is the only parent
213219 return asyncOption || chunk . parents . length === 1 ;
214220 } ) ;
@@ -217,30 +223,30 @@ The available options are:
217223 /**
218224 * past this point only entry chunks are allowed to become commonChunks
219225 */
220- if ( commonChunk . parents . length > 0 ) {
221- compilation . errors . push ( new Error ( "CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + commonChunk . name + ")" ) ) ;
226+ if ( targetChunk . parents . length > 0 ) {
227+ compilation . errors . push ( new Error ( "CommonsChunkPlugin: While running in normal mode it's not allowed to use a non-entry chunk (" + targetChunk . name + ")" ) ) ;
222228 return ;
223229 }
224230
225231 // what is this?
226232 return allChunks . filter ( ( chunk ) => {
227- const found = commonChunks . indexOf ( chunk ) ;
233+ const found = targetChunks . indexOf ( chunk ) ;
228234 if ( found >= currentIndex ) return false ;
229235 return chunk . hasRuntime ( ) ;
230236 } ) ;
231237 }
232238
233- createAsyncChunk ( compilation , asyncOption , commonChunk ) {
239+ createAsyncChunk ( compilation , asyncOption , targetChunk ) {
234240 const asyncChunk = compilation . addChunk ( typeof asyncOption === "string" ? asyncOption : undefined ) ;
235241 asyncChunk . chunkReason = "async commons chunk" ;
236242 asyncChunk . extraAsync = true ;
237- asyncChunk . addParent ( commonChunk ) ;
243+ asyncChunk . addParent ( targetChunk ) ;
238244 return asyncChunk ;
239245 }
240246
241247 // If minChunks is a function use that
242248 // otherwhise check if a module is used at least minChunks or 2 or usedChunks.length time
243- getModuleFilter ( minChunks , commonChunk , usedChunksLength ) {
249+ getModuleFilter ( minChunks , targetChunk , usedChunksLength ) {
244250 if ( typeof minChunks === "function" ) {
245251 return minChunks ;
246252 }
@@ -249,7 +255,7 @@ The available options are:
249255 return isUsedAtLeastMinTimes ;
250256 }
251257
252- getReallyUsedModules ( minChunks , usedChunks , commonChunk ) {
258+ getExtractableModules ( minChunks , usedChunks , targetChunk ) {
253259 if ( minChunks === Infinity ) {
254260 return [ ] ;
255261 }
@@ -264,7 +270,7 @@ The available options are:
264270 } , new Map ( ) ) ;
265271
266272 // filter by minChunks
267- const moduleFilterCount = this . getModuleFilter ( minChunks , commonChunk , usedChunks . length ) ;
273+ const moduleFilterCount = this . getModuleFilter ( minChunks , targetChunk , usedChunks . length ) ;
268274 // filter by condition
269275 const moduleFilterCondition = ( module , chunk ) => {
270276 if ( ! module . chunkCondition ) {
@@ -277,15 +283,15 @@ The available options are:
277283 const module = entry [ 0 ] ;
278284 const count = entry [ 1 ] ;
279285 // if the module passes both filters, keep it.
280- return moduleFilterCount ( module , count ) && moduleFilterCondition ( module , commonChunk ) ;
286+ return moduleFilterCount ( module , count ) && moduleFilterCondition ( module , targetChunk ) ;
281287 } ) . map ( entry => entry [ 0 ] ) ;
282288 }
283289
284- getModulesSize ( modules ) {
290+ calculateModuleSize ( modules ) {
285291 return modules . reduce ( ( count , module ) => count + module . size ( ) , 0 ) ;
286292 }
287293
288- removeModulesFromUsedChunksAndReturnUsedChunks ( reallyUsedModules , usedChunks ) {
294+ extractModulesAndReturnAffectedChunks ( reallyUsedModules , usedChunks ) {
289295 return reallyUsedModules . reduce ( ( affectedChunksSet , module ) => {
290296 for ( let chunk of usedChunks ) {
291297 // removeChunk returns true if the chunk was contained and succesfully removed
@@ -298,14 +304,14 @@ The available options are:
298304 } , new Set ( ) ) ;
299305 }
300306
301- connectModulesWithCommonChunk ( chunk , modules ) {
307+ addExtractedModulesToTargetChunk ( chunk , modules ) {
302308 for ( let module of modules ) {
303309 chunk . addModule ( module ) ;
304310 module . addChunk ( chunk ) ;
305311 }
306312 }
307313
308- addCommonChunkAsParentOfAffectedChunks ( usedChunks , commonChunk ) {
314+ makeTargetChunkParentOfAffectedChunks ( usedChunks , commonChunk ) {
309315 for ( let chunk of usedChunks ) {
310316 // set commonChunk as new sole parent
311317 chunk . parents = [ commonChunk ] ;
@@ -318,7 +324,7 @@ The available options are:
318324 }
319325 }
320326
321- connectChunkBlocksWithCommonChunk ( chunks , commonChunk ) {
327+ moveExtractedChunkBlocksToTargetChunk ( chunks , commonChunk ) {
322328 for ( let chunk of chunks ) {
323329 // only for non initial chunks
324330 // TODO: why?
@@ -331,7 +337,7 @@ The available options are:
331337 }
332338 }
333339
334- getAsyncChunkOrigin ( chunks ) {
340+ extractOriginsOfChunksWithExtractedModules ( chunks ) {
335341 const origins = [ ] ;
336342 for ( let chunk of chunks ) {
337343 for ( let origin of chunk . origins ) {
0 commit comments