|
| 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 | +}; |
0 commit comments