Skip to content

Commit 3ae040d

Browse files
authored
ci: restore bundle size badges (#1805)
1 parent 8eeaba7 commit 3ae040d

1 file changed

Lines changed: 78 additions & 6 deletions

File tree

.github/scripts/render-bundle-size-comment.js

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,79 @@ function formatDiff(diff, baseValue) {
4949
return `${formatSignedSize(diff)}${percentage}`;
5050
}
5151

52+
const BUNDLE_SIZE_BADGES = {
53+
deleted: ' 🗑️',
54+
new: ' 🆕',
55+
improved: ' 🎉',
56+
investigate: ' 🔍',
57+
};
58+
59+
function getChangeBadge(prValue, baseValue) {
60+
if (prValue == null) {
61+
return BUNDLE_SIZE_BADGES.deleted;
62+
}
63+
64+
if (baseValue == null) {
65+
return BUNDLE_SIZE_BADGES.new;
66+
}
67+
68+
if (baseValue <= 0) {
69+
return '';
70+
}
71+
72+
const percentage = ((prValue - baseValue) / baseValue) * 100;
73+
74+
if (percentage <= -10) {
75+
return BUNDLE_SIZE_BADGES.improved;
76+
}
77+
78+
if (percentage >= 5) {
79+
return BUNDLE_SIZE_BADGES.investigate;
80+
}
81+
82+
return '';
83+
}
84+
85+
function getPackageBadge(prTotal, baseTotal) {
86+
if (baseTotal === 0 && prTotal > 0) {
87+
return BUNDLE_SIZE_BADGES.new;
88+
}
89+
90+
if (baseTotal <= 0) {
91+
return '';
92+
}
93+
94+
const percentage = ((prTotal - baseTotal) / baseTotal) * 100;
95+
96+
if (percentage <= -10) {
97+
return BUNDLE_SIZE_BADGES.improved;
98+
}
99+
100+
if (percentage >= 5) {
101+
return BUNDLE_SIZE_BADGES.investigate;
102+
}
103+
104+
return '';
105+
}
106+
52107
function getPackageName(filePath) {
53108
const match = filePath.match(/^packages\/([^/]+)\//);
54109
return match ? match[1] : '(root)';
55110
}
56111

112+
function escapeHtml(value) {
113+
return value
114+
.replace(/&/g, '&amp;')
115+
.replace(/</g, '&lt;')
116+
.replace(/>/g, '&gt;')
117+
.replace(/\|/g, '&#124;')
118+
.replace(/\r?\n/g, ' ');
119+
}
120+
121+
function formatCode(value) {
122+
return `<code>${escapeHtml(String(value))}</code>`;
123+
}
124+
57125
function getFileLabel(filePath, packageName) {
58126
const packagePrefix = `packages/${packageName}/dist/`;
59127

@@ -110,22 +178,26 @@ const sections = [...filesByPackage.entries()]
110178
0,
111179
);
112180
const packageDiff = packagePrSize - packageBaseSize;
181+
const packageBadge = getPackageBadge(packagePrSize, packageBaseSize);
113182

114183
const rows = files
115184
.map((filePath) => {
185+
const prSize = prSizes[filePath];
186+
const baseSize = baseSizes[filePath];
116187
const fileDiff = (prSizes[filePath] ?? 0) - (baseSizes[filePath] ?? 0);
117-
return `| \`${getFileLabel(filePath, packageName)}\` | ${formatSize(
118-
baseSizes[filePath],
119-
)} | ${formatSize(prSizes[filePath])} | ${formatDiff(
188+
189+
return `| ${formatCode(
190+
getFileLabel(filePath, packageName),
191+
)} | ${formatSize(baseSize)} | ${formatSize(prSize)} | ${formatDiff(
120192
fileDiff,
121-
baseSizes[filePath] ?? 0,
122-
)} |`;
193+
baseSize ?? 0,
194+
)}${getChangeBadge(prSize, baseSize)} |`;
123195
})
124196
.join('\n');
125197

126198
return [
127199
'<details>',
128-
`<summary>\`${packageName}\` - ${formatSize(
200+
`<summary>${formatCode(packageName)}${packageBadge} - ${formatSize(
129201
packageBaseSize,
130202
)} -> ${formatSize(packagePrSize)} (${formatDiff(
131203
packageDiff,

0 commit comments

Comments
 (0)