44*/
55"use strict" ;
66let nextIdent = 0 ;
7+
78class CommonsChunkPlugin {
89 constructor ( options ) {
910 if ( arguments . length > 1 ) {
@@ -31,6 +32,7 @@ The available options are:
3132 this . minChunks = normalizedOptions . minChunks ;
3233 this . selectedChunks = normalizedOptions . selectedChunks ;
3334 this . children = normalizedOptions . children ;
35+ this . deepChildren = normalizedOptions . deepChildren ;
3436 this . async = normalizedOptions . async ;
3537 this . minSize = normalizedOptions . minSize ;
3638 this . ident = __filename + ( nextIdent ++ ) ;
@@ -76,6 +78,7 @@ You can however specify the name of the async chunk by passing the desired strin
7678 minChunks : options . minChunks ,
7779 selectedChunks : options . chunks ,
7880 children : options . children ,
81+ deepChildren : options . deepChildren ,
7982 async : options . async ,
8083 minSize : options . minSize
8184 } ;
@@ -211,6 +214,37 @@ You can however specify the name of the async chunk by passing the desired strin
211214Take a look at the "name"/"names" or async/children option.` ) ;
212215 }
213216
217+ getAffectedUnnamedChunks ( affectedChunks , targetChunk , asyncOption ) {
218+ let chunks = targetChunk . chunks ;
219+ chunks && chunks . forEach ( ( chunk ) => {
220+ if ( chunk . isInitial ( ) ) {
221+ return ;
222+ }
223+ // If all the parents of a chunk are either
224+ // a) the target chunk we started with
225+ // b) themselves affected chunks
226+ // we can assume that this chunk is an affected chunk too, as there is no way a chunk that
227+ // isn't only depending on the target chunk is a parent of the chunk tested
228+ if ( asyncOption || chunk . parents . every ( ( parentChunk ) => parentChunk === targetChunk || affectedChunks . indexOf ( parentChunk ) >= 0 ) ) {
229+ // This check not only dedupes the affectedChunks but also guarantees we avoid endless loops
230+ if ( affectedChunks . findIndex ( usedChunk => usedChunk === chunk ) < 1 ) {
231+ // We mutate the affected chunks before going deeper, so the deeper levels and other branches
232+ // Have the information of this chunk being affected for their assertion if a chunk shouldnt
233+ // be affected
234+ affectedChunks . push ( chunk ) ;
235+
236+ // We recurse down to all the children of the chunk, applying the same assumption.
237+ // This guarantees that if a chunk should be an affected chunk,
238+ // at the latest the last connection to the same chunk meets the
239+ // condition to add it to the affected chunks.
240+ if ( this . deepChildren === true ) {
241+ this . getAffectedUnnamedChunks ( affectedChunks , chunk , asyncOption ) ;
242+ }
243+ }
244+ }
245+ } ) ;
246+ }
247+
214248 getAffectedChunks ( compilation , allChunks , targetChunk , targetChunks , currentIndex , selectedChunks , asyncOption , children ) {
215249 const asyncOrNoSelectedChunk = children || asyncOption ;
216250
@@ -223,22 +257,9 @@ Take a look at the "name"/"names" or async/children option.`);
223257 }
224258
225259 if ( asyncOrNoSelectedChunk ) {
226- // nothing to do here
227- if ( ! targetChunk . chunks ) {
228- return [ ] ;
229- }
230-
231- return targetChunk . chunks . filter ( ( chunk ) => {
232- // we only are interested in on-demand chunks
233- if ( chunk . isInitial ( ) )
234- return false ;
235-
236- // we can only move modules from this chunk if the "commonChunk" is the only parent
237- if ( ! asyncOption )
238- return chunk . parents . length === 1 ;
239-
240- return true ;
241- } ) ;
260+ let affectedChunks = [ ] ;
261+ this . getAffectedUnnamedChunks ( affectedChunks , targetChunk , asyncOption ) ;
262+ return affectedChunks ;
242263 }
243264
244265 /**
0 commit comments