@@ -27,9 +27,9 @@ class Integrity {
2727
2828 this . options = options ;
2929 this . chunkChildChunksMap = new WeakMap ( ) ;
30- this . referencePlaceholders = new Map ( ) ;
31- this . placeholderByChunkId = new Map ( ) ;
3230 this . chunkByChunkId = new Map ( ) ;
31+ this . templateByChunkId = new Map ( ) ;
32+ this . placeholderByChunkId = new Map ( ) ;
3333 }
3434
3535 apply ( compiler ) {
@@ -69,12 +69,12 @@ class Integrity {
6969 this . isRealContentHash = this . options . isRealContentHash ( ) ;
7070
7171 // dynamically import a JS file
72- mainTemplate . hooks . jsonpScript . tap ( pluginName , ( source ) => this . addReference ( 'script' , source ) ) ;
73- mainTemplate . hooks . linkPreload . tap ( pluginName , ( source ) => this . addReference ( 'link' , source ) ) ;
74- mainTemplate . hooks . localVars . tap ( pluginName , this . setReferencePlaceholder . bind ( this ) ) ;
72+ mainTemplate . hooks . jsonpScript . tap ( pluginName , ( source ) => this . getTemplateByTag ( 'script' , source ) ) ;
73+ mainTemplate . hooks . linkPreload . tap ( pluginName , ( source ) => this . getTemplateByTag ( 'link' , source ) ) ;
74+ mainTemplate . hooks . localVars . tap ( pluginName , this . getTemplateByChunk . bind ( this ) ) ;
7575
7676 compilation . hooks . beforeRuntimeRequirements . tap ( pluginName , ( ) => {
77- this . placeholderByChunkId . clear ( ) ;
77+ // note: don't clear the cached placeholderByChunkId
7878 } ) ;
7979
8080 compilation . hooks . processAssets . tap (
@@ -134,6 +134,7 @@ class Integrity {
134134 }
135135
136136 // TODO: find the use case when childChunk.files.size > 1
137+ // the size of childChunk.files is always 1
137138 const childChunkFile = [ ...childChunk . files ] [ 0 ] ;
138139 const placeholder = this . placeholderByChunkId . get ( childChunk . id ) ;
139140
@@ -143,16 +144,22 @@ class Integrity {
143144 childChunkFile ,
144145 ( source ) => source ,
145146 ( assetInfo ) => {
146- return assetInfo
147- ? {
148- ...assetInfo ,
149- contenthash : Array . isArray ( assetInfo . contenthash )
150- ? [ ...new Set ( [ ...assetInfo . contenthash , placeholder ] ) ]
151- : assetInfo . contenthash
152- ? [ ...new Set ( [ assetInfo . contenthash , placeholder ] ) ]
153- : placeholder ,
154- }
155- : undefined ;
147+ if ( ! assetInfo ) {
148+ return undefined ;
149+ }
150+
151+ let contenthash = placeholder ;
152+
153+ if ( Array . isArray ( assetInfo . contenthash ) ) {
154+ contenthash = [ ...new Set ( [ ...assetInfo . contenthash , placeholder ] ) ] ;
155+ } else if ( assetInfo . contenthash ) {
156+ contenthash = [ ...new Set ( [ assetInfo . contenthash , placeholder ] ) ] ;
157+ }
158+
159+ return {
160+ ...assetInfo ,
161+ contenthash,
162+ } ;
156163 }
157164 ) ;
158165 } else {
@@ -172,13 +179,13 @@ class Integrity {
172179 }
173180
174181 /**
175- * Add the reference of integrity hashes into a tag object .
182+ * Create the integrity template by the tag.
176183 *
177184 * @param {string } tagName
178185 * @param {string } source
179186 * @return {string }
180187 */
181- addReference = ( tagName , source ) => {
188+ getTemplateByTag = ( tagName , source ) => {
182189 const { compilation, pluginName } = this ;
183190 const { Template } = compilation . compiler . webpack ;
184191 const { crossOriginLoading } = compilation . outputOptions ;
@@ -191,19 +198,21 @@ class Integrity {
191198 } ;
192199
193200 /**
194- * Set the placeholder in the hash reference using the hash of a chunk file.
201+ * Create the integrity template by the chunk.
202+ *
203+ * Saves the placeholder in the hash reference using the hash of a chunk file.
195204 * When the asset is processed, the placeholder will be replaced
196205 * with real integrity hash of the processed asset.
197206 *
198207 * @param {string } source
199208 * @param {Chunk } chunk
200209 * @return {string }
201210 */
202- setReferencePlaceholder ( source , chunk ) {
211+ getTemplateByChunk ( source , chunk ) {
203212 const { Template } = this . compilation . compiler . webpack ;
204213
205- if ( this . referencePlaceholders . has ( chunk . id ) ) {
206- return this . referencePlaceholders . get ( chunk . id ) ;
214+ if ( this . templateByChunkId . has ( chunk . id ) ) {
215+ return this . templateByChunkId . get ( chunk . id ) ;
207216 }
208217
209218 const childChunks = chunk . getAllAsyncChunks ( ) ;
@@ -218,10 +227,10 @@ class Integrity {
218227 }
219228
220229 if ( Object . keys ( placeholders ) . length > 0 ) {
221- const refTemplate = Template . asString ( [ source , `${ hashesReference } = ${ JSON . stringify ( placeholders ) } ;` ] ) ;
222- this . referencePlaceholders . set ( chunk . id , refTemplate ) ;
230+ const template = Template . asString ( [ source , `${ hashesReference } = ${ JSON . stringify ( placeholders ) } ;` ] ) ;
231+ this . templateByChunkId . set ( chunk . id , template ) ;
223232
224- return refTemplate ;
233+ return template ;
225234 }
226235
227236 return source ;
@@ -320,10 +329,10 @@ class Integrity {
320329 */
321330const getPlaceholder = ( chunkId ) => {
322331 // the prefix must be exact 7 chars, the same length as a hash function name, e.g. 'sha256-'
323- const placeholderPrefix = '___TMP -' ;
332+ const prefix = 'xxxxxx -' ;
324333 const hash = Integrity . computeIntegrity ( chunkId ) ;
325334
326- return placeholderPrefix + hash . slice ( placeholderPrefix . length ) ;
335+ return prefix + hash . slice ( prefix . length ) ;
327336} ;
328337
329338module . exports = Integrity ;
0 commit comments