Skip to content

Commit 7089ed6

Browse files
committed
test(minify): build the oxc and swc gaps through webpack
A config case runs each rewrite through a real production build, with and without mangling, keeping quoted keys and with `side_effects` off, checking what each bundle prints and runs. The minimizer runs in the process so the coverage counts.
1 parent f2b043e commit 7089ed6

4 files changed

Lines changed: 227 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
const log = [];
2+
3+
function record(value) {
4+
log.push(value);
5+
return value;
6+
}
7+
8+
function bigIntPatterns() {
9+
return [String(new RegExp(10n)), String(new RegExp(-10n))];
10+
}
11+
12+
class Keys {
13+
[NaN]() {
14+
return 1;
15+
}
16+
17+
get [null]() {
18+
return 2;
19+
}
20+
21+
static [NaN]() {
22+
return 3;
23+
}
24+
}
25+
26+
class Fields {
27+
[null] = 4;
28+
}
29+
30+
function keyed(o) {
31+
const { [NaN]: a, [null]: b } = o;
32+
return [a, b, Object.keys({ [NaN]: 5, [null]: 6 }).join()];
33+
}
34+
35+
function quoted(x, o) {
36+
return x ? o["read"]() : o.read();
37+
}
38+
39+
function quotedKeys(a, b, value) {
40+
if (a) return { kept: value };
41+
if (b) return { "kept": value };
42+
}
43+
44+
function unreadReturns(y) {
45+
(function () {
46+
try {
47+
throw 1;
48+
} finally {
49+
return 2;
50+
}
51+
})();
52+
(function (x) {
53+
if (x) return 3;
54+
record(x);
55+
})(y);
56+
(() => {
57+
if (record(1)) return 4;
58+
record(2);
59+
})();
60+
record(5),
61+
(function (x) {
62+
if (x) return x;
63+
record(x);
64+
})(y);
65+
void (function (x) {
66+
if (x) return record;
67+
record(6);
68+
})(y);
69+
}
70+
71+
function readReturns(y) {
72+
const assigned = (function (x) {
73+
if (x) return 7;
74+
record(x);
75+
})(y);
76+
const named = (function count(n) {
77+
if (!n) return 2;
78+
return count(n - 1) * 3;
79+
})(1);
80+
const passed = [1].map(function (x) {
81+
if (x) return 8;
82+
record(x);
83+
});
84+
(function (x) {
85+
if (x) return arguments.length;
86+
record(x);
87+
})(y);
88+
const constructed = new (function (x) {
89+
if (x) return { built: 9 };
90+
this.built = 0;
91+
})(y);
92+
let written;
93+
(function (x) {
94+
if (x) return (written = 10);
95+
record(x);
96+
})(y);
97+
return [assigned, named, passed[0], constructed.built, written];
98+
}
99+
100+
function deadZone(y) {
101+
try {
102+
(function (x) {
103+
if (x) return later;
104+
record(x);
105+
})(y);
106+
} catch (err) {
107+
return err.name;
108+
}
109+
let later = 1;
110+
return later;
111+
}
112+
113+
async function asyncReturn(y) {
114+
return (async function (x) {
115+
if (x) return { then: (resolve) => resolve(11) };
116+
await record(x);
117+
})(y);
118+
}
119+
120+
it("should drop `new` before `RegExp` of a BigInt", () => {
121+
expect(bigIntPatterns()).toEqual(["/10/", "/-10/"]);
122+
});
123+
124+
it("should read computed `NaN` and `null` keys as the names they convert to", () => {
125+
expect(new Keys().NaN()).toBe(1);
126+
expect(new Keys().null).toBe(2);
127+
expect(Keys.NaN()).toBe(3);
128+
expect(new Fields().null).toBe(4);
129+
expect(keyed({ NaN: "a", null: "b" })).toEqual(["a", "b", "NaN,null"]);
130+
});
131+
132+
it("should read `a[\"b\"]` as `a.b`", () => {
133+
const o = { read: () => "r" };
134+
expect([quoted(0, o), quoted(1, o)]).toEqual(["r", "r"]);
135+
expect([quotedKeys(1, 0, 1), quotedKeys(0, 1, 2), quotedKeys(0, 0, 3)]).toEqual([
136+
{ kept: 1 },
137+
{ kept: 2 },
138+
undefined
139+
]);
140+
});
141+
142+
it("should run what an unread function called in place runs", () => {
143+
log.length = 0;
144+
unreadReturns(0);
145+
expect(log).toEqual([0, 1, 5, 0, 6]);
146+
log.length = 0;
147+
unreadReturns(1);
148+
expect(log).toEqual([1, 5]);
149+
});
150+
151+
it("should keep what a function called in place returns where it is read", async () => {
152+
expect(readReturns(1)).toEqual([7, 6, 8, 9, 10]);
153+
expect(deadZone(1)).toBe("ReferenceError");
154+
expect(deadZone(0)).toBe(1);
155+
expect(await asyncReturn(1)).toBe(11);
156+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const path = require("path");
5+
6+
// The config keeping quoted keys apart, and the one leaving `side_effects` off.
7+
const KEEPS_QUOTES = 2;
8+
const KEEPS_EFFECTS = 3;
9+
10+
module.exports = {
11+
afterExecute(options) {
12+
for (const [index, config] of options.entries()) {
13+
const code = fs.readFileSync(
14+
path.join(config.output.path, `bundle${index}.js`),
15+
"utf8"
16+
);
17+
expect(code).toContain("RegExp(10n)");
18+
expect(code).not.toContain("new RegExp");
19+
expect(code).toContain("NaN(){");
20+
expect(code).toContain("get null(");
21+
const merged = !/\?\w\.read\(\):\w\.read\(\)/.test(code);
22+
expect(merged).toBe(index !== KEEPS_QUOTES);
23+
expect(code).toContain(
24+
index === KEEPS_EFFECTS ? "finally{return 2}" : "finally{return}"
25+
);
26+
}
27+
}
28+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
// The fixture uses class fields, which Node 12 is the first to parse when the
4+
// harness executes the bundle.
5+
module.exports = function filter() {
6+
try {
7+
eval("class A { [null] = 1; }");
8+
return true;
9+
} catch (_err) {
10+
return false;
11+
}
12+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"use strict";
2+
3+
const MinimizerPlugin = require("minimizer-webpack-plugin");
4+
const jsMinify = require("../../../../lib/javascript/jsMinify");
5+
6+
/** @type {import("terser").MinifyOptions[]} */
7+
const minimizerOptions = [
8+
{ compress: { passes: 2 }, mangle: false },
9+
{},
10+
{ format: { keep_quoted_props: true } },
11+
{ compress: { side_effects: false } }
12+
];
13+
14+
/** @type {import("../../../../").Configuration[]} */
15+
module.exports = minimizerOptions.map((options, index) => ({
16+
mode: "production",
17+
target: ["web", "es2022"],
18+
experiments: { futureDefaults: true },
19+
output: { filename: `bundle${index}.js`, module: false },
20+
optimization: {
21+
minimize: true,
22+
minimizer: [
23+
new MinimizerPlugin({
24+
// Keep coverage counters in the process running the tests.
25+
parallel: false,
26+
minify: jsMinify,
27+
minimizerOptions: options
28+
})
29+
]
30+
}
31+
}));

0 commit comments

Comments
 (0)