Skip to content

Commit 9831760

Browse files
feat(minify): write strings as templates where every one can be, split the gzip-noise gaps by cause (#22923)
* feat(minify): write every string of a program as a template where each one can be Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test(minify): retire the entries backticks close and split the gzip-noise groups by cause Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * feat(minify): let a function's tail return fall off and keep statements out of for-of heads Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test(minify): update snapshots for backtick strings, retire a stale entry, add changeset Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD * test(minify): snapshot the new printer suites with backticks, keep terser's quotes where compared Claude-Session: https://claude.ai/code/session_01ViD41i2KdwwGxrDk7yrjeD
1 parent 1bac8b0 commit 9831760

12 files changed

Lines changed: 729 additions & 273 deletions

File tree

‎.changeset/068-minify-gzip-gaps.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"webpack": minor
3+
---
4+
5+
Write strings as templates when minifying where every one can be.

‎cspell.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"curlies",
104104
"currentcolor",
105105
"Dani",
106+
"dangi",
106107
"darkblue",
107108
"darkgreen",
108109
"darkred",

‎docs/syntax.md‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎lib/javascript/syntax-printer.js‎

Lines changed: 238 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ const {
8585
* @property {(stream: OutputStream<PrintNode | number>, value: string) => string | undefined} lineBreakTemplateOf a string written as a template where it saves line break escapes
8686
* @property {LineBreakTemplates} lineBreakTemplates what decides and writes the templates `lineBreakTemplateOf` writes, for a printer that decides once per source
8787
* @property {((tree: Node) => LetCounts | null) | undefined} writePendingLets writes the `const` declarations held back as `let`
88+
* @property {((stream: OutputStream<PrintNode | number>, text: string) => string) | undefined} writeBackticks a program's printed text with its strings written as templates, where the improve phase found every one may be
8889
* @property {TerserCompressHelpers["common"]} common terser's `compress/common.js`
8990
* @property {TerserCompressHelpers["inference"]} inference the functions of terser's `compress/inference.js`
9091
* @property {TerserCompressHelpers["flags"]} flags terser's `compress/compressor-flags.js`
@@ -318,7 +319,7 @@ const {
318319

319320
/** @typedef {(sources: MinifySources, options?: MinifyOptions) => Promise<MinifyOutput>} Minify what one call to the minifier takes and answers */
320321

321-
/** @typedef {{ minify: Minify, phases: string[], corrections?: { enabled: boolean }, improvements?: { enabled: boolean, lets: { enabled: boolean }, literalsLast: { enabled: boolean } }, modules: MinifierModules }} Minifier what a caller minifies with, the switches of its corrections and improvements, and the modules it is built from */
322+
/** @typedef {{ minify: Minify, phases: string[], corrections?: { enabled: boolean }, improvements?: { enabled: boolean, lets: { enabled: boolean }, literalsLast: { enabled: boolean }, backticks: { enabled: boolean } }, modules: MinifierModules }} Minifier what a caller minifies with, the switches of its corrections and improvements, and the modules it is built from */
322323

323324
/**
324325
* One step building webpack's minifier, installing what it implements onto
@@ -9905,6 +9906,16 @@ const installMinify = (modules) => {
99059906
// Mangle.
99069907
if (timings) timings.scope = Date.now();
99079908
const printsCode = !hasOwn(options.format, "code") || options.format.code;
9909+
/**
9910+
* @param {OutputStream} printed the stream a program printed into
9911+
* @param {string} text what it wrote
9912+
* @returns {string} the text, its strings as templates where the improve phase writes them so
9913+
*/
9914+
const writeBackticksOf = (printed, text) =>
9915+
// Printed alone, a program prints as a tree printed directly does.
9916+
modules.writeBackticks === undefined || !options.compress
9917+
? text
9918+
: modules.writeBackticks(printed, text);
99089919
// terser's `_destroy_ast`: the tree is dropped once printed.
99099920
const destroysTree = printsCode && !options.format.ast;
99109921
const handsTreeBack = options.format.ast || options.format.spidermonkey;
@@ -9989,7 +10000,7 @@ const installMinify = (modules) => {
998910000
stream.options._destroy_ast = false;
999010001
const printed = modules.printRecording(tree, stream, mangleOptions);
999110002
recorded = printed;
9992-
recordedText = stream.get();
10003+
recordedText = writeBackticksOf(stream, stream.get());
999310004
if (stream.wroteComments) {
999410005
modules.computeEstreeCharFrequency(tree, mangleOptions, letCounts);
999510006
} else {
@@ -10085,7 +10096,10 @@ const installMinify = (modules) => {
1008510096
releases && !options.mangle
1008610097
);
1008710098

10088-
result.code = /** @type {OutputStream} */ (stream).get();
10099+
result.code = writeBackticksOf(
10100+
/** @type {OutputStream} */ (stream),
10101+
/** @type {OutputStream} */ (stream).get()
10102+
);
1008910103
} else {
1009010104
result.code = modules.patchRecording(
1009110105
/** @type {OutputStream} */ (stream),
@@ -21802,7 +21816,9 @@ const unescapeWhitespace = (literal, tabs, lineBreaks) => {
2180221816
const LINE_BREAK_ESCAPES = 1000;
2180321817
const LINE_BREAK_SPACING = 500;
2180421818

21805-
/** @typedef {{ enabled: boolean, lets: { enabled: boolean }, literalsLast: { enabled: boolean } }} Improvements the switch the improvements read, one for writing `const` as `let` alone, and one for writing a literal after what an equality compares it with */
21819+
/** @typedef {{ enabled: boolean, lets: { enabled: boolean }, literalsLast: { enabled: boolean }, backticks: { enabled: boolean } }} Improvements the switch the improvements read, one for writing `const` as `let` alone, one for writing a literal after what an equality compares it with, and one for writing a program's strings as templates */
21820+
21821+
/** @typedef {{ stream: OutputStream<PrintNode | number>, starts: number[], blocked: boolean, own: boolean }} BacktickRecord where a program's strings start in a stream's text, whether one printed where no template of its length may, and whether the improve phase is spelling one */
2180621822

2180721823
/**
2180821824
* Installs webpack's improvements on terser: each writes a program terser
@@ -21819,7 +21835,8 @@ const installImprove = (modules) => {
2181921835
const improvements = {
2182021836
enabled: true,
2182121837
lets: { enabled: true },
21822-
literalsLast: { enabled: true }
21838+
literalsLast: { enabled: true },
21839+
backticks: { enabled: true }
2182321840
};
2182421841
modules.improvements = improvements;
2182521842
// terser leaves `0;` where it drops a write from a function it is done with.
@@ -21839,6 +21856,11 @@ const installImprove = (modules) => {
2183921856

2184021857
const { prototype: output } = modules.MinifiedOutput;
2184121858
const { makeString: makeTerserString } = output;
21859+
// The program printing with its strings written as templates in view.
21860+
/** @type {BacktickRecord | undefined} */
21861+
let backticks;
21862+
/** @type {WeakMap<OutputStream<PrintNode | number>, BacktickRecord>} */
21863+
const backtickRecords = new WeakMap();
2184221864
/**
2184321865
* Whether a string writes its tabs raw: a long one holding several, as a
2184421866
* short one's `\t` gzips better kept, matching the escapes near it.
@@ -21854,6 +21876,14 @@ const installImprove = (modules) => {
2185421876
* @returns {string} the string literal
2185521877
*/
2185621878
output.makeString = function makeString(str, quote) {
21879+
if (
21880+
backticks !== undefined &&
21881+
this === backticks.stream &&
21882+
quote !== "`" &&
21883+
!backticks.own
21884+
) {
21885+
backticks.blocked = true;
21886+
}
2185721887
const literal = makeTerserString.call(this, str, quote);
2185821888
return improvements.enabled && quote !== "`" && writesTabsRaw(str)
2185921889
? unescapeWhitespace(literal, true, false)
@@ -21962,14 +21992,32 @@ const installImprove = (modules) => {
2196221992
);
2196321993
estreeCodegen.Program = (node, stream) => {
2196421994
const outer = rawLineBreaks;
21995+
const outerBackticks = backticks;
2196521996
rawLineBreaks =
2196621997
improvements.enabled &&
2196721998
stream.option("ecma") >= 2015 &&
2196821999
writesLineBreaksRaw(/** @type {Node} */ (node));
22000+
backticks = undefined;
22001+
if (
22002+
improvements.enabled &&
22003+
improvements.backticks.enabled &&
22004+
stream.option("ecma") >= 2015 &&
22005+
stream.option("quote_style") === 0 &&
22006+
// Without semicolons a statement opening with a template would be
22007+
// read as a tag of the line before.
22008+
stream.option("semicolons")
22009+
) {
22010+
backticks = backtickRecords.get(stream);
22011+
if (backticks === undefined) {
22012+
backticks = { stream, starts: [], blocked: false, own: false };
22013+
backtickRecords.set(stream, backticks);
22014+
}
22015+
}
2196922016
try {
2197022017
printProgram(node, stream);
2197122018
} finally {
2197222019
rawLineBreaks = outer;
22020+
backticks = outerBackticks;
2197322021
}
2197422022
};
2197522023
/**
@@ -22042,6 +22090,53 @@ const installImprove = (modules) => {
2204222090
const printLiteral = /** @type {NonNullable<EstreeGenerators["Literal"]>} */ (
2204322091
estreeCodegen.Literal
2204422092
);
22093+
/**
22094+
* Whether a call's arguments are read by Node's `cjs-module-lexer`, which
22095+
* finds a CommonJS module's imports and exports in quoted strings alone.
22096+
* @param {PrintNode} callee the callee
22097+
* @returns {boolean} true for `require` and `Object.defineProperty`
22098+
*/
22099+
const readsQuotedArguments = (callee) =>
22100+
(callee.type === "Identifier" && callee.name === "require") ||
22101+
(callee.type === "MemberExpression" &&
22102+
!callee.computed &&
22103+
callee.object.type === "Identifier" &&
22104+
callee.object.name === "Object" &&
22105+
callee.property.type === "Identifier" &&
22106+
callee.property.name === "defineProperty");
22107+
/**
22108+
* Whether a string printed here may be a template when its program's are:
22109+
* where `takesTemplate` allows one, a member's object or key too.
22110+
* @param {OutputStream} stream the stream printed into
22111+
* @returns {boolean} true when it may
22112+
*/
22113+
const takesBacktick = (stream) => {
22114+
if (stream.in_directive || stream.use_asm) return false;
22115+
const parent = /** @type {PrintNode} */ (stream.parent());
22116+
if (parent.type === "MemberExpression") return true;
22117+
return (
22118+
takesTemplate(stream) &&
22119+
!(
22120+
(parent.type === "CallExpression" || parent.type === "NewExpression") &&
22121+
readsQuotedArguments(parent.callee)
22122+
)
22123+
);
22124+
};
22125+
/**
22126+
* Whether a quoted string reads the same with backticks for its quotes:
22127+
* it holds no quote of its own kind, no backtick and no `${`.
22128+
* @param {string} literal the quoted string
22129+
* @returns {boolean} true when it does
22130+
*/
22131+
const spellsAsTemplate = (literal) => {
22132+
const quote = literal[0];
22133+
return (
22134+
(quote === '"' || quote === "'") &&
22135+
literal.indexOf(quote, 1) === literal.length - 1 &&
22136+
!literal.includes("`") &&
22137+
!literal.includes("${")
22138+
);
22139+
};
2204522140
estreeCodegen.Literal = (node, stream) => {
2204622141
const { value } = node;
2204722142
if (
@@ -22051,10 +22146,73 @@ const installImprove = (modules) => {
2205122146
takesTemplate(stream)
2205222147
) {
2205322148
stream.print(templateOf(stream, value, writesTabsRaw(value)));
22149+
} else if (
22150+
backticks !== undefined &&
22151+
backticks.stream === stream &&
22152+
typeof value === "string" &&
22153+
node.role !== "bigint" &&
22154+
takesBacktick(stream)
22155+
) {
22156+
const record = backticks;
22157+
record.own = true;
22158+
let literal;
22159+
try {
22160+
literal = stream.encode_string(value, node.quote);
22161+
} finally {
22162+
record.own = false;
22163+
}
22164+
stream.print(literal);
22165+
if (spellsAsTemplate(literal)) {
22166+
record.starts.push(stream.writtenLength() - literal.length);
22167+
} else {
22168+
record.blocked = true;
22169+
}
2205422170
} else {
2205522171
printLiteral(node, stream);
2205622172
}
2205722173
};
22174+
// A directive keeps its quotes, and so the program's other strings theirs.
22175+
const printStatement =
22176+
/** @type {NonNullable<EstreeGenerators["ExpressionStatement"]>} */ (
22177+
estreeCodegen.ExpressionStatement
22178+
);
22179+
estreeCodegen.ExpressionStatement = (node, stream) => {
22180+
if (
22181+
backticks !== undefined &&
22182+
backticks.stream === stream &&
22183+
typeof node.directive === "string"
22184+
) {
22185+
backticks.blocked = true;
22186+
}
22187+
printStatement(node, stream);
22188+
};
22189+
/**
22190+
* A program's text with its strings written as templates where every one
22191+
* it prints reads the same so: deflate codes a backtick, beside the
22192+
* lowercase letters, shorter than a double quote.
22193+
* @param {OutputStream<PrintNode | number>} stream the stream it printed into
22194+
* @param {string} text what it wrote
22195+
* @returns {string} the text, its strings written as templates where all may be
22196+
*/
22197+
modules.writeBackticks = (stream, text) => {
22198+
const record = backtickRecords.get(stream);
22199+
if (record === undefined) return text;
22200+
backtickRecords.delete(stream);
22201+
if (record.blocked || record.starts.length === 0) return text;
22202+
let result = "";
22203+
let from = 0;
22204+
for (const start of record.starts) {
22205+
const quote = text[start];
22206+
const end = text.indexOf(quote, start + 1);
22207+
// A start off its string means the text is not the one printed.
22208+
if ((quote !== '"' && quote !== "'") || end === -1 || start < from) {
22209+
return text;
22210+
}
22211+
result += `${text.slice(from, start)}\`${text.slice(start + 1, end)}\``;
22212+
from = end + 1;
22213+
}
22214+
return result + text.slice(from);
22215+
};
2205822216

2205922217
/**
2206022218
* Whether a node reads the `this`, `arguments`, `new.target` or `super` of
@@ -34765,6 +34923,14 @@ const installImprove = (modules) => {
3476534923
alternate: null
3476634924
});
3476734925
}
34926+
const shortened = statements[statements.length - 1];
34927+
if (
34928+
!shortened.alternate &&
34929+
A.isSimpleStatementNode(shortened.consequent) &&
34930+
compressor.option("conditionals")
34931+
) {
34932+
statements[statements.length - 1] = tailIfAsExpression(shortened);
34933+
}
3476834934
return;
3476934935
}
3477034936
if (!A.isReturnNode(last) || !last.argument) return;
@@ -34813,6 +34979,27 @@ const installImprove = (modules) => {
3481334979
return;
3481434980
}
3481534981
if (concise || !A.isConditionalNode(value)) return;
34982+
const whenVoid =
34983+
!isPlainUndefined(value.alternate) &&
34984+
!isPlainUndefined(/** @type {Node} */ (value.consequent))
34985+
? voidOperandOf(/** @type {Node} */ (value.consequent))
34986+
: undefined;
34987+
const otherwiseVoid = whenVoid && voidOperandOf(value.alternate);
34988+
if (whenVoid && otherwiseVoid) {
34989+
// `return c ? void a : void b` falls off either way.
34990+
statements[statements.length - 1] = makeNode(
34991+
A.SimpleStatementNode,
34992+
last,
34993+
{
34994+
expression: makeNode(A.ConditionalNode, value, {
34995+
test: value.test,
34996+
consequent: whenVoid,
34997+
alternate: otherwiseVoid
34998+
})
34999+
}
35000+
);
35001+
return;
35002+
}
3481635003
const whenTrue = isPlainUndefined(value.alternate);
3481735004
if (
3481835005
!whenTrue &&
@@ -34829,6 +35016,45 @@ const installImprove = (modules) => {
3482935016
}),
3483035017
alternate: null
3483135018
});
35019+
// The `return` it holds may now fall off too: `c ? void d() : void 0`.
35020+
shortenTail(statements, concise, compressor);
35021+
};
35022+
35023+
/**
35024+
* What an expression runs once its `undefined` value is not read: `x` for
35025+
* `void x`, `a, x` for `a, void x`.
35026+
* @param {Node} node an expression
35027+
* @returns {Node | undefined} that expression, or undefined where it has a value
35028+
*/
35029+
const voidOperandOf = (node) => {
35030+
if (A.isUnaryPrefixNode(node) && node.operator === "void") {
35031+
return node.argument;
35032+
}
35033+
if (!A.isSequenceNode(node)) return undefined;
35034+
const tail = node.expressions[node.expressions.length - 1];
35035+
return A.isUnaryPrefixNode(tail) && tail.operator === "void"
35036+
? makeSequence(node, [...node.expressions.slice(0, -1), tail.argument])
35037+
: undefined;
35038+
};
35039+
35040+
/**
35041+
* A tail `if (c) e` without `else` as the expression statement terser's
35042+
* `conditionals` writes for it, `c && e`, or `a || e` where `c` is `!a`.
35043+
* @param {Node} statement the `if`
35044+
* @returns {Node} the expression statement
35045+
*/
35046+
const tailIfAsExpression = (statement) => {
35047+
const { test } = statement;
35048+
const negated = isNegation(test);
35049+
return makeNode(A.SimpleStatementNode, statement, {
35050+
expression: associateLeft(
35051+
makeNode(A.BinaryNode, statement, {
35052+
operator: negated ? "||" : "&&",
35053+
left: negated ? test.argument : test,
35054+
right: /** @type {Node} */ (statement.consequent).expression
35055+
})
35056+
)
35057+
});
3483235058
};
3483335059

3483435060
/**
@@ -43747,6 +43973,13 @@ const createTightenBody = (modules) => {
4374743973
}
4374843974
} else if (isForInNode(statement)) {
4374943975
if (
43976+
isForOfNode(statement) &&
43977+
modules.improvements !== undefined &&
43978+
modules.improvements.enabled &&
43979+
!assignsWhatIsRead(previous, statement.right)
43980+
) {
43981+
// Its parens around the sequence cost what the `;` did.
43982+
} else if (
4375043983
!isDefinitionsLikeNode(statement.left) ||
4375143984
isVarNode(statement.left) ||
4375243985
((!isForOfNode(statement) ||
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigCacheTestCases optimization minify-object-enumeration exported tests should enumerate a string's UTF-16 code units 1`] = `"function(){return[[\\"0\\",\\"1\\"],[\\"a\\",\\"b\\"],[[\\"0\\",\\"a\\"]],[\\"\\\\ud800\\"],[],[],[]]}"`;
3+
exports[`ConfigCacheTestCases optimization minify-object-enumeration exported tests should enumerate a string's UTF-16 code units 1`] = `"function(){return[[\`0\`,\`1\`],[\`a\`,\`b\`],[[\`0\`,\`a\`]],[\`\\\\ud800\`],[],[],[]]}"`;
44
5-
exports[`ConfigCacheTestCases optimization minify-object-enumeration exported tests should enumerate array literals without reading holes or length 1`] = `"function(){return[[\\"0\\",\\"2\\"],[10,30],[[\\"0\\",10],[\\"2\\",30]],[],[],[]]}"`;
5+
exports[`ConfigCacheTestCases optimization minify-object-enumeration exported tests should enumerate array literals without reading holes or length 1`] = `"function(){return[[\`0\`,\`2\`],[10,30],[[\`0\`,10],[\`2\`,30]],[],[],[]]}"`;
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
22

3-
exports[`ConfigTestCases optimization minify-object-enumeration exported tests should enumerate a string's UTF-16 code units 1`] = `"function(){return[[\\"0\\",\\"1\\"],[\\"a\\",\\"b\\"],[[\\"0\\",\\"a\\"]],[\\"\\\\ud800\\"],[],[],[]]}"`;
3+
exports[`ConfigTestCases optimization minify-object-enumeration exported tests should enumerate a string's UTF-16 code units 1`] = `"function(){return[[\`0\`,\`1\`],[\`a\`,\`b\`],[[\`0\`,\`a\`]],[\`\\\\ud800\`],[],[],[]]}"`;
44
5-
exports[`ConfigTestCases optimization minify-object-enumeration exported tests should enumerate array literals without reading holes or length 1`] = `"function(){return[[\\"0\\",\\"2\\"],[10,30],[[\\"0\\",10],[\\"2\\",30]],[],[],[]]}"`;
5+
exports[`ConfigTestCases optimization minify-object-enumeration exported tests should enumerate array literals without reading holes or length 1`] = `"function(){return[[\`0\`,\`2\`],[10,30],[[\`0\`,10],[\`2\`,30]],[],[],[]]}"`;

0 commit comments

Comments
 (0)