Skip to content

Commit 06bda34

Browse files
fix: dedupe extracted comments in linear time (#682)
1 parent 39fd982 commit 06bda34

15 files changed

Lines changed: 400 additions & 262 deletions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"minimizer-webpack-plugin": patch
3+
---
4+
5+
deduplicate extracted comments in linear time, so builds stay fast when an asset contains many distinct preserved comments

.github/workflows/nodejs.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,22 @@ jobs:
118118
if: matrix.node-version == '14.x' || matrix.node-version == '16.x'
119119
run: npm install -D --no-save cssnano@^6 --force
120120

121+
- name: Pin terser to the locked version (Node 10/12/14)
122+
# npm 6 (bundled with Node <= 14) can't read the lockfileVersion 3
123+
# package-lock.json, so the install step re-resolves `terser` to the
124+
# latest match of its range instead of the locked version (and
125+
# rewrites package-lock.json to v1 in the process). A newer terser
126+
# changes the minified output, which breaks the snapshots. Read the
127+
# locked version from the pristine committed lockfile and reinstall
128+
# that exact version so these rows match the snapshots.
129+
if: matrix.node-version == '10.x' || matrix.node-version == '12.x' || matrix.node-version == '14.x'
130+
shell: bash
131+
run: |
132+
git show HEAD:package-lock.json > pristine-package-lock.json
133+
TERSER_VERSION="$(node -e "process.stdout.write(require('./pristine-package-lock.json').packages['node_modules/terser'].version)")"
134+
rm -f pristine-package-lock.json
135+
npm install -D --no-save --ignore-scripts --force "terser@${TERSER_VERSION}"
136+
121137
- name: Install webpack ${{ matrix.webpack-version }}
122138
if: matrix.webpack-version != 'latest'
123139
run: npm i webpack@${{ matrix.webpack-version }}

jest.config.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
// The bundled CSS minimizers (`cssnano@7`, `@swc/css`, `lightningcss`,
22
// `esbuild@0.27`) require modern Node and don't reliably install on the
33
// Windows agents. Skip the dedicated CSS test file outright on rows that
4-
// can't run them so we don't end up with stale or missing snapshots.
4+
// can't run them so we don't end up with stale or missing snapshots. The
5+
// `@swc/html` minimizers require Node >= 14, so the dedicated swc-html file
6+
// is skipped on older Node for the same reason.
57
const NODE_MAJOR = Number(process.versions.node.split(".")[0]);
68
const IS_WINDOWS = process.platform === "win32";
79
const RUN_CSS_TESTS = NODE_MAJOR >= 18 && !IS_WINDOWS;
10+
const RUN_SWC_HTML_TESTS = NODE_MAJOR >= 14;
11+
12+
const testPathIgnorePatterns = [];
13+
14+
if (!RUN_CSS_TESTS) {
15+
testPathIgnorePatterns.push("/test/css-minify-option\\.test\\.js$");
16+
}
17+
18+
if (!RUN_SWC_HTML_TESTS) {
19+
testPathIgnorePatterns.push("/test/swc-html-minify-option\\.test\\.js$");
20+
}
821

922
module.exports = {
1023
testEnvironment: "node",
@@ -13,7 +26,6 @@ module.exports = {
1326
// routinely take longer than that.
1427
testTimeout: 60000,
1528
coveragePathIgnorePatterns: ["src/serialize-javascript.js"],
16-
testPathIgnorePatterns: RUN_CSS_TESTS
17-
? []
18-
: ["/test/css-minify-option\\.test\\.js$"],
29+
snapshotSerializers: ["<rootDir>/test/helpers/snapshotHashSerializer.js"],
30+
testPathIgnorePatterns,
1931
};

package-lock.json

Lines changed: 53 additions & 36 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@
116116
"serialize-javascript": "^7.0.5",
117117
"typescript": "^6.0.3",
118118
"uglify-js": "^3.19.3",
119-
"webpack": "^5.101.0",
119+
"webpack": "^5.107.2",
120120
"webpack-cli": "^4.10.0",
121121
"worker-loader": "^3.0.8"
122122
},

src/utils.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ async function terserMinify(
223223

224224
// Redefine the comments function to extract and preserve
225225
// comments according to the two conditions
226+
const seenComments = new Set(extractedComments);
227+
226228
return (astNode, comment) => {
227229
if (
228230
/** @type {{ extract: ExtractCommentsFunction }} */
@@ -234,7 +236,8 @@ async function terserMinify(
234236
: `//${comment.value}`;
235237

236238
// Don't include duplicate comments
237-
if (!extractedComments.includes(commentText)) {
239+
if (!seenComments.has(commentText)) {
240+
seenComments.add(commentText);
238241
extractedComments.push(commentText);
239242
}
240243
}
@@ -477,6 +480,8 @@ async function uglifyJsMinify(
477480

478481
// Redefine the comments function to extract and preserve
479482
// comments according to the two conditions
483+
const seenComments = new Set(extractedComments);
484+
480485
return (astNode, comment) => {
481486
if (
482487
/** @type {{ extract: ExtractCommentsFunction }} */
@@ -488,7 +493,8 @@ async function uglifyJsMinify(
488493
: `//${comment.value}`;
489494

490495
// Don't include duplicate comments
491-
if (!extractedComments.includes(commentText)) {
496+
if (!seenComments.has(commentText)) {
497+
seenComments.add(commentText);
492498
extractedComments.push(commentText);
493499
}
494500
}

0 commit comments

Comments
 (0)