chore(dev): tweak frontend-js-size.mjs

This commit is contained in:
syuilo 2026-06-23 15:18:09 +09:00
commit 2c814ecd83

View file

@ -71,7 +71,7 @@ function formatColoredDiff(text, diff) {
if (diff === 0) return text; if (diff === 0) return text;
const color = diff > 0 ? 'orange' : 'green'; const color = diff > 0 ? 'orange' : 'green';
const sign = diff > 0 ? '+' : '-'; const sign = diff > 0 ? '+' : '-';
return `$\\color{${color}}{\\text{${sign}${escapeLatex(text).replaceAll('\\%', '\\\\%')}}}$`; return `$\\color{${color}}{\\text{${sign}${escapeLatex(text)}}}$`;
} }
function formatNumberDiff(before, after) { function formatNumberDiff(before, after) {
@ -80,8 +80,9 @@ function formatNumberDiff(before, after) {
return formatColoredDiff(formatNumber(Math.abs(diff)), diff); return formatColoredDiff(formatNumber(Math.abs(diff)), diff);
} }
function formatBytesDiff(diff) { function formatBytesDiff(before, after) {
if (diff == null) return '-'; if (before == null || after == null) return '-';
const diff = after - before;
if (diff === 0) return '0 B'; if (diff === 0) return '0 B';
return formatColoredDiff(formatBytes(Math.abs(diff)), diff); return formatColoredDiff(formatBytes(Math.abs(diff)), diff);
} }
@ -410,16 +411,16 @@ function chunkMarkdownTable(rows, total) {
'| --- | ---: | ---: | ---: | ---: |', '| --- | ---: | ---: | ---: | ---: |',
]; ];
if (total != null) { if (total != null) {
lines.push(`| (total) | ${formatBytes(total.beforeSize)} | ${formatBytes(total.afterSize)} | ${formatBytesDiff(total.afterSize - total.beforeSize)} | ${formatDiffPercent(total.beforeSize, total.afterSize)} |`); lines.push(`| (total) | ${formatBytes(total.beforeSize)} | ${formatBytes(total.afterSize)} | ${formatBytesDiff(total.beforeSize, total.afterSize)} | ${formatDiffPercent(total.beforeSize, total.afterSize)} |`);
lines.push('| | | | | |'); lines.push('| | | | | |');
} }
for (const row of rows) { for (const row of rows) {
if (row.changeType === 'added') { if (row.changeType === 'added') {
lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.afterSize - row.beforeSize)} | $\\color{orange}{\\text{(+)}}$ |`); lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.beforeSize, row.afterSize)} | $\\color{orange}{\\text{(+)}}$ |`);
} else if (row.changeType === 'removed') { } else if (row.changeType === 'removed') {
lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.afterSize - row.beforeSize)} | $\\color{green}{\\text{(-)}}$ |`); lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.beforeSize, row.afterSize)} | $\\color{green}{\\text{(-)}}$ |`);
} else { } else {
lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.afterSize - row.beforeSize)} | ${formatDiffPercent(row.beforeSize, row.afterSize)} |`); lines.push(`| <details><summary>\`${escapeCell(row.name)}\`</summary> \`${escapeCell(row.chunkFile)}\` </details> | ${formatBytes(row.beforeSize)} | ${formatBytes(row.afterSize)} | ${formatBytesDiff(row.beforeSize, row.afterSize)} | ${formatDiffPercent(row.beforeSize, row.afterSize).replaceAll('\\%', '\\\\%')} |`);
} }
} }
return lines.join('\n'); return lines.join('\n');