Skip to content

Commit 6cb7807

Browse files
committed
fix: avoid Array#flat() so optimize() runs on Node 10
`Array.prototype.flat` only landed in Node 11; the engines field is `>= 10.13.0`. Restore the `.filter` + `.map` split and stash each asset's matched-minimizer indices in a `Map` keyed by name, so the matching regexes still run only once per asset and the array of records is built without `flat`.
1 parent 0dfbbcc commit 6cb7807

1 file changed

Lines changed: 30 additions & 23 deletions

File tree

src/index.js

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -412,20 +412,21 @@ class TerserPlugin {
412412

413413
return matched;
414414
};
415-
const assetsForMinify = (
416-
await Promise.all(
417-
Object.keys(assets).map(async (name) => {
418-
const { info, source } = /** @type {Asset} */ (
419-
compilation.getAsset(name)
420-
);
415+
/** @type {Map<string, number[]>} */
416+
const matchedByName = new Map();
417+
418+
const assetsForMinify = await Promise.all(
419+
Object.keys(assets)
420+
.filter((name) => {
421+
const { info } = /** @type {Asset} */ (compilation.getAsset(name));
421422

422423
if (
423424
// Skip double minimize assets from child compilation
424425
info.minimized ||
425426
// Skip minimizing for extracted comments assets
426427
info.extractedComments
427428
) {
428-
return [];
429+
return false;
429430
}
430431

431432
if (
@@ -434,17 +435,26 @@ class TerserPlugin {
434435
this.options,
435436
)(name)
436437
) {
437-
return [];
438+
return false;
438439
}
439440

440-
// Compute the matching minimizers once and carry them through to
441-
// the per-asset task so we don't pay the lookup twice.
441+
// Compute the matching minimizers once and carry the result to the
442+
// per-asset task via `matchedByName` so the regexes don't run again.
442443
const matched = matchingMinimizers(name, info);
443444

444445
if (matched.length === 0) {
445-
return [];
446+
return false;
446447
}
447448

449+
matchedByName.set(name, matched);
450+
451+
return true;
452+
})
453+
.map(async (name) => {
454+
const { info, source } = /** @type {Asset} */ (
455+
compilation.getAsset(name)
456+
);
457+
448458
const eTag = cache.getLazyHashedEtag(source);
449459
const cacheItem = cache.getItemCache(name, eTag);
450460
const output = await cacheItem.getPromise();
@@ -453,19 +463,16 @@ class TerserPlugin {
453463
numberOfAssets += 1;
454464
}
455465

456-
return [
457-
{
458-
name,
459-
info,
460-
inputSource: source,
461-
output,
462-
cacheItem,
463-
matched,
464-
},
465-
];
466+
return {
467+
name,
468+
info,
469+
inputSource: source,
470+
output,
471+
cacheItem,
472+
matched: /** @type {number[]} */ (matchedByName.get(name)),
473+
};
466474
}),
467-
)
468-
).flat();
475+
);
469476

470477
if (assetsForMinify.length === 0) {
471478
return;

0 commit comments

Comments
 (0)