Skip to content

Commit 772c00c

Browse files
fix(minify): keep reads, quotes, targets and dead zones the new folds missed
- count reads in `export default` and exported declarations before moving an import to `export … from` - keep a quoted imported name quoted in the re-export - write `export * as` only where `ecma` is 2020 or later - keep `[a][0]` as a property target of a destructuring assignment - keep a direct `eval` out of a `for…in`/`for…of` head declaring `let`s Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 9df2c4e commit 772c00c

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

‎.changeset/116-minifier-oxc-gaps.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Minify JavaScript smaller where oxc does.
5+
Reduce JavaScript output size where oxc produces smaller output.

‎lib/javascript/syntax-printer.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30454,9 +30454,10 @@ const installImprove = (modules) => {
3045430454
* from that module: `import{a as b}from"m";export{b as c}` as
3045530455
* `export{a as c}from"m"`, in the import's place, which keeps module order.
3045630456
* @param {Node} toplevel a module
30457+
* @param {boolean} namespaceExports whether the output may use `export * as`
3045730458
* @returns {void}
3045830459
*/
30459-
const exportImportsFrom = (toplevel) => {
30460+
const exportImportsFrom = (toplevel, namespaceExports) => {
3046030461
if (!improvements.enabled || A.usesEval(toplevel)) return;
3046130462
const body = /** @type {Node[]} */ (toplevel.body);
3046230463
/** @type {Map<Definition, number>} */
@@ -30467,7 +30468,9 @@ const installImprove = (modules) => {
3046730468
toplevel,
3046830469
A.createWalker((/** @type {Node} */ node) => {
3046930470
if (A.isExportNode(node)) {
30470-
if (node.source || !node.exported_names) return true;
30471+
if (node.source) return true;
30472+
// An exported value or declaration reads its names as any code does.
30473+
if (!node.exported_names) return;
3047130474
for (const mapping of node.exported_names) {
3047230475
const { definition } = mapping.local;
3047330476
if (!definition) continue;
@@ -30503,17 +30506,19 @@ const installImprove = (modules) => {
3050330506
/**
3050430507
* @param {string} imported the name the module exports
3050530508
* @param {Node[]} mappings the exports of the name bound
30509+
* @param {string=} quote the quote the name is written in, if a string
3050630510
* @returns {void}
3050730511
*/
30508-
const reexport = (imported, mappings) => {
30512+
const reexport = (imported, mappings, quote) => {
3050930513
for (const mapping of mappings) {
3051030514
moved.add(mapping);
3051130515
names.push(
3051230516
makeNode(A.NameMappingNode, mapping, {
3051330517
imported: null,
3051430518
exported: mapping.exported,
3051530519
local: makeNode(A.SymbolExportNode, mapping.local, {
30516-
name: imported
30520+
name: imported,
30521+
quote
3051730522
})
3051830523
})
3051930524
);
@@ -30533,11 +30538,11 @@ const installImprove = (modules) => {
3053330538
const mappings = onlyExported(mapping.local);
3053430539
if (!mappings) return true;
3053530540
if (mapping.imported.name !== "*" || mapping.imported.quote) {
30536-
reexport(mapping.imported.name, mappings);
30541+
reexport(mapping.imported.name, mappings, mapping.imported.quote);
3053730542
return false;
3053830543
}
30539-
// A namespace is exported once per `export * as`.
30540-
if (mappings.length !== 1) return true;
30544+
// A namespace is exported once per `export * as`, from ES2020.
30545+
if (!namespaceExports || mappings.length !== 1) return true;
3054130546
moved.add(mappings[0]);
3054230547
namespaces.push(
3054330548
makeNode(A.ExportNode, declaration, {
@@ -31189,7 +31194,9 @@ const installImprove = (modules) => {
3118931194
A.isPrefixedTemplateStringNode(parent) ||
3119031195
(A.isForInNode(parent) && parent.left === self) ||
3119131196
A.isDestructuringNode(parent) ||
31192-
A.isDefaultAssignNode(parent)
31197+
A.isDefaultAssignNode(parent) ||
31198+
(A.isObjectKeyValNode(parent) &&
31199+
A.isDestructuringNode(compressor.parent(1)))
3119331200
) {
3119431201
return undefined;
3119531202
}
@@ -31790,7 +31797,7 @@ const installImprove = (modules) => {
3179031797
}
3179131798
if (A.isToplevelNode(compressed)) {
3179231799
mergeImports(/** @type {Node[]} */ (compressed.body));
31793-
exportImportsFrom(compressed);
31800+
exportImportsFrom(compressed, this.option("ecma") >= 2020);
3179431801
}
3179531802
if (lets === undefined) return compressed;
3179631803
const pending = lets.finish();
@@ -38093,8 +38100,11 @@ const createTightenBody = (modules) => {
3809338100
walk(left, (/** @type {Node} */ node) => {
3809438101
if (isSymbolDeclarationNode(node)) names.add(node.name);
3809538102
});
38103+
// A direct `eval` may read any of them.
3809638104
return !walk(previous.expression, (/** @type {Node} */ node) =>
38097-
isSymbolRefNode(node) && names.has(node.name) ? walkAbort : undefined
38105+
isSymbolRefNode(node) && (names.has(node.name) || node.name === "eval")
38106+
? walkAbort
38107+
: undefined
3809838108
);
3809938109
};
3810038110

‎test/unitCases/javascript/printer.unittest.js‎

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2079,6 +2079,8 @@ const IMPROVED_CASES = [
20792079
/** @type {[string, string][]} */
20802080
const KEPT_CASES = [
20812081
["a computed key of a loop head's pattern, which the body writes", 'function f(o) { let k = 1; for ({ [k]: x } of o) { console.log(x); k = 2; } } var x; f([{ 1: "a", 2: "b" }, { 1: "c", 2: "d" }]);'],
2082+
["an element of an array literal written through as a property of a destructuring assignment", "var a = 1, value = { x: 5 }; ({ x: [a][0] } = value); console.log(a);"],
2083+
["a direct `eval` kept out of a `for…in` head whose `let` it may read in its dead zone", "function f(o) { let x = 1; eval('console.log(x)'); for (let x in o) console.log(x); } f({ a: 1 });"],
20822084
["an element of an array literal called, read as a place, or beside one with effects", "function f(o, g) { [o][0] = 1; [o][0]++; delete [o][0]; return [[o][0](), [g(), o][1], [, o][0]]; } console.log(f(function () { return this.length; }, () => 1));"],
20832085
["a `break` in a `do … while` that runs once, before a declaration or a labeled `break`", "function f(a) { do { if (a) break; class C {} console.log(C.name); } while (0); x: do { if (a) break x; console.log(2); } while (0); } f(0); f(1);"],
20842086
["a `Map` given what is no entry, or a collection given a spread or an argument more", "function f(a, s) { try { new Map([a]); } catch (e) { console.log(e.name); } try { new Map([, [1]]); } catch (e) { console.log(e.name); } new Set([...s]); new Map([[...s]]); new Set([1], a); } f(1, [2]);"],
@@ -9561,10 +9563,42 @@ describe("syntax-printer", () => {
95619563
const input =
95629564
"import A, { b as c, d } from 'm'; import 'side'; import * as N from 'n' with { type: 'json' }; import * as P from 'p'; import { r } from 'r'; import defer * as D from 'd'; export { A, c as e, d, N as \"n\", P as p1, P as p2, r, D }; export {}; sink(d);";
95639565
/** @type {import("terser").MinifyOptions} */
9564-
const options = { module: true, compress: {}, mangle: false };
9566+
const options = {
9567+
module: true,
9568+
ecma: /** @type {const} */ (2020),
9569+
compress: {},
9570+
mangle: false
9571+
};
95659572
expect((await minify(input, options)).code).toBe(
95669573
'export{default as A,b as e}from"m";import{d}from"m";import"side";export*as"n"from"n"with{type:"json"};import*as P from"p";export{r}from"r";import defer*as D from"d";export{d,P as p1,P as p2,D};export{};sink(d);'
95679574
);
9575+
// `export * as` is ES2020.
9576+
expect(
9577+
(
9578+
await minify("import * as N from 'n'; export { N };", {
9579+
...options,
9580+
ecma: 2015,
9581+
compress: {}
9582+
})
9583+
).code
9584+
).toBe('import*as N from"n";export{N};');
9585+
expect(
9586+
(
9587+
await minify(
9588+
"import { 'foo-bar' as v } from 'm'; export { v };",
9589+
options
9590+
)
9591+
).code
9592+
).toBe('export{"foo-bar"as v}from"m";');
9593+
// `export default` reads the name it exports.
9594+
expect(
9595+
(
9596+
await minify(
9597+
"import { v } from 'm'; export default v; export { v };",
9598+
options
9599+
)
9600+
).code
9601+
).toBe('import{v}from"m";export default v;export{v};');
95689602
expect(
95699603
(
95709604
await minify("import { a } from 'm'; export { a }; eval('a');", options)

0 commit comments

Comments
 (0)