- `syntax-printer.js` — webpack's JavaScript minifier, terser's design ported to write byte for byte what terser writes; `jsMinify.js` is the `minify` the default minimizer dispatches JS to under `experiments.futureDefaults`, and goes through it alone; without that flag the minimizer dispatches to `terserMinify` as `minimizer-webpack-plugin` publishes it, running the user's own terser since webpack lists it only as a devDependency. Neither needs an option to say which, and the printer itself never loads terser. `createModules` builds what terser's modules exported — the node factories (`createAst`), the parser's tables (`createParseTables`), the compressor and the output stream — and each entry of `PHASES` installs one part onto them: add phases there and nowhere else. A node is a plain object without methods, made by a factory `createAst` compiles from `NODE_KINDS` with its fields; its prototype is its kind's, holding its ESTree `type`, the values ESTree fixes for the kind (`prefix`, a name's `role`, a declaration's `kind`), the kind itself, which `ast.kindOf` reads, and its kind bits. A kind's prototype sits below a frozen prototype per terser class, chained as `NODE_KIND_ANCESTRY` gives each kind's classes, and `createAst` names a predicate per class, `is<Class>` (`isLambdaNode`), a bound `isPrototypeOf` of that class's prototype: the phases ask a node what it is through these, never `instanceof`. There are no node methods: every operation terser defined per class (`optimize`, `evaluate`, `has_side_effects`, `drop_side_effect_free`, `size`, scope analysis, …) is a function on `ast` dispatching on `node.type` through a table, a type absent reading the default, and walkers, transformers and the compressor are plain objects (`createWalker`, `createTransformer`, `createCompressor`), with `ast.walkNode` and `ast.transformNode` visiting children by type. A `pure_funcs` function is still handed a call under terser's field names (`args`, `expression`, `print_to_string`) through a read-only view. A parsed source is adopted from its `SyntaxTree`, reading the columns rather than calling a method per field (`ast.adoptTree`); an ESTree, which a `parse.spidermonkey` input is, through `ast.adoptEstree`, and `ast.toEstree` writes the SpiderMonkey JSON `format.spidermonkey` hands back, as terser's `to_mozilla_ast` did. `correct` wraps the methods with which terser writes a wrong program and changes only that output; `corrections.enabled` switches it off. `improve` writes less where terser's longer program provably runs the same — after compressing, a statement calling a function in place with nothing passed runs its body there, and a call of an exactly specified built-in on literals (`"abc".charAt(1)`, `Math.max(1, 2)`, `parseInt("ff", 16)`, `100 .toString(16)`, `[1].concat([2])`) is replaced by its value, an array without holes as an array literal, and a `Number` constant by its number where shorter, and an expression nobody reads that only reads a built-in global (`JSON`, `Math.PI`, `Object.prototype.toString`), calls one on literals without throwing (`new Set`, `Object.keys([])`, `new RegExp("a", "i")`) or reads a global a `typeof` proved declared (`typeof x != "undefined" && x`) is dropped, and a shorter spelling of the same is written: `Array` and `Object` constructed as literals (`new Array(1, 2)` as `[1,2]`), `new` dropped where the call constructs the same (`Error`, `Function`, `RegExp` passed no regular expression, …, by `CONSTRUCTED_WHEN_CALLED`), a spread array literal passed as its elements, `typeof x == "undefined"` as `typeof x > "u"`, a `+` beside a number literal in arithmetic dropped, `!(a == b)` as `a != b`, `!!` of a boolean dropped and `Boolean(x)` as `!!x`, `??` after a value never `null` or `undefined` dropped, an integer tested against zero as the integer, `===` between numbers as `==`, an array literal or a bigint concatenated as the string it makes, a string literal's character (`"abc"[1]`), `+[]`, `Number`, `String` and `BigInt` of a literal and `toString` of a boolean or a string as their value, `c ? a = x : a = y` as `a = c ? x : y` (a name no `with` reaches, an undeclared one in sloppy code only, a property of `this` where it is bound or of a name nothing reassigns), `(a = b) === null || a === void 0` as `null == (a = b)`, from ECMAScript 2020 `a == null ? void 0 : a.b` as `a?.b` (and `null == (a = b) ? void 0 : a.c` as `(a = b)?.c`), and from ECMAScript 2021 `a || (a = b)` as `a ||= b` (a name or a property of a name assigned, `&&` and `??` alike) and `a = a || b` as `a ||= b` (a `var`, `let` or parameter, which writing back to itself shows nothing), and `const` as `let` where nothing writes the name, below the top level and where no `eval` or `with` reaches (once names are mangled, so the letters mangling counts stay terser's); `drop_unused` also drops from a declared or parameter pattern the names nothing reads (an array's as holes but for the last, an object's where no rest follows and no getter could run: under `pure_getters`, or reading a literal), a declaration whose object pattern then names nothing and reads a literal (an array pattern still looks up its value's iterator), and from ECMAScript 2019 a `catch` binding nothing reads — taking built-ins as present and as the language defines them, as oxc and swc do — and `improvements.enabled` switches it off. The `parse` phase builds the tree from `syntax-parser.js`; `javascript/printer.unittest.js` holds the adopted tree to terser's own conversion, and `PARSED_DIFFERENTLY` in `javascript/printer.unittest.js` lists where the tree it builds prints otherwise than terser's. `test/specCases/minify-corpora.spectest.js` holds the whole printer to terser's output, a refusal matching any refusal, 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` and `improve` phases off gives terser's bytes back, and fails where `improve` writes more than without it, unless the program is shorter and only `const` written as `let` makes it gzip bigger (`improvements.lets.enabled` switches that one off to tell); 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. Where the reference instead refuses a source the spec allows, the parse error it answers with is listed in `REFUSED_BY_REFERENCE` with what makes the source legal; where webpack refuses a source the reference reads, the source is listed in `REFUSED_BY_WEBPACK` with the rule it breaks (a test262 test expecting a parse error needs no entry); where it reads or prints one differently, the case is listed in `REFERENCE_MISPRINTS` with what it gets wrong — a listed one webpack must still run correctly, and one the reference stops differing on fails until retired. Each output is also sized against swc's recorded one, a fixture's `output.js` or, for one of terser's cases, what swc's port of it records (gzip decides, raw breaks a tie): one bigger fails unless `test/helpers/swcSmaller.js` lists it under what swc does that webpack does not yet, and a listed one no longer bigger fails until retired. Under the default minimizer's options each source of every corpus but test262 is also minified by `oxc-minify`, held the same way to `test/helpers/oxcSmaller.js`; an oxc output that misprints is no lead. `JS_MINIFY_REPORT=<file>` tallies where webpack's is worse, the same and better. terser's corpus submodule is pinned to the `terser` devDependency — bump both together, and regenerate `data.js`.
0 commit comments