Skip to content

Commit e76ad9e

Browse files
authored
Merge pull request #16803 from ryanwilsonperkin/revert-16759-real-content-hash-regex-perf
2 parents c989143 + 52b1b0e commit e76ad9e

File tree

1 file changed

+14
-44
lines changed

1 file changed

+14
-44
lines changed

lib/optimize/RealContentHashPlugin.js

+14-44
Original file line numberDiff line numberDiff line change
@@ -178,43 +178,10 @@ class RealContentHashPlugin {
178178
}
179179
}
180180
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"
183184
);
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-
218185
await Promise.all(
219186
assetsWithInfo.map(async asset => {
220187
const { name, source, content, hashes } = asset;
@@ -231,7 +198,7 @@ class RealContentHashPlugin {
231198
await cacheAnalyse.providePromise(name, etag, () => {
232199
const referencedHashes = new Set();
233200
let ownHashes = new Set();
234-
const inContent = hashMatch(content);
201+
const inContent = content.match(hashRegExp);
235202
if (inContent) {
236203
for (const hash of inContent) {
237204
if (hashes.has(hash)) {
@@ -331,7 +298,7 @@ ${referencingAssets
331298
identifier,
332299
etag,
333300
() => {
334-
const newContent = hashReplace(asset.content, hash =>
301+
const newContent = asset.content.replace(hashRegExp, hash =>
335302
hashToNewHash.get(hash)
336303
);
337304
return new RawSource(newContent);
@@ -356,12 +323,15 @@ ${referencingAssets
356323
identifier,
357324
etag,
358325
() => {
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);
362333
}
363-
return hashToNewHash.get(hash);
364-
});
334+
);
365335
return new RawSource(newContent);
366336
}
367337
);
@@ -407,7 +377,7 @@ ${referencingAssets
407377
await Promise.all(
408378
assetsWithInfo.map(async asset => {
409379
await computeNewContent(asset);
410-
const newName = hashReplace(asset.name, hash =>
380+
const newName = asset.name.replace(hashRegExp, hash =>
411381
hashToNewHash.get(hash)
412382
);
413383

0 commit comments

Comments
 (0)