@@ -178,43 +178,10 @@ class RealContentHashPlugin {
178
178
}
179
179
}
180
180
if ( hashToAssets . size === 0 ) return ;
181
- const hashRegExps = Array . from ( hashToAssets . keys ( ) , quoteMeta ) . map (
182
- hash => new RegExp ( hash , "g" )
181
+ const hashRegExp = new RegExp (
182
+ Array . from ( hashToAssets . keys ( ) , quoteMeta ) . join ( "|" ) ,
183
+ "g"
183
184
) ;
184
-
185
- /**
186
- * @param {string } str string to be matched against all hashRegExps
187
- * @returns {string[] | null } matches found
188
- */
189
- const hashMatch = str => {
190
- /** @type {string[] } */
191
- const results = [ ] ;
192
- for ( const hashRegExp of hashRegExps ) {
193
- const matches = str . match ( hashRegExp ) ;
194
- if ( matches ) {
195
- matches . forEach ( match => results . push ( match ) ) ;
196
- }
197
- }
198
- if ( results . length ) {
199
- return results ;
200
- } else {
201
- return null ;
202
- }
203
- } ;
204
-
205
- /**
206
- * @param {string } str string to be replaced with all hashRegExps
207
- * @param {function(string): string } fn replacement function to use when a hash is found
208
- * @returns {string } replaced content
209
- */
210
- const hashReplace = ( str , fn ) => {
211
- let result = str ;
212
- for ( const hashRegExp of hashRegExps ) {
213
- result = result . replace ( hashRegExp , fn ) ;
214
- }
215
- return result ;
216
- } ;
217
-
218
185
await Promise . all (
219
186
assetsWithInfo . map ( async asset => {
220
187
const { name, source, content, hashes } = asset ;
@@ -231,7 +198,7 @@ class RealContentHashPlugin {
231
198
await cacheAnalyse . providePromise ( name , etag , ( ) => {
232
199
const referencedHashes = new Set ( ) ;
233
200
let ownHashes = new Set ( ) ;
234
- const inContent = hashMatch ( content ) ;
201
+ const inContent = content . match ( hashRegExp ) ;
235
202
if ( inContent ) {
236
203
for ( const hash of inContent ) {
237
204
if ( hashes . has ( hash ) ) {
@@ -331,7 +298,7 @@ ${referencingAssets
331
298
identifier ,
332
299
etag ,
333
300
( ) => {
334
- const newContent = hashReplace ( asset . content , hash =>
301
+ const newContent = asset . content . replace ( hashRegExp , hash =>
335
302
hashToNewHash . get ( hash )
336
303
) ;
337
304
return new RawSource ( newContent ) ;
@@ -356,12 +323,15 @@ ${referencingAssets
356
323
identifier ,
357
324
etag ,
358
325
( ) => {
359
- const newContent = hashReplace ( asset . content , hash => {
360
- if ( asset . ownHashes . has ( hash ) ) {
361
- return "" ;
326
+ const newContent = asset . content . replace (
327
+ hashRegExp ,
328
+ hash => {
329
+ if ( asset . ownHashes . has ( hash ) ) {
330
+ return "" ;
331
+ }
332
+ return hashToNewHash . get ( hash ) ;
362
333
}
363
- return hashToNewHash . get ( hash ) ;
364
- } ) ;
334
+ ) ;
365
335
return new RawSource ( newContent ) ;
366
336
}
367
337
) ;
@@ -407,7 +377,7 @@ ${referencingAssets
407
377
await Promise . all (
408
378
assetsWithInfo . map ( async asset => {
409
379
await computeNewContent ( asset ) ;
410
- const newName = hashReplace ( asset . name , hash =>
380
+ const newName = asset . name . replace ( hashRegExp , hash =>
411
381
hashToNewHash . get ( hash )
412
382
) ;
413
383
0 commit comments