Skip to content

Commit 70d4ecc

Browse files
feat(javascript): write camelCase minify option names as terser's for terser
Outside `futureDefaults` the terser package minifies JavaScript and reads only its own snake_case names, so `minimizeOptions.javascript` names in camelCase are written as terser's before they reach it. The name table moves to `terser-option-names.js`, which reads them either way, and the default library options use camelCase too. Claude-Session: https://claude.ai/code/session_01Tgxgyb4N35ndP6cC4KcrXC
1 parent 48f5118 commit 70d4ecc

5 files changed

Lines changed: 224 additions & 102 deletions

File tree

‎lib/config/defaults.js‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2538,8 +2538,7 @@ const applyOptimizationDefaults = (
25382538
esmLibrary
25392539
? {
25402540
compress: { passes: 2 },
2541-
// eslint-disable-next-line camelcase -- terser's spelling, as terser reads these outside `futureDefaults`
2542-
format: { preserve_annotations: true }
2541+
format: { preserveAnnotations: true }
25432542
}
25442543
: { compress: { passes: 2 } }
25452544
);
@@ -2573,7 +2572,7 @@ const applyOptimizationDefaults = (
25732572
const minimizeByType = /** @type {OptimizationMinimizeOptions} */ (
25742573
compiler.options.optimization.minimizeOptions
25752574
);
2576-
const javascriptOptions = minimizeByType.javascript || {
2575+
const givenJavascriptOptions = minimizeByType.javascript || {
25772576
compress: { passes: 2 }
25782577
};
25792578
// webpack's printer, held to what terser writes, replaces terser; until
@@ -2585,6 +2584,13 @@ const applyOptimizationDefaults = (
25852584
path: require.resolve("minimizer-webpack-plugin"),
25862585
export: "terserMinify"
25872586
};
2587+
// terser reads only its own snake_case names, which webpack's camelCase
2588+
// ones are written as before they reach it.
2589+
const javascriptOptions = futureDefaults
2590+
? givenJavascriptOptions
2591+
: require("../javascript/terser-option-names").toTerserNames(
2592+
givenJavascriptOptions
2593+
);
25882594

25892595
const { css, html } = compiler.options.experiments;
25902596
const minifyJs = minimizeByType.javascript !== false;

‎lib/javascript/syntax-printer.js‎

Lines changed: 6 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ const {
5353
analyzeMinifierTree,
5454
isMinifierVariable
5555
} = require("./ScopeAnalyzer");
56+
const {
57+
TERSER_ALIASES,
58+
fromTerserNames,
59+
withTerserAliases
60+
} = require("./terser-option-names");
5661

5762
// cspell:ignore Mihai, Bazon, DEFNODE, endpos, endcol, sequencesize, lvalues, binop, loopcontrol, symbolref, funs, jridgewell, DEFMETHOD, Defun, defun, defuns, fnames, funarg, classnames, mangleable, unmangleable, NOINLINE, privatename, argnames, thedef, argname, bcatch, bfinally, MANGLEPROP, nlb, punc, Funarg, propmangle, preferrably, nondeferred, fargs, domprops, noin, Noin, DEFPRINT, defprint, dgimsuyv, nokeyword, etnriaoslucdfmhpgvybkwjxqz, ETNRIAOSLUCDFMHPGVYBKWJXQZ
5863

@@ -237,85 +242,6 @@ class DefaultsError extends Error {
237242
const hasOwn = (object, property) =>
238243
Object.prototype.hasOwnProperty.call(object, property);
239244

240-
/**
241-
* terser's option names that webpack spells in camelCase, by option group.
242-
* Each is read as an alias of its camelCase name, so a terser configuration
243-
* minifies unchanged; where a group names both, the camelCase one wins.
244-
*/
245-
const TERSER_ALIASES = {
246-
minify: ["keep_classnames", "keep_fnames"],
247-
compress: [
248-
"builtins_ecma",
249-
"computed_props",
250-
"dead_code",
251-
"drop_console",
252-
"drop_debugger",
253-
"global_defs",
254-
"join_vars",
255-
"keep_classnames",
256-
"keep_fnames",
257-
"keep_infinity",
258-
"negate_iife",
259-
"pure_conversions",
260-
"pure_funcs",
261-
"side_effects",
262-
"top_retain"
263-
],
264-
mangle: ["keep_classnames", "keep_fnames", "nth_identifier"],
265-
properties: ["keep_quoted", "nth_identifier", "only_annotated", "only_cache"],
266-
format: [
267-
"ascii_only",
268-
"indent_level",
269-
"indent_start",
270-
"inline_script",
271-
"keep_numbers",
272-
"keep_quoted_props",
273-
"max_line_len",
274-
"preserve_annotations",
275-
"quote_keys",
276-
"quote_style",
277-
"wrap_func_args",
278-
"wrap_iife"
279-
],
280-
parse: ["bare_returns", "html5_comments"]
281-
};
282-
283-
/**
284-
* @param {string} name a snake_case name
285-
* @returns {string} the same name in camelCase
286-
*/
287-
const camelCaseOf = (name) =>
288-
name.replace(
289-
/_([a-z\d])/g,
290-
/**
291-
* @param {string} _match an underscore and the character after it
292-
* @param {string} character the character after it
293-
* @returns {string} the character, upper case
294-
*/
295-
(_match, character) => character.toUpperCase()
296-
);
297-
298-
/**
299-
* An option group with each of terser's names it holds read as its camelCase
300-
* name, which wins where the group holds both.
301-
* @template {object} T
302-
* @param {T} group the options, as given
303-
* @param {string[]} aliases terser's names the group may hold
304-
* @returns {T} the group, copied where it held one
305-
*/
306-
const withTerserAliases = (group, aliases) => {
307-
const given = /** @type {Record<string, T[keyof T]>} */ (group);
308-
let renamed = given;
309-
for (const alias of aliases) {
310-
if (!hasOwn(given, alias)) continue;
311-
if (renamed === given) renamed = { ...given };
312-
const name = camelCaseOf(alias);
313-
if (!hasOwn(given, name)) renamed[name] = given[alias];
314-
delete renamed[alias];
315-
}
316-
return /** @type {T} */ (renamed);
317-
};
318-
319245
/**
320246
* terser's `defaults`: the options given, each one missing taken from the defaults.
321247
* @template {object} T
@@ -17009,26 +16935,7 @@ const prepareMinify = (text, options) => {
1700916935
const value = /** @type {MinifyOptions} */ (options)[name];
1701016936
if (value !== undefined) given[name] = value;
1701116937
}
17012-
given = withTerserAliases(given, TERSER_ALIASES.minify);
17013-
for (const group of /** @type {const} */ (["compress", "format", "parse"])) {
17014-
if (given[group] && typeof given[group] === "object") {
17015-
given[group] = withTerserAliases(given[group], TERSER_ALIASES[group]);
17016-
}
17017-
}
17018-
if (given.mangle && typeof given.mangle === "object") {
17019-
const mangle = withTerserAliases(
17020-
/** @type {GivenMangleOptions} */ (given.mangle),
17021-
TERSER_ALIASES.mangle
17022-
);
17023-
const { properties } = mangle;
17024-
given.mangle =
17025-
properties && typeof properties === "object"
17026-
? {
17027-
...mangle,
17028-
properties: withTerserAliases(properties, TERSER_ALIASES.properties)
17029-
}
17030-
: mangle;
17031-
}
16938+
given = fromTerserNames(given);
1703216939
const { parse, format } = /** @type {MinifyOptions} */ (given);
1703316940
if (
1703416941
(parse && parse.spidermonkey) ||
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Alexander Akait @alexander-akait
4+
*/
5+
6+
"use strict";
7+
8+
// cspell:ignore fnames, classnames
9+
10+
/** @typedef {import("./syntax-printer").MinifyOptions} MinifyOptions */
11+
12+
/**
13+
* terser's option names that webpack spells in camelCase, by option group.
14+
* Each is read as an alias of its camelCase name, so a terser configuration
15+
* minifies unchanged; where a group names both, the camelCase one wins.
16+
*/
17+
const TERSER_ALIASES = {
18+
minify: ["keep_classnames", "keep_fnames"],
19+
compress: [
20+
"builtins_ecma",
21+
"computed_props",
22+
"dead_code",
23+
"drop_console",
24+
"drop_debugger",
25+
"global_defs",
26+
"join_vars",
27+
"keep_classnames",
28+
"keep_fnames",
29+
"keep_infinity",
30+
"negate_iife",
31+
"pure_conversions",
32+
"pure_funcs",
33+
"side_effects",
34+
"top_retain"
35+
],
36+
mangle: ["keep_classnames", "keep_fnames", "nth_identifier"],
37+
properties: ["keep_quoted", "nth_identifier", "only_annotated", "only_cache"],
38+
format: [
39+
"ascii_only",
40+
"indent_level",
41+
"indent_start",
42+
"inline_script",
43+
"keep_numbers",
44+
"keep_quoted_props",
45+
"max_line_len",
46+
"preserve_annotations",
47+
"quote_keys",
48+
"quote_style",
49+
"wrap_func_args",
50+
"wrap_iife"
51+
],
52+
parse: ["bare_returns", "html5_comments"]
53+
};
54+
55+
/**
56+
* @param {object} object an object
57+
* @param {PropertyKey} property a key
58+
* @returns {boolean} whether the object has it as its own
59+
*/
60+
const hasOwn = (object, property) =>
61+
Object.prototype.hasOwnProperty.call(object, property);
62+
63+
/**
64+
* @param {string} name a snake_case name
65+
* @returns {string} the same name in camelCase
66+
*/
67+
const camelCaseOf = (name) =>
68+
name.replace(
69+
/_([a-z\d])/g,
70+
/**
71+
* @param {string} _match an underscore and the character after it
72+
* @param {string} character the character after it
73+
* @returns {string} the character, upper case
74+
*/
75+
(_match, character) => character.toUpperCase()
76+
);
77+
78+
/**
79+
* An option group with each name it holds in one spelling written in the
80+
* other; the camelCase one wins where the group holds both.
81+
* @template {object} T
82+
* @param {T} group the options, as given
83+
* @param {string[]} aliases terser's names the group may hold
84+
* @param {boolean} toTerser true to write terser's names, false to read them as camelCase
85+
* @returns {T} the group, copied where it held one
86+
*/
87+
const renameGroup = (group, aliases, toTerser) => {
88+
const given = /** @type {Record<string, T[keyof T]>} */ (group);
89+
let renamed = given;
90+
for (const alias of aliases) {
91+
const name = camelCaseOf(alias);
92+
const from = toTerser ? name : alias;
93+
const to = toTerser ? alias : name;
94+
if (!hasOwn(given, from)) continue;
95+
if (renamed === given) renamed = { ...given };
96+
if (toTerser || !hasOwn(given, to)) renamed[to] = given[from];
97+
delete renamed[from];
98+
}
99+
return /** @type {T} */ (renamed);
100+
};
101+
102+
/**
103+
* An option group with each of terser's names it holds read as its camelCase
104+
* name, which wins where the group holds both.
105+
* @template {object} T
106+
* @param {T} group the options, as given
107+
* @param {string[]} aliases terser's names the group may hold
108+
* @returns {T} the group, copied where it held one
109+
*/
110+
const withTerserAliases = (group, aliases) =>
111+
renameGroup(group, aliases, false);
112+
113+
/**
114+
* Minify options with every group renamed, nested ones included.
115+
* @param {MinifyOptions} options the options, as given
116+
* @param {boolean} toTerser true to write terser's names, false to read them as camelCase
117+
* @returns {MinifyOptions} the options, copied where one was renamed
118+
*/
119+
const renameOptions = (options, toTerser) => {
120+
const renamed = renameGroup(options, TERSER_ALIASES.minify, toTerser);
121+
const result = renamed === options ? { ...options } : renamed;
122+
const { compress, parse, mangle } = result;
123+
if (compress && typeof compress === "object") {
124+
result.compress = renameGroup(compress, TERSER_ALIASES.compress, toTerser);
125+
}
126+
if (parse && typeof parse === "object") {
127+
result.parse = renameGroup(parse, TERSER_ALIASES.parse, toTerser);
128+
}
129+
// `output` is terser's older name for `format`.
130+
if (result.format && typeof result.format === "object") {
131+
result.format = renameGroup(result.format, TERSER_ALIASES.format, toTerser);
132+
}
133+
if (result.output && typeof result.output === "object") {
134+
result.output = renameGroup(result.output, TERSER_ALIASES.format, toTerser);
135+
}
136+
if (mangle && typeof mangle === "object") {
137+
const named = renameGroup(mangle, TERSER_ALIASES.mangle, toTerser);
138+
const { properties } = named;
139+
result.mangle =
140+
properties && typeof properties === "object"
141+
? {
142+
...named,
143+
properties: renameGroup(
144+
properties,
145+
TERSER_ALIASES.properties,
146+
toTerser
147+
)
148+
}
149+
: named;
150+
}
151+
return result;
152+
};
153+
154+
/**
155+
* Minify options with terser's names read as their camelCase ones, as
156+
* webpack's minifier reads them.
157+
* @param {MinifyOptions} options the options, as given
158+
* @returns {MinifyOptions} the options, copied
159+
*/
160+
const fromTerserNames = (options) => renameOptions(options, false);
161+
162+
/**
163+
* Minify options with camelCase names written as terser's, for the terser
164+
* package, which knows only its own spelling.
165+
* @param {MinifyOptions} options the options, as given
166+
* @returns {MinifyOptions} the options, copied
167+
*/
168+
const toTerserNames = (options) => renameOptions(options, true);
169+
170+
module.exports = {
171+
TERSER_ALIASES,
172+
fromTerserNames,
173+
toTerserNames,
174+
withTerserAliases
175+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
it("should keep function and class names, as `keepFnames` and `keepClassnames` ask", () => {
2+
function namedFunction() {
3+
return 1;
4+
}
5+
class NamedClass {}
6+
expect(namedFunction()).toBe(1);
7+
expect(namedFunction.name).toBe("namedFunction");
8+
expect(NamedClass.name).toBe("NamedClass");
9+
});
10+
11+
it("should write only ASCII, as `asciiOnly` asks", () => {
12+
const text = () => "café";
13+
expect(text()).toHaveLength(4);
14+
// A function's text is the code the minifier wrote for it.
15+
expect(text.toString()).not.toMatch(/[^\x00-\x7f]/);
16+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"use strict";
2+
3+
/** @type {import("../../../../").Configuration[]} */
4+
module.exports = [false, true].map((futureDefaults) => ({
5+
// terser minifies outside `futureDefaults`, webpack's minifier within it.
6+
mode: "production",
7+
experiments: { futureDefaults },
8+
optimization: {
9+
minimizer: ["..."],
10+
minimize: {
11+
javascript: {
12+
keepFnames: true,
13+
keepClassnames: true,
14+
format: { asciiOnly: true }
15+
}
16+
}
17+
}
18+
}));

0 commit comments

Comments
 (0)