Skip to content

Commit 2435b40

Browse files
perf(hashing): skip materializing assets when no content hash is named
RealContentHashPlugin read every asset's full text before deciding whether it had anything to do: the bail-out for a build that names no content hash sat after the loop that materialized them all. Collect each asset's hashes from its info first, return when none carries one, and materialize only then.
1 parent 87a62de commit 2435b40

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": patch
3+
---
4+
5+
Skip reading every asset when no content hash is named.

‎lib/optimize/RealContentHashPlugin.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,36 @@ class RealContentHashPlugin {
243243
// real hash is re-encoded in it instead of `output.hashDigest`.
244244
/** @type {Map<string, string>} */
245245
const hashToDigest = new Map();
246+
// Which hashes each asset carries, from its info alone: a build
247+
// naming no content hash has nothing to do here, and materializing
248+
// every asset to find that out is the whole cost.
249+
/** @type {{ source: Source, info: AssetInfo, name: string, hashes: Hashes }[]} */
250+
const candidates = [];
251+
let anyHashes = false;
246252
for (const { source, info, name } of assets) {
247-
const cachedSource = toCachedSource(source);
248-
const content = /** @type {string} */ (cachedSource.source());
249253
/** @type {Hashes} */
250254
const hashes = new Set();
251255
addToList(info.contenthash, hashes);
252256
if (repairing) addToList(info.chunkhash, hashes);
257+
if (hashes.size !== 0) anyHashes = true;
253258
if (info.contenthashDigest) {
254259
for (const hash of Object.keys(info.contenthashDigest)) {
255260
hashToDigest.set(hash, info.contenthashDigest[hash]);
256261
}
257262
}
263+
candidates.push({ source, info, name, hashes });
264+
}
265+
if (!anyHashes) return;
266+
for (const { source, info, name, hashes } of candidates) {
267+
const cachedSource = toCachedSource(source);
258268
/** @type {AssetInfoForRealContentHash} */
259269
const data = {
260270
name,
261271
info,
262272
source: cachedSource,
263273
newSource: undefined,
264274
newSourceWithoutOwn: undefined,
265-
content,
275+
content: /** @type {string} */ (cachedSource.source()),
266276
ownHashes: undefined,
267277
contentComputePromise: undefined,
268278
contentComputeWithoutOwnPromise: undefined,
@@ -279,7 +289,6 @@ class RealContentHashPlugin {
279289
}
280290
}
281291
}
282-
if (hashToAssets.size === 0) return;
283292
const hashRegExp = new RegExp(
284293
Array.from(hashToAssets.keys(), quoteMeta).join("|"),
285294
"g"

0 commit comments

Comments
 (0)