@@ -287,47 +287,58 @@ class WebpackOptionsApply extends OptionsApply {
287
287
new FlagDependencyUsagePlugin ( ) . apply ( compiler ) ;
288
288
if ( options . optimization . concatenateModules )
289
289
new ModuleConcatenationPlugin ( ) . apply ( compiler ) ;
290
- const normalizeCommonsChunksNameOption = value => {
290
+ const nameOptionToName = value => {
291
291
if ( value === true ) {
292
292
return ( module , chunks ) => {
293
293
const names = chunks . map ( c => c . name ) ;
294
294
if ( ! names . every ( Boolean ) ) return ;
295
295
names . sort ( ) ;
296
296
return names . join ( "~" ) ;
297
297
} ;
298
- } else if ( typeof value === "string" ) {
299
- return ( module , chunks ) => {
300
- return value ;
301
- } ;
302
298
} else {
303
299
return value ;
304
300
}
305
- }
306
- if ( options . optimization . asyncCommonsChunks ) {
307
- const accpOptions = Object . assign ( { } , options . optimization . asyncCommonsChunks , {
308
- initialChunks : false ,
309
- name : normalizeCommonsChunksNameOption ( typeof options . optimization . asyncCommonsChunks === "object" ? options . optimization . asyncCommonsChunks . name : undefined )
310
- } ) ;
311
- new AutomaticCommonsChunksPlugin ( accpOptions ) . apply ( compiler ) ;
312
- }
313
- if ( options . optimization . initialCommonsChunks || options . optimization . initialVendorsChunk ) {
314
- let nameOption = options . optimization . initialVendorsChunk ;
315
- if ( nameOption === true ) {
316
- nameOption = {
317
- vendors : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] /
301
+ } ;
302
+ const initialVendorsOptionToEnforce = value => {
303
+ if ( value === true ) {
304
+ value = / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / ;
305
+ }
306
+ if ( typeof value === "string" ) {
307
+ value = {
308
+ [ value ] : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] /
309
+ } ;
310
+ }
311
+ return vendorsOptionToEnforce ( value ) ;
312
+ } ;
313
+ const vendorsOptionToEnforce = value => {
314
+ if ( value === true ) {
315
+ value = / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] / ;
316
+ }
317
+ if ( typeof value === "string" ) {
318
+ value = {
319
+ [ value ] : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] /
320
+ } ;
321
+ }
322
+ if ( value instanceof RegExp ) {
323
+ return ( module , chunks ) => {
324
+ if ( ! module . nameForCondition ) return ;
325
+ const name = module . nameForCondition ( ) ;
326
+ return value . test ( name ) ;
318
327
} ;
319
328
}
320
- if ( typeof nameOption === "string" ) {
321
- nameOption = {
322
- [ nameOption ] : / [ \\ / ] n o d e _ m o d u l e s [ \\ / ] /
329
+ if ( typeof value === "string" ) {
330
+ return ( module , chunks ) => {
331
+ if ( ! module . nameForCondition ) return ;
332
+ const name = module . nameForCondition ( ) ;
333
+ return name . startsWith ( value ) ;
323
334
} ;
324
335
}
325
- if ( nameOption && typeof nameOption === "object" ) {
326
- nameOption = ( ( nameOption ) => ( ( module , chunks ) => {
336
+ if ( value && typeof value === "object" ) {
337
+ return ( module , chunks ) => {
327
338
if ( ! module . nameForCondition ) return ;
328
339
const name = module . nameForCondition ( ) ;
329
- for ( const chunkName of Object . keys ( nameOption ) ) {
330
- const regExp = nameOption [ chunkName ] ;
340
+ for ( const chunkName of Object . keys ( value ) ) {
341
+ const regExp = value [ chunkName ] ;
331
342
if ( typeof regExp === "string" ) {
332
343
if ( name . startsWith ( regExp ) )
333
344
return chunkName ;
@@ -342,26 +353,30 @@ class WebpackOptionsApply extends OptionsApply {
342
353
return chunkName ;
343
354
}
344
355
}
345
- } ) ) ( nameOption ) ;
346
- }
347
- let commonsNameOption = normalizeCommonsChunksNameOption ( typeof options . optimization . initialCommonsChunks === "object" ? options . optimization . initialCommonsChunks . name : undefined ) ;
348
- let name ;
349
- if ( commonsNameOption && ! nameOption ) {
350
- name = commonsNameOption ;
351
- } else if ( commonsNameOption ) {
352
- name = ( module , chunks ) => {
353
- const r = nameOption ( module , chunks ) ;
354
- if ( r !== undefined ) return r ;
355
- return commonsNameOption ( module , chunks ) ;
356
356
} ;
357
- } else {
358
- name = nameOption ;
359
357
}
358
+ return value ;
359
+ } ;
360
+ if ( options . optimization . asyncCommonsChunks || options . optimization . asyncVendorsChunks ) {
361
+ const accpOptions = Object . assign ( { } , options . optimization . asyncCommonsChunks , {
362
+ initialChunks : false ,
363
+ name : nameOptionToName ( typeof options . optimization . asyncCommonsChunks === "object" ? options . optimization . asyncCommonsChunks . name : undefined ) ,
364
+ enforce : vendorsOptionToEnforce ( options . optimization . asyncVendorsChunks )
365
+ } ) ;
366
+ if ( ! options . optimization . asyncCommonsChunks ) {
367
+ accpOptions . minChunks = Infinity ;
368
+ }
369
+ new AutomaticCommonsChunksPlugin ( accpOptions ) . apply ( compiler ) ;
370
+ }
371
+ if ( options . optimization . initialCommonsChunks || options . optimization . initialVendorsChunks ) {
360
372
const accpOptions = Object . assign ( { } , options . optimization . initialCommonsChunks , {
361
373
initialChunks : true ,
362
- onlyNamed : ! options . optimization . initialCommonsChunks ,
363
- name
374
+ name : nameOptionToName ( typeof options . optimization . initialCommonsChunks === "object" ? options . optimization . initialCommonsChunks . name : undefined ) ,
375
+ enforce : initialVendorsOptionToEnforce ( options . optimization . initialVendorsChunks )
364
376
} ) ;
377
+ if ( ! options . optimization . initialCommonsChunks ) {
378
+ accpOptions . minChunks = Infinity ;
379
+ }
365
380
new AutomaticCommonsChunksPlugin ( accpOptions ) . apply ( compiler ) ;
366
381
}
367
382
if ( options . optimization . noEmitOnErrors )
0 commit comments