Skip to content

Commit 04c6586

Browse files
fix(javascript): correct minifier output terser gets wrong
Add a `correct` printer phase wrapping the terser methods that write a wrong program: `__proto__` shorthands and spreads, lost `this` bindings for calls and tags, `arguments` aliasing, dropped parameter defaults and patterns, classes moved out of reach, and `===` between two calls. The corpora suite now runs every terser stdout case, accepts a byte difference only where the phase makes it, and lists what it fixes. Claude-Session: https://claude.ai/code/session_016Yz3eH1MRfYQJKa7x4wERK
1 parent c9bdc2c commit 04c6586

9 files changed

Lines changed: 721 additions & 36 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+
Fix JavaScript minifier outputs that changed what the program does.

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ This is the canonical repository map. **When you add, rename or remove a top-lev
8282
- `lib/javascript/` — JS parsing (webpack's own parser, ported from acorn), generation, exports analysis, and the always-on, non-public plugins `WebpackOptionsApply` applies unconditionally: `APIPlugin` (`__webpack_require__` and other free variables), `CompatibilityPlugin`, `ConstPlugin`, `ExportsInfoApiPlugin`, `JavascriptMetaInfoPlugin`, `UseStrictPlugin`, `WebpackIsIncludedPlugin` — the `ExportsInfoDependency` and `WebpackIsIncludedDependency` the third and last of those own live here too. `StackedMap` is here too — only the parser's scope chain stacks definitions that way. Who applies a plugin decides its home, not which parser it taps — user-constructed ones live in `lib/define/` and `lib/provide/`.
8383
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser only for its `parse` phase and to read terser's node walks for its `walk` phase.
8484
- `syntax-parser.js` — tokenizer, acorn-derived core and every production in one file (any parse reaches the productions, and the coming struct-of-arrays rewrite moves node creation through them). `regexp.js` (pattern validator) is the one on-demand piece, reached only for a pattern the host engine rejected — never `require` it from a build path.
85-
- `syntax-printer.js` — prints JS back out. `jsMinify.js` (the `minify` the default minimizer dispatches JS to) goes through it when given `printer: true` — which `optimization.minimizeOptions.javascript` defaults to under `experiments.futureDefaults` — and through terser as published otherwise. Its loader reads terser's own sources rather than the published entry, which lets a phase webpack implements replace terser's method. Each phase taken over is one less thing terser does. A phase states what it reads with `supports`; a minifier that moved any of it, or a runtime that can't import those sources, keeps its own. Add phases to the `PHASES` list there and nowhere else, each writing byte-for-byte what it replaced. Its `parse` phase builds terser's tree from `syntax-parser.js` (`createTerserTree`), leaving to terser's parser a source terser reads unlike the spec; `syntax-printer-parse.unittest.js` holds it to terser's parse over acorn's corpus. `test/specCases/minify-corpora.spectest.js` holds the whole printer to terser's output, errors included, over each corpus in its `CORPORA` list (add a new one there) under the option sets in `OPTION_SETS`, and runs each output whose test states what its input prints (terser's `expect_stdout`, swc's `expected.stdout` and `exec.rs`) in terser's sandbox. An output that prints right where terser's misprints is a fix, not a difference; one misprinting exactly as terser's goes in its `INHERITED` table with the reason, and an entry that starts printing right fails until retired. `JS_MINIFY_REPORT=<file>` lists where swc's recorded output is smaller — leads for printing less, never a failure. terser's corpus submodule is pinned to the installed `terser` — bump both together.
85+
- `syntax-printer.js` — prints JS back out. `jsMinify.js` (the `minify` the default minimizer dispatches JS to) goes through it when given `printer: true` — which `optimization.minimizeOptions.javascript` defaults to under `experiments.futureDefaults` — and through terser as published otherwise. Its loader reads terser's own sources rather than the published entry, which lets a phase webpack implements replace terser's method. Each phase taken over is one less thing terser does. A phase states what it reads with `supports`; a minifier that moved any of it, or a runtime that can't import those sources, keeps its own. Add phases to the `PHASES` list there and nowhere else, each writing byte-for-byte what it replaced — except `correct` (`terserCorrections.js`), which wraps the terser methods writing a wrong program and changes only that output, installed only for the terser files whose hashes it names. Its `parse` phase builds terser's tree from `syntax-parser.js` (`createTerserTree`), leaving to terser's parser a source terser reads unlike the spec; `syntax-printer-parse.unittest.js` holds it to terser's parse over acorn's corpus. `test/specCases/minify-corpora.spectest.js` holds the whole printer to terser's output, errors included, over each corpus in its `CORPORA` list (add a new one there) under the option sets in `OPTION_SETS`, and runs each output whose test states what its input prints (terser's `expect_stdout`, swc's `expected.stdout` and `exec.rs`) in terser's sandbox. A byte difference is accepted only where switching the `correct` phase off gives terser's bytes back; what it fixes is listed in `CORRECTED` (printing right with the phase, as terser without), and an output misprinting as terser's by an option's own contract in `INHERITED`, each with the reason — an entry that stops matching fails until retired. `JS_MINIFY_REPORT=<file>` lists where swc's recorded output is smaller — leads for printing less, never a failure. terser's corpus submodule is pinned to the installed `terser` — bump both together.
8686
- `lib/json/` — JSON modules, with the `JsonExportsDependency` naming what one exports, and `jsonMinify`, which strips the whitespace between a JSON asset's tokens for the default minimizer.
8787
- `lib/library/` — UMD/AMD/ESM/CommonJS library formats, and the deprecated `LibraryTemplatePlugin` (old two-argument API).
8888
- `lib/loaders/` — loader execution (vendored loader-runner): pitching/normal iteration and loader loading, plus `LoaderOptionsPlugin` and `LoaderTargetPlugin`, which feed the loader context, and `LoaderPlugin` with the `LoaderDependency`/`LoaderImportDependency` it builds a loader's own imports from.

‎TESTING_DOCS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Directories come first, in alphabetical order, then the individual files worth t
6464

6565
#### `terser/`
6666

67-
- **Purpose**: terser's own repository, pinned to the version webpack depends on. Its `test/compress` cases and `test/input` files are two of the corpora `lib/javascript/syntax-printer.js` is held to (test262 is the third): each source is minified by terser as published and by webpack's printer under several option sets, and the outputs, or the errors, must be byte-for-byte the same. A case stating its `expect_stdout` also has each output run in terser's sandbox, which must print it.
67+
- **Purpose**: terser's own repository, pinned to the version webpack depends on. Its `test/compress` cases and `test/input` files are two of the corpora `lib/javascript/syntax-printer.js` is held to (test262 is the third): each source is minified by terser as published and by webpack's printer under several option sets, and the outputs, or the errors, must be byte-for-byte the same, but for what webpack's `correct` phase fixes. A case stating its `expect_stdout` also has each output run in terser's sandbox, which must print it — or, for `expect_stdout: true`, do whatever its input does, a throw or silence included.
6868
- **Usage**: Git submodule — initialize with `git submodule update --init --depth 1 test/external/terser`. Test runner: `test/specCases/minify-corpora.spectest.js` (`yarn test:minify-corpora`), which also fails when the pin and the installed `terser` disagree, so bumping the dependency means moving the pin with it. A new corpus is one entry in its `CORPORA` list.
6969

7070
### `fixtures/`

‎lib/javascript/syntax-printer.js‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77

88
/* eslint-disable camelcase, new-cap -- terser's own API, which this speaks */
99

10+
const { correctFits, installCorrect } = require("./terserCorrections");
11+
1012
// cspell:ignore DEFMETHOD, Defun, defun, fnames, funarg, classnames, mangleable, unmangleable, NOINLINE, privatename, argnames, thedef, argname, bcatch, bfinally, MANGLEPROP, nlb, punc, Funarg, propmangle, preferrably, nondeferred, fargs
1113

1214
/** @typedef {EXPECTED_ANY} TerserModules terser's own modules */
13-
/** @typedef {{ minify: typeof import("terser").minify, phases: string[] }} Terser what a caller minifies with */
15+
/** @typedef {{ minify: typeof import("terser").minify, phases: string[], corrections?: { enabled: boolean } }} Terser what a caller minifies with, and the switch of its corrections */
1416

1517
/**
1618
* A phase webpack implements in place of terser's. `supports` reads the
@@ -6173,8 +6175,8 @@ const installFrequency = ({ scope, utils }) => {
61736175
};
61746176

61756177
// The phases webpack has taken over. Each one replaces a method on the
6176-
// minifier's own classes and is expected to write exactly what it wrote, only
6177-
// faster; a new phase is added here and nowhere else.
6178+
// minifier's own classes and writes exactly what it wrote, only faster, except
6179+
// `correct`, which changes only what it wrote wrong; add phases here only.
61786180
/** @type {Phase[]} */
61796181
const PHASES = [
61806182
{ name: "walk", supports: walkFits, install: installWalk },
@@ -6186,7 +6188,8 @@ const PHASES = [
61866188
{ name: "frequency", supports: frequencyFits, install: installFrequency },
61876189
{ name: "output", supports: outputFits, install: installOutput },
61886190
{ name: "print", supports: printFits, install: installPrint },
6189-
{ name: "parse", supports: parseFits, install: installParse }
6191+
{ name: "parse", supports: parseFits, install: installParse },
6192+
{ name: "correct", supports: correctFits, install: installCorrect }
61906193
];
61916194

61926195
/** @type {Promise<Terser> | undefined} */
@@ -6270,7 +6273,11 @@ const load = () => {
62706273
phase.install(modules);
62716274
phases.push(phase.name);
62726275
}
6273-
return { minify: modules.minify, phases };
6276+
return {
6277+
minify: modules.minify,
6278+
phases,
6279+
corrections: modules.corrections
6280+
};
62746281
})
62756282
.catch(() => ({ minify: require("terser").minify, phases: [] }));
62766283
return loading;

0 commit comments

Comments
 (0)