@@ -30,10 +30,14 @@ class Integrity {
3030 this . chunkByChunkId = new Map ( ) ;
3131 this . templateByChunkId = new Map ( ) ;
3232 this . placeholderByChunkId = new Map ( ) ;
33+ this . Template = null ;
3334 }
3435
3536 apply ( compiler ) {
3637 const { pluginName } = this ;
38+ const { Template } = compiler . webpack ;
39+
40+ this . Template = Template ;
3741
3842 compiler . hooks . afterPlugins . tap ( pluginName , ( compiler ) => {
3943 compiler . hooks . thisCompilation . tap ( { name : pluginName , stage : - 10000 } , ( compilation ) => {
@@ -74,7 +78,7 @@ class Integrity {
7478 mainTemplate . hooks . localVars . tap ( pluginName , this . getTemplateByChunk . bind ( this ) ) ;
7579
7680 compilation . hooks . beforeRuntimeRequirements . tap ( pluginName , ( ) => {
77- // note: don't clear the cached placeholderByChunkId
81+ this . templateByChunkId . clear ( ) ;
7882 } ) ;
7983
8084 compilation . hooks . processAssets . tap (
@@ -111,6 +115,9 @@ class Integrity {
111115 return chunk ? Integrity . getIntegrity ( this . compilation , content [ 0 ] , chunk ) : undefined ;
112116 }
113117
118+ /**
119+ * @param {Object } assets
120+ */
114121 processAssets ( assets ) {
115122 const { compilation, isRealContentHash } = this ;
116123 const { compiler } = compilation ;
@@ -122,11 +129,7 @@ class Integrity {
122129 continue ;
123130 }
124131
125- let childChunks = this . chunkChildChunksMap . get ( chunk ) ;
126- if ( ! childChunks ) {
127- childChunks = chunk . getAllAsyncChunks ( ) ;
128- this . chunkChildChunksMap . set ( chunk , childChunks ) ;
129- }
132+ const childChunks = this . getChildChunks ( chunk ) ;
130133
131134 for ( const childChunk of childChunks ) {
132135 if ( childChunk . id == null ) {
@@ -178,6 +181,21 @@ class Integrity {
178181 }
179182 }
180183
184+ /**
185+ * @param {Chunk } chunk The webpack chunk.
186+ * @return {Set<Chunk> } The child chunks.
187+ */
188+ getChildChunks ( chunk ) {
189+ let childChunks = this . chunkChildChunksMap . get ( chunk ) ;
190+
191+ if ( ! childChunks ) {
192+ childChunks = chunk . getAllAsyncChunks ( ) ;
193+ this . chunkChildChunksMap . set ( chunk , childChunks ) ;
194+ }
195+
196+ return childChunks ;
197+ }
198+
181199 /**
182200 * Create the integrity template by the tag.
183201 *
@@ -209,31 +227,31 @@ class Integrity {
209227 * @return {string }
210228 */
211229 getTemplateByChunk ( source , chunk ) {
212- const { Template } = this . compilation . compiler . webpack ;
213-
214230 if ( this . templateByChunkId . has ( chunk . id ) ) {
215231 return this . templateByChunkId . get ( chunk . id ) ;
216232 }
217233
218- const childChunks = chunk . getAllAsyncChunks ( ) ;
219- this . chunkChildChunksMap . set ( chunk , childChunks ) ;
234+ const childChunks = this . getChildChunks ( chunk ) ;
235+
236+ if ( childChunks . size < 1 ) {
237+ return source ;
238+ }
220239
221240 const placeholders = { } ;
222241 for ( const childChunk of childChunks ) {
223- const placeholder = getPlaceholder ( childChunk . id ) ;
242+ let placeholder = this . placeholderByChunkId . get ( childChunk . id ) ;
243+ if ( ! placeholder ) {
244+ placeholder = getPlaceholder ( childChunk . id ) ;
245+ this . placeholderByChunkId . set ( childChunk . id , placeholder ) ;
246+ }
224247 placeholders [ childChunk . id ] = placeholder ;
225- this . placeholderByChunkId . set ( childChunk . id , placeholder ) ;
226248 this . chunkByChunkId . set ( childChunk . id , childChunk ) ;
227249 }
228250
229- if ( Object . keys ( placeholders ) . length > 0 ) {
230- const template = Template . asString ( [ source , `${ hashesReference } = ${ JSON . stringify ( placeholders ) } ;` ] ) ;
231- this . templateByChunkId . set ( chunk . id , template ) ;
232-
233- return template ;
234- }
251+ const template = this . Template . asString ( [ source , `${ hashesReference } = ${ JSON . stringify ( placeholders ) } ;` ] ) ;
252+ this . templateByChunkId . set ( chunk . id , template ) ;
235253
236- return source ;
254+ return template ;
237255 }
238256
239257 /**
0 commit comments