1717'use strict' ;
1818
1919const objectAssign = require ( 'object-assign' ) ;
20+
21+ const flatten = arr => arr . reduce ( ( prev , curr ) => prev . concat ( curr ) , [ ] ) ;
22+
2023const defaultOptions = {
2124 rel : 'preload' ,
2225 include : 'asyncChunks' ,
@@ -39,21 +42,14 @@ class PreloadPlugin {
3942 // get wired up using link rel=preload when using this plugin. This behaviour can be
4043 // configured to preload all types of chunks or just prefetch chunks as needed.
4144 if ( options . include === undefined || options . include === 'asyncChunks' ) {
42- let asyncChunksSource = null ;
4345 try {
44- asyncChunksSource = compilation
45- . chunks . filter ( chunk => ! chunk . isInitial ( ) )
46- . map ( chunk => chunk . files ) ;
46+ extractedChunks = compilation . chunks . filter ( chunk => ! chunk . isInitial ( ) ) ;
4747 } catch ( e ) {
48- asyncChunksSource = compilation . chunks
49- . map ( chunk => chunk . files ) ;
48+ extractedChunks = compilation . chunks ;
5049 }
51- extractedChunks = [ ] . concat . apply ( [ ] , asyncChunksSource ) ;
5250 } else if ( options . include === 'all' ) {
5351 // Async chunks, vendor chunks, normal chunks.
54- extractedChunks = compilation
55- . chunks
56- . reduce ( ( chunks , chunk ) => chunks . concat ( chunk . files ) , [ ] ) ;
52+ extractedChunks = compilation . chunks ;
5753 } else if ( Array . isArray ( options . include ) ) {
5854 // Keep only user specified chunks
5955 extractedChunks = compilation
@@ -65,14 +61,12 @@ class PreloadPlugin {
6561 return false ;
6662 }
6763 return options . include . indexOf ( chunkName ) > - 1 ;
68- } )
69- . map ( chunk => chunk . files )
70- . reduce ( ( prev , curr ) => prev . concat ( curr ) , [ ] ) ;
64+ } ) ;
7165 }
7266
7367 const publicPath = compilation . outputOptions . publicPath || '' ;
7468
75- extractedChunks . filter ( entry => {
69+ flatten ( extractedChunks . map ( chunk => chunk . files ) ) . filter ( entry => {
7670 return this . options . fileBlacklist . every ( regex => regex . test ( entry ) === false ) ;
7771 } ) . forEach ( entry => {
7872 entry = `${ publicPath } ${ entry } ` ;
0 commit comments