@@ -85,7 +85,9 @@ module.exports = class SplitChunksPlugin {
8585
8686 static normalizeOptions ( options = { } ) {
8787 return {
88- chunks : options . chunks || "all" ,
88+ chunksFilter : SplitChunksPlugin . normalizeChunksFilter (
89+ options . chunks || "all"
90+ ) ,
8991 minSize : options . minSize || 0 ,
9092 minChunks : options . minChunks || 1 ,
9193 maxAsyncRequests : options . maxAsyncRequests || 1 ,
@@ -135,6 +137,19 @@ module.exports = class SplitChunksPlugin {
135137 if ( typeof name === "function" ) return name ;
136138 }
137139
140+ static normalizeChunksFilter ( chunks ) {
141+ if ( chunks === "initial" ) {
142+ return chunk => chunk . canBeInitial ( ) ;
143+ }
144+ if ( chunks === "async" ) {
145+ return chunk => ! chunk . canBeInitial ( ) ;
146+ }
147+ if ( chunks === "all" ) {
148+ return ( ) => true ;
149+ }
150+ if ( typeof chunks === "function" ) return chunks ;
151+ }
152+
138153 static normalizeCacheGroups ( { cacheGroups, automaticNameDelimiter } ) {
139154 if ( typeof cacheGroups === "function" ) {
140155 return cacheGroups ;
@@ -174,7 +189,9 @@ module.exports = class SplitChunksPlugin {
174189 name : option . name ,
175190 automaticNameDelimiter
176191 } ) ,
177- chunks : option . chunks ,
192+ chunksFilter : SplitChunksPlugin . normalizeChunksFilter (
193+ option . chunks
194+ ) ,
178195 enforce : option . enforce ,
179196 minSize : option . minSize ,
180197 minChunks : option . minChunks ,
@@ -264,7 +281,8 @@ module.exports = class SplitChunksPlugin {
264281 const cacheGroup = {
265282 key : cacheGroupSource . key ,
266283 priority : cacheGroupSource . priority || 0 ,
267- chunks : cacheGroupSource . chunks || this . options . chunks ,
284+ chunksFilter :
285+ cacheGroupSource . chunksFilter || this . options . chunksFilter ,
268286 minSize :
269287 cacheGroupSource . minSize !== undefined
270288 ? cacheGroupSource . minSize
@@ -304,18 +322,9 @@ module.exports = class SplitChunksPlugin {
304322 // Break if minimum number of chunks is not reached
305323 if ( chunkIndices . length < cacheGroup . minChunks ) continue ;
306324 // Select chunks by configuration
307- const selectedChunks =
308- typeof cacheGroup . chunks === "function"
309- ? cacheGroup . chunks ( Array . from ( chunkCombination ) )
310- : cacheGroup . chunks === "initial"
311- ? Array . from ( chunkCombination ) . filter ( chunk =>
312- chunk . canBeInitial ( )
313- )
314- : cacheGroup . chunks === "async"
315- ? Array . from ( chunkCombination ) . filter (
316- chunk => ! chunk . canBeInitial ( )
317- )
318- : Array . from ( chunkCombination ) ;
325+ const selectedChunks = Array . from ( chunkCombination ) . filter (
326+ cacheGroup . chunksFilter
327+ ) ;
319328 // Break if minimum number of chunks is not reached
320329 if ( selectedChunks . length < cacheGroup . minChunks ) continue ;
321330 // Determine name for split chunk
0 commit comments