Skip to content

Commit 9df2c4e

Browse files
feat(minify): fold array literals in templates and a regular expression's source
`${[1, 1]}` as `1,1`, and `/a\/b/.source` as the string the engine gives back. Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent b174462 commit 9df2c4e

5 files changed

Lines changed: 76 additions & 13 deletions

File tree

‎lib/javascript/syntax-printer.js‎

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24009,8 +24009,38 @@ const installImprove = (modules) => {
2400924009
});
2401024010
};
2401124011

24012+
/**
24013+
* `/a\/b/i.source` as `"a\\/b"`, the pattern as the engine writes it back.
24014+
* @param {Node} node a property read
24015+
* @param {Compressor} compressor the compressor
24016+
* @returns {Node | undefined} the string, if it reads a literal's `source`
24017+
*/
24018+
const regExpSourceOf = (node, compressor) => {
24019+
if (
24020+
!A.isDotNode(node) ||
24021+
node.optional ||
24022+
node.property.name !== "source" ||
24023+
!A.isRegExpNode(node.object) ||
24024+
isLhs(node, compressor.parent()) ||
24025+
!compressor.option("evaluate")
24026+
) {
24027+
return undefined;
24028+
}
24029+
const { source, flags } = /** @type {RegExp} */ (node.object.value);
24030+
try {
24031+
return makeNode(A.StringNode, node, {
24032+
value: new RegExp(source, flags).source
24033+
});
24034+
} catch (_err) {
24035+
return undefined;
24036+
}
24037+
};
24038+
2401224039
A.wrapOptimizer("Dot", (optimizeDot) => (self, compressor) => {
2401324040
const optimized = optimizeDot(self, compressor);
24041+
const source =
24042+
improvements.enabled && regExpSourceOf(optimized, compressor);
24043+
if (source) return source;
2401424044
if (
2401524045
!improvements.enabled ||
2401624046
!A.isDotNode(optimized) ||
@@ -44500,6 +44530,36 @@ const installValueOptimizers = (modules, helpers) => {
4450044530
return self;
4450144531
});
4450244532

44533+
/**
44534+
* The string an array literal of primitives converts to, once improved.
44535+
* @param {Node} array an array literal
44536+
* @param {Compressor} compressor the compressor
44537+
* @returns {string | Node} the string, or the array where an element is no primitive
44538+
*/
44539+
const joinedLiterals = (array, compressor) => {
44540+
if (modules.improvements === undefined || !modules.improvements.enabled) {
44541+
return array;
44542+
}
44543+
/** @type {string[]} */
44544+
const parts = [];
44545+
for (const element of array.elements) {
44546+
if (element === null) {
44547+
parts.push("");
44548+
continue;
44549+
}
44550+
const value = ast.evaluate(element, compressor);
44551+
if (
44552+
A.isSyntaxNode(value) ||
44553+
(value !== null && typeof value === "object") ||
44554+
typeof value === "function"
44555+
) {
44556+
return array;
44557+
}
44558+
parts.push(value === null || value === undefined ? "" : String(value));
44559+
}
44560+
return parts.join(",");
44561+
};
44562+
4450344563
defineOptimizer("TemplateString", (self, compressor) => {
4450444564
if (
4450544565
!compressor.option("evaluate") ||
@@ -44512,7 +44572,9 @@ const installValueOptimizers = (modules, helpers) => {
4451244572
for (let i = 0; i < self.segments.length; i++) {
4451344573
let segment = self.segments[i];
4451444574
if (A.isSyntaxNode(segment)) {
44515-
const result = ast.evaluate(segment, compressor);
44575+
const result = A.isArrayNode(segment)
44576+
? joinedLiterals(segment, compressor)
44577+
: ast.evaluate(segment, compressor);
4451644578
// A constant no longer than `${segment}`, the 3 being `${}`; a node
4451744579
// always has a segment before and after it.
4451844580
if (

‎test/helpers/oxcSmaller.js‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,7 @@ const OXC_SMALLER_BY_REASON = [
271271
],
272272
[
273273
"oxc evaluates built-in calls and reads on literals at build time",
274-
[
275-
"swc minifier: fixture/issues/7714/1/input.js",
276-
"terser compress: evaluate/regexp_property_eval",
277-
"terser compress: issue-126/concatenate_rhs_strings"
278-
]
274+
["terser compress: issue-126/concatenate_rhs_strings"]
279275
],
280276
[
281277
"oxc writes classes, methods and object literals shorter",

‎test/helpers/swcSmaller.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,6 @@ const SWC_SMALLER_BY_REASON = [
336336
"swc minifier: fixture/issues/12213/numeric-order/input.js",
337337
"swc minifier: fixture/issues/12214/input.js",
338338
"swc minifier: fixture/issues/12224/input.js",
339-
"swc minifier: fixture/issues/7714/1/input.js",
340339
"swc minifier: fixture/next/swc-4559/input.js",
341340
"swc minifier: pass-1/compute/1/input.js",
342341
"swc minifier: pass-default/compute/1/input.js",

0 commit comments

Comments
 (0)