Skip to content

Commit 9a5a873

Browse files
test(javascript): run minified output across terser and swc corpora, and correct what terser gets wrong (#22352)
* test(javascript): run minified output in the byte-identical minifier suite Fold js-minify-stdout into js-minify: each source is held to terser's bytes, and where its test states what the input prints (terser's expect_stdout, swc's expected.stdout and exec.rs) the output is run too. Terser misprints webpack inherits are listed with reasons, and JS_MINIFY_REPORT lists where swc's recorded output is smaller. Claude-Session: https://claude.ai/code/session_016Yz3eH1MRfYQJKa7x4wERK * test(javascript): rename the minifier suite to minify-corpora Claude-Session: https://claude.ai/code/session_016Yz3eH1MRfYQJKa7x4wERK * 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 * test(javascript): run test262 minified by terser and by webpack's printer Production now builds each test262 case once per minimizer: terser as published, and webpack's printer as futureDefaults switches it, in process. What each still fails is listed by reason and must keep failing; the terser mode goes once the printer replaces terser. Claude-Session: https://claude.ai/code/session_016Yz3eH1MRfYQJKa7x4wERK * docs: count swc's three corpora among the minifier's Claude-Session: https://claude.ai/code/session_016Yz3eH1MRfYQJKa7x4wERK * refactor(javascript): keep the correct phase in the printer, its tests in existing suites
1 parent 045d2ab commit 9a5a873

12 files changed

Lines changed: 2536 additions & 441 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.

‎.github/workflows/test.yml‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,12 @@ jobs:
294294
- language: js
295295
submodules: test/external/test262-cases
296296
script: test:test262-parser
297-
# Runs what webpack's minifier writes for each terser case that
298-
# states its stdout. A leg of its own, as it installs phases into
299-
# terser's modules that the js-minify leg must not share.
300-
- language: js-minify-stdout
301-
submodules: test/external/terser
302-
script: test:js-minify-stdout
303297
# Minifies every source of each JS corpus with the published minifier
304-
# and with webpack's own, and requires the same bytes back.
305-
- language: js-minify
298+
# and with webpack's own: the same bytes back, and where a test says
299+
# what its input prints, an output printing it too.
300+
- language: minify-corpora
306301
submodules: test/external/terser test/external/test262-cases test/external/swc
307-
script: test:js-minify
302+
script: test:minify-corpora
308303
runs-on: ubuntu-latest
309304
# Bounded: the js leg allows ten minutes per area per mode, so a stall
310305
# would otherwise hold a runner for the 6h default.

‎AGENTS.md‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ All defined in `package.json` `scripts`.
3333
- `yarn test:unit` — All `*.unittest.js`.
3434
- `yarn test:integration` — Integration suites (`basictest`/`longtest`/`test`).
3535
- `yarn test:test262` / `test:html5lib` / `test:css-parsing` — Spec-conformance suites.
36-
- `yarn test:js-minify` — webpack's JS minifier vs the published one it replaces, byte for byte (errors included), over every JS corpus: terser's own `test/compress` and `test/input`, and test262.
36+
- `yarn test:minify-corpora` — webpack's JS minifier vs the published one it replaces, byte for byte (errors included), over every JS corpus: terser's own `test/compress` and `test/input`, test262, and swc's minifier tests; where a test states what its input prints, the output is also run and must print it. `PHASES=` picks the printer phases installed; `JS_MINIFY_REPORT=<file>` writes where swc's recorded output is smaller.
3737
- `yarn test:syntax-equivalence` — HTML/CSS printers vs a real browser's reading of their output (`configCases`, `wpt`).
3838
- `yarn test:base -u` — Update snapshots (eyeball the diff first).
3939
- `yarn test:size` — Generated-code size over all `configCases/` (per asset, plus runtime modules per runtime).
@@ -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/js-minify.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`; 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`, 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.
@@ -147,16 +147,17 @@ Every schema is derived from the module declaring its options by `generate-schem
147147
- `templates/` — suite drivers (`TestCases.js`, `ConfigTestCases.js`, `HotTestCases.js`, `WatchTestCases.js`); the top-level `*.test.js` / `*.basictest.js` / `*.longtest.js` are thin shims over them. Each exports `describeCases(config)` and resolves case directories against `test/`, never its own. The shims' option sets live together in `templates/variants.js` (re-exported as `variants`) so suite differences read side by side; there's one shim per entry because jest parallelizes per file, not per describe.
148148
- `harness/` — what runs the suites: jest lifecycle (`globalSetup.js`, `globalTeardown.js`, `setupTestFramework.js`), the `patch-node-env.js` environment, the crash reporter, the case `runner/`, the `snapshot/` resolver, and `runtimes/` (preload/setup files for Bun and Deno). Reusable assertions and fixtures go in `helpers/`.
149149
- `RoundTripConfigCases` re-bundles the output of `configCases` that have a `roundTrip.js`.
150-
- `external/` — what webpack doesn't maintain: every git submodule (today the four spec corpora below and terser's own tests) — upstream's to change, ours only to pin. `external/wpt/` (web-platform-tests), checked out one commit deep by `parser (html)` and `syntax-equivalence`, holds the HTML tree-construction corpus since html5lib-tests dropped it.
150+
- `external/` — what webpack doesn't maintain: every git submodule (today the four spec corpora below and terser's and swc's own tests) — upstream's to change, ours only to pin. `external/wpt/` (web-platform-tests), checked out one commit deep by `parser (html)` and `syntax-equivalence`, holds the HTML tree-construction corpus since html5lib-tests dropped it.
151151
- `fixtures/acorn-corpus.json` — acorn's test suite — the one upstream corpus vendored rather than pinned, because acorn's npm tarball ships no tests. `unitCases/WebpackParser.unittest.js` holds both webpack parser entry points to it and owns recording it (no generator script or `package.json` entry): it replays acorn's `test/tests*.js` against a recording driver, keeping sources and options but never expected trees, which come from acorn itself. Bumping the `acorn` devDependency moves the corpus; to refresh, clone acorn at the new version into `node_modules/.cache/acorn-<version>` and re-run with `WEBPACK_UPDATE_ACORN_CORPUS=1`. With that checkout present the run checks the vendored corpus against it; without it (CI, most machines) the corpus stands on the version it names, which the run pins to the installed acorn.
152152

153153
**Git submodules** — all under `test/external/`, checked out on demand: `yarn setup` doesn't fetch them, and each CI job fetches only its own, one commit deep.
154154

155-
- `test/external/test262-cases` — [tc39/test262](https://github.com/tc39/test262); fetched by `test262`, `parser (js)`, `parser (js-minify)`
155+
- `test/external/test262-cases` — [tc39/test262](https://github.com/tc39/test262); fetched by `test262`, `parser (js)`, `parser (minify-corpora)`
156156
- `test/external/html5lib-tests` — [html5lib/html5lib-tests](https://github.com/html5lib/html5lib-tests); fetched by `parser (html)`
157157
- `test/external/wpt` — [web-platform-tests/wpt](https://github.com/web-platform-tests/wpt); fetched by `parser (html)`, `syntax-equivalence` (browsers)
158158
- `test/external/css-parsing-tests` — [CourtBouillon/css-parsing-tests](https://github.com/CourtBouillon/css-parsing-tests); fetched by `parser (css)`
159-
- `test/external/terser` — [terser/terser](https://github.com/terser/terser), pinned to the installed `terser`'s version; fetched by `parser (js-minify)`
159+
- `test/external/terser` — [terser/terser](https://github.com/terser/terser), pinned to the installed `terser`'s version; fetched by `parser (minify-corpora)`
160+
- `test/external/swc` — [swc-project/swc](https://github.com/swc-project/swc), read only under `crates/swc_ecma_minifier/tests`; fetched by `parser (minify-corpora)`
160161

161162
```sh
162163
git submodule update --init --recursive --depth 1 # check out the commits the repo pins

‎TESTING_DOCS.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ Directories come first, in alphabetical order, then the individual files worth t
3535

3636
### `external/`
3737

38-
- **Purpose**: Every git submodule webpack checks out for testing — today the four spec corpora below and terser's own tests. Nothing here is webpack's to edit: each directory belongs to its upstream project, and this repository only pins a commit.
38+
- **Purpose**: Every git submodule webpack checks out for testing — today the four spec corpora below and terser's and swc's own tests. Nothing here is webpack's to edit: each directory belongs to its upstream project, and this repository only pins a commit.
3939

4040
#### `test262-cases/`
4141

4242
- **Purpose**: ECMAScript test262 conformance test cases.
43-
- **Usage**: Git submodule — initialize with `git submodule update --init test/external/test262-cases`. Test runners: `test/specCases/test262.spectest.js`, and `test/specCases/js-minify.spectest.js`, which minifies each test with webpack's printer and with terser.
43+
- **Usage**: Git submodule — initialize with `git submodule update --init test/external/test262-cases`. Test runners: `test/specCases/test262.spectest.js`, which builds and runs each test in development and in production once per JavaScript minimizer — terser, and webpack's printer, as `experiments.futureDefaults` switches it — skipping the `fn-name` cases a renamed binding changes; what each minimizer still fails is listed by reason in its `MINIFIED_FAILURES` table, and a listed case that passes fails the suite until removed. And `test/specCases/minify-corpora.spectest.js`, which minifies each test with webpack's printer and with terser.
4444

4545
#### `html5lib-tests/`
4646

@@ -57,10 +57,15 @@ Directories come first, in alphabetical order, then the individual files worth t
5757
- **Purpose**: CSS Syntax Level 3 conformance corpus for `lib/css/syntax`.
5858
- **Usage**: Git submodule — initialize with `git submodule update --init test/external/css-parsing-tests`. Test runner: `test/specCases/cssParsing-webpack.spectest.js` (`yarn test:css-parsing`) compiles every input as a webpack CSS entry to confirm the full pipeline handles it without crashing.
5959

60+
#### `swc/`
61+
62+
- **Purpose**: swc's repository, read only under `crates/swc_ecma_minifier/tests`: its fixtures with their configs, the tests `exec.rs` and `mangle.rs` write inline, and the libraries it measures itself on. Each is held to terser's bytes like the terser corpora; a test with an `expected.stdout`, and every `exec.rs` test, also has its outputs run, and each fixture's recorded `output.js` is what `JS_MINIFY_REPORT=<file>` compares sizes against.
63+
- **Usage**: Git submodule — initialize with `git submodule update --init --depth 1 test/external/swc`. Test runner: `test/specCases/minify-corpora.spectest.js` (`yarn test:minify-corpora`).
64+
6065
#### `terser/`
6166

62-
- **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.
63-
- **Usage**: Git submodule — initialize with `git submodule update --init --depth 1 test/external/terser`. Test runner: `test/specCases/js-minify.spectest.js` (`yarn test:js-minify`), 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.
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 six corpora `lib/javascript/syntax-printer.js` is held to (test262 and swc's fixtures, `exec.rs` and `mangle.rs` tests are the other four): 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.
68+
- **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.
6469

6570
### `fixtures/`
6671

@@ -102,7 +107,7 @@ Directories come first, in alphabetical order, then the individual files worth t
102107
- `html5lib.spectest.js` — `yarn test:html5lib`
103108
- `syntaxEquivalence.spectest.js` — `yarn test:syntax-equivalence`
104109
- `cssParsing-webpack.spectest.js` — `yarn test:css-parsing`
105-
- `js-minify.spectest.js` — `yarn test:js-minify`
110+
- `minify-corpora.spectest.js` — `yarn test:minify-corpora`
106111

107112
### `statsCases/`
108113

0 commit comments

Comments
 (0)