@@ -41,21 +41,54 @@ function checkConditions(asset, compilation, conditions = []) {
4141}
4242
4343/**
44- * Returns the names of all the assets in a chunk group.
44+ * Returns the names of all the assets in all the chunks in a chunk group,
45+ * if provided a chunk group name.
46+ * Otherwise, if provided a chunk name, return all the assets in that chunk.
47+ * Otherwise, if there isn't a chunk group or chunk with that name, return null.
4548 *
46- * @param {ChunkGroup } chunkGroup
49+ * @param {Compilation } compilation
50+ * @param {string } chunkOrGroup
51+ * @return {Array<Asset>|null }
52+ * @private
53+ */
54+ function getNamesOfAssetsInChunkOrGroup ( compilation , chunkOrGroup ) {
55+ const chunkGroup = compilation . namedChunkGroups &&
56+ compilation . namedChunkGroups . get ( chunkOrGroup ) ;
57+ if ( chunkGroup ) {
58+ const assetNames = [ ] ;
59+ for ( const chunk of chunkGroup . chunks ) {
60+ assetNames . push ( ...getNamesOfAssetsInChunk ( chunk ) ) ;
61+ }
62+ return assetNames ;
63+ } else {
64+ const chunk = compilation . namedChunks &&
65+ compilation . namedChunks . get ( chunkOrGroup ) ;
66+ if ( chunk ) {
67+ return getNamesOfAssetsInChunk ( chunk ) ;
68+ }
69+ }
70+
71+ // If we get here, there's no chunkGroup or chunk with that name.
72+ return null ;
73+ }
74+
75+ /**
76+ * Returns the names of all the assets in a chunk.
77+ *
78+ * @param {Chunk } chunk
4779 * @return {Array<Asset> }
4880 * @private
4981 */
50- function getNamesOfAssetsInChunkGroup ( chunkGroup ) {
82+ function getNamesOfAssetsInChunk ( chunk ) {
5183 const assetNames = [ ] ;
52- for ( const chunk of chunkGroup . chunks ) {
53- assetNames . splice ( 0 , 0 , ...chunk . files ) ;
54- // This only appears to be set in webpack v5.
55- if ( chunk . auxiliaryFiles ) {
56- assetNames . splice ( 0 , 0 , ... chunk . auxiliaryFiles ) ;
57- }
84+
85+ assetNames . push ( ...chunk . files ) ;
86+
87+ // This only appears to be set in webpack v5.
88+ if ( chunk . auxiliaryFiles ) {
89+ assetNames . push ( ... chunk . auxiliaryFiles ) ;
5890 }
91+
5992 return assetNames ;
6093}
6194
@@ -77,14 +110,16 @@ function filterAssets(compilation, config) {
77110 const allowedAssetNames = new Set ( ) ;
78111 // See https://github.com/GoogleChrome/workbox/issues/1287
79112 if ( Array . isArray ( config . chunks ) ) {
80- for ( const chunkName of config . chunks ) {
81- const namedChunkGroup = compilation . namedChunkGroups . get ( chunkName ) ;
82- if ( namedChunkGroup ) {
83- for ( const assetName of getNamesOfAssetsInChunkGroup ( namedChunkGroup ) ) {
113+ for ( const name of config . chunks ) {
114+ // See https://github.com/GoogleChrome/workbox/issues/2717
115+ const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup (
116+ compilation , name ) ;
117+ if ( assetsInChunkOrGroup ) {
118+ for ( const assetName of assetsInChunkOrGroup ) {
84119 allowedAssetNames . add ( assetName ) ;
85120 }
86121 } else {
87- compilation . warnings . push ( new Error ( `The chunk '${ chunkName } ' was ` +
122+ compilation . warnings . push ( new Error ( `The chunk '${ name } ' was ` +
88123 `provided in your Workbox chunks config, but was not found in the ` +
89124 `compilation.` ) ) ;
90125 }
@@ -93,10 +128,12 @@ function filterAssets(compilation, config) {
93128
94129 const deniedAssetNames = new Set ( ) ;
95130 if ( Array . isArray ( config . excludeChunks ) ) {
96- for ( const chunkName of config . excludeChunks ) {
97- const namedChunkGroup = compilation . namedChunkGroups . get ( chunkName ) ;
98- if ( namedChunkGroup ) {
99- for ( const assetName of getNamesOfAssetsInChunkGroup ( namedChunkGroup ) ) {
131+ for ( const name of config . excludeChunks ) {
132+ // See https://github.com/GoogleChrome/workbox/issues/2717
133+ const assetsInChunkOrGroup = getNamesOfAssetsInChunkOrGroup (
134+ compilation , name ) ;
135+ if ( assetsInChunkOrGroup ) {
136+ for ( const assetName of assetsInChunkOrGroup ) {
100137 deniedAssetNames . add ( assetName ) ;
101138 }
102139 } // Don't warn if the chunk group isn't found.
0 commit comments