@@ -20376,10 +20376,46 @@ const installSizeChoice = (modules, improvements) => {
2037620376 // A tree is spent by the first run, and a name cache changed by it.
2037720377 if (!measured) return minifyOnce(files, decided, fsModule);
2037820378 improvements.measured = true;
20379+ // A tree printed alone prints as the first run did, but for what only
20380+ // its source holds: a map's sources, a wrap, or names a cache shares.
20381+ const format = decided && (decided.format || decided.output);
20382+ const reprints =
20383+ decided !== undefined &&
20384+ Boolean(decided.mangle) &&
20385+ !decided.sourceMap &&
20386+ !(/** @type {{ wrap?: string }} */ (decided).wrap) &&
20387+ !(/** @type {{ enclose?: unknown }} */ (decided).enclose) &&
20388+ !decided.output &&
20389+ !(typeof decided.mangle === "object" && decided.mangle.properties) &&
20390+ !(format && (format.ast || format.spidermonkey));
20391+ /**
20392+ * @param {MinifyOptions | undefined} given options to run with
20393+ * @returns {MinifyOptions | undefined} them, handing the tree back where it reprints
20394+ */
20395+ const keepingTree = (given) =>
20396+ reprints && given
20397+ ? { ...given, format: { ...given.format, ast: true } }
20398+ : given;
20399+ /** @type {Node | undefined} */
20400+ let tree;
20401+ /**
20402+ * @param {MinifyOutput} output a run's output
20403+ * @returns {MinifyOutput} it, its tree taken back
20404+ */
20405+ const withoutTree = (output) => {
20406+ if (reprints) {
20407+ const { ast, ...rest } = output;
20408+ tree = /** @type {Node} */ (/** @type {unknown} */ (ast));
20409+ return rest;
20410+ }
20411+ return output;
20412+ };
2037920413 /** @type {{ enabled: boolean }[]} */
2038020414 const off = [];
2038120415 try {
20382- const result = await minifyOnce(files, decided, fsModule);
20416+ const result = withoutTree(
20417+ await minifyOnce(files, keepingTree(decided), fsModule)
20418+ );
2038320419 if (
2038420420 typeof result.code !== "string" ||
2038520421 withoutMapUrl(result.code).length > SIZE_CHOICE_LIMIT
@@ -20402,16 +20438,27 @@ const installSizeChoice = (modules, improvements) => {
2040220438 const bestCode = withoutMapUrl(result.code);
2040320439 let bestSize = gzipSize(bestCode);
2040420440 let bestLength = bestCode.length;
20441+ let bestTree = tree;
2040520442 /**
20406- * Minifies again with the switches as set now, keeping the output if smaller.
20443+ * Minifies again with the switches as set now, keeping the output if
20444+ * smaller: names alone are given anew to the smallest tree compressed.
20445+ * @param {boolean} names whether only the names change
2040720446 * @returns {Promise<boolean>} whether it was kept
2040820447 */
20409- const trial = async () => {
20410- const other = await minifyOnce(
20411- files,
20412- pristine && copyOptions(pristine),
20413- fsModule
20414- );
20448+ const trial = async (names) => {
20449+ const given = pristine && copyOptions(pristine);
20450+ const other =
20451+ names && bestTree !== undefined
20452+ ? await minifyOnce(
20453+ /** @type {MinifySources} */ (
20454+ /** @type {unknown} */ (modules.ast.cloneNode(bestTree, true))
20455+ ),
20456+ { ...given, compress: false },
20457+ fsModule
20458+ )
20459+ : withoutTree(
20460+ await minifyOnce(files, keepingTree(given), fsModule)
20461+ );
2041520462 const otherCode = withoutMapUrl(/** @type {string} */ (other.code));
2041620463 const otherSize = gzipSize(otherCode);
2041720464 if (
@@ -20423,22 +20470,24 @@ const installSizeChoice = (modules, improvements) => {
2042320470 best = other;
2042420471 bestSize = otherSize;
2042520472 bestLength = otherCode.length;
20473+ if (!names) bestTree = tree;
2042620474 return true;
2042720475 };
20428- // A spelling the program never took gives the same output without.
20476+ // A spelling the program never took gives the same output without;
20477+ // the ones only naming change come last, read off the tree kept.
20478+ const naming = [improvements.namesByCount, improvements.namesAtSites];
2042920479 const taken = [
2043020480 improvements.literalsLast,
2043120481 improvements.lets,
20432- improvements.namesByCount,
2043320482 improvements.updates,
20434- improvements.namesAtSites,
2043520483 improvements.sharedGlobals,
20436- improvements.letArguments
20484+ improvements.letArguments,
20485+ ...naming
2043720486 ].filter((spelling) => spelling.enabled && spelling.applied);
2043820487 for (const spelling of taken) {
2043920488 spelling.enabled = false;
2044020489 off.push(spelling);
20441- if (!(await trial())) {
20490+ if (!(await trial(naming.includes(spelling) ))) {
2044220491 spelling.enabled = true;
2044320492 off.pop();
2044420493 }
@@ -20450,7 +20499,7 @@ const installSizeChoice = (modules, improvements) => {
2045020499 spelling.enabled = false;
2045120500 off.push(spelling);
2045220501 }
20453- await trial();
20502+ await trial(kept.every((spelling) => naming.includes(spelling)) );
2045420503 }
2045520504 return best;
2045620505 } finally {
0 commit comments