Skip to content

Commit 0ecf7b6

Browse files
test(javascript): hold the printer to terser's own test corpus (#22307)
* test(javascript): hold the printer to terser's own test corpus * fix(javascript): match terser where the corpora found the printer diverging Leave to terser's parser the sources it reads unlike the spec (escaped keywords, let/yield/await names, import() trailing commas, await before a regexp, get/set fields before a generator, U+1680, with after a line break, export * as a keyword, a default function export read on as a call), key BigInts by their digits, copy module into parse only as terser does, and keep short exported names out of every scope's mangled names. * test(javascript): hold the minifier to terser over every JS corpus Rename the suite to js-minify with a CORPORA list: terser's compress cases and input files, and test262, each under several option sets. * test(javascript): cover the printer's new declines at the end of input
1 parent 29d86ab commit 0ecf7b6

14 files changed

Lines changed: 546 additions & 26 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,11 @@ jobs:
294294
- language: js
295295
submodules: test/external/test262-cases
296296
script: test:test262-parser
297+
# Minifies every source of each JS corpus with the published minifier
298+
# and with webpack's own, and requires the same bytes back.
299+
- language: js-minify
300+
submodules: test/external/terser test/external/test262-cases
301+
script: test:js-minify
297302
runs-on: ubuntu-latest
298303
# Bounded: the js leg allows ten minutes per area per mode, so a stall
299304
# would otherwise hold a runner for the 6h default.

‎.gitmodules‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010
[submodule "test/external/wpt"]
1111
path = test/external/wpt
1212
url = https://github.com/web-platform-tests/wpt.git
13+
[submodule "test/external/terser"]
14+
path = test/external/terser
15+
url = https://github.com/terser/terser

‎.prettierignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ test/**/*.*
2323
test/js/**/*.*
2424
test/external/test262-cases/**/*.*
2525
test/external/wpt/**
26+
test/external/terser/**
2627

2728
# Ignore some folders
2829
benchmark/

‎AGENTS.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +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.
3637
- `yarn test:syntax-equivalence` — HTML/CSS printers vs a real browser's reading of their output (`configCases`, `wpt`).
3738
- `yarn test:base -u` — Update snapshots (eyeball the diff first).
3839
- `yarn test:size` — Generated-code size over all `configCases/` (per asset, plus runtime modules per runtime).
@@ -81,7 +82,7 @@ This is the canonical repository map. **When you add, rename or remove a top-lev
8182
- `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. Who applies a plugin decides its home, not which parser it taps — user-constructed ones live in `lib/define/` and `lib/provide/`.
8283
- `syntax.js` reaches `parser` and `printer` through getters: parsing never loads the printer, and the printer loads the parser only for its `parse` phase.
8384
- `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.
84-
- `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.
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.
8586
- `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.
8687
- `lib/library/` — UMD/AMD/ESM/CommonJS library formats, and the deprecated `LibraryTemplatePlugin` (old two-argument API).
8788
- `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.
@@ -143,22 +144,23 @@ This is the canonical repository map. **When you add, rename or remove a top-lev
143144
- `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.
144145
- `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/`.
145146
- `RoundTripConfigCases` re-bundles the output of `configCases` that have a `roundTrip.js`.
146-
- `external/` — what webpack doesn't maintain: every git submodule (today the four spec corpora below) — 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.
147+
- `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.
147148
- `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.
148149

149150
**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.
150151

151-
- `test/external/test262-cases` — [tc39/test262](https://github.com/tc39/test262); fetched by `test262`, `parser (js)`
152+
- `test/external/test262-cases` — [tc39/test262](https://github.com/tc39/test262); fetched by `test262`, `parser (js)`, `parser (js-minify)`
152153
- `test/external/html5lib-tests` — [html5lib/html5lib-tests](https://github.com/html5lib/html5lib-tests); fetched by `parser (html)`
153154
- `test/external/wpt` — [web-platform-tests/wpt](https://github.com/web-platform-tests/wpt); fetched by `parser (html)`, `syntax-equivalence` (browsers)
154155
- `test/external/css-parsing-tests` — [CourtBouillon/css-parsing-tests](https://github.com/CourtBouillon/css-parsing-tests); fetched by `parser (css)`
156+
- `test/external/terser` — [terser/terser](https://github.com/terser/terser), pinned to the installed `terser`'s version; fetched by `parser (js-minify)`
155157

156158
```sh
157159
git submodule update --init --recursive --depth 1 # check out the commits the repo pins
158160
git submodule update --init --recursive --remote --depth 1 # move every pin to its upstream tip
159161
```
160162

161-
Keep `--depth 1` (`wpt` alone is ~161k files). `--remote` changes the recorded commits, so `git status` shows the four paths modified — commit that only once CI is green on them, or `git submodule update` back to the pins.
163+
Keep `--depth 1` (`wpt` alone is ~161k files). `--remote` changes the recorded commits, so `git status` shows the paths modified — commit that only once CI is green on them, or `git submodule update` back to the pins.
162164

163165
**Examples & changesets**
164166

‎TESTING_DOCS.md‎

Lines changed: 8 additions & 2 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. 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 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 runner: `test/specCases/test262.spectest.js`.
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.
4444

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

@@ -57,6 +57,11 @@ 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+
#### `terser/`
61+
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.
64+
6065
### `fixtures/`
6166

6267
- **Purpose**: Stores sample/mock data used in tests.
@@ -97,6 +102,7 @@ Directories come first, in alphabetical order, then the individual files worth t
97102
- `html5lib.spectest.js` — `yarn test:html5lib`
98103
- `syntaxEquivalence.spectest.js` — `yarn test:syntax-equivalence`
99104
- `cssParsing-webpack.spectest.js` — `yarn test:css-parsing`
105+
- `js-minify.spectest.js` — `yarn test:js-minify`
100106

101107
### `statsCases/`
102108

‎cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@
615615
"test/external/wpt/**",
616616
"test/external/css-parsing-tests/**",
617617
"test/external/html5lib-tests/**",
618+
"test/external/terser/**",
618619
"**/*.svg",
619620
"*.log",
620621
"**/*.wasm",

‎eslint.config.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export default defineConfig([
5252
"test/js/**/*.*",
5353
"test/external/test262-cases/**/*.*",
5454
"test/external/wpt/**/*.*",
55+
"test/external/terser/**",
5556

5657
// Ignore some folders
5758
"benchmark",

0 commit comments

Comments
 (0)