Skip to content

Commit 53647a5

Browse files
committed
ci: Improve size-limit action
1 parent 3fc12c6 commit 53647a5

File tree

1 file changed

+31
-48
lines changed

1 file changed

+31
-48
lines changed

dev-packages/size-limit-gh-action/index.mjs

Lines changed: 31 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,27 @@ class SizeLimit {
3131
return bytes.format(size, { unitSeparator: ' ' });
3232
}
3333

34-
formatTime(seconds) {
35-
if (seconds >= 1) {
36-
return `${Math.ceil(seconds * 10) / 10} s`;
34+
formatPercentageChange(base = 0, current = 0) {
35+
if (base === 0) {
36+
return 'added';
37+
}
38+
39+
if (current === 0) {
40+
return 'removed';
41+
}
42+
43+
const value = ((current - base) / base) * 100;
44+
const formatted = (Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;
45+
46+
if (value > 0) {
47+
return `+${formatted}%`;
3748
}
3849

39-
return `${Math.ceil(seconds * 1000)} ms`;
50+
if (value === 0) {
51+
return '-';
52+
}
53+
54+
return `${formatted}%`;
4055
}
4156

4257
formatChange(base = 0, current = 0) {
@@ -48,75 +63,50 @@ class SizeLimit {
4863
return 'removed';
4964
}
5065

51-
const value = ((current - base) / base) * 100;
52-
const formatted = (Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;
66+
const value = current - base;
67+
const formatted = this.formatBytes(value);
5368

5469
if (value > 0) {
55-
return `+${formatted}% 🔺`;
70+
return `+${formatted} 🔺`;
5671
}
5772

5873
if (value === 0) {
59-
return `${formatted}%`;
74+
return '-';
6075
}
6176

62-
return `${formatted}% 🔽`;
77+
return `${formatted} 🔽`;
6378
}
6479

6580
formatLine(value, change) {
6681
return `${value} (${change})`;
6782
}
6883

6984
formatSizeResult(name, base, current) {
70-
return [name, this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size))];
71-
}
72-
73-
formatTimeResult(name, base, current) {
7485
return [
7586
name,
76-
this.formatLine(this.formatBytes(current.size), this.formatChange(base.size, current.size)),
77-
this.formatLine(this.formatTime(current.loading), this.formatChange(base.loading, current.loading)),
78-
this.formatLine(this.formatTime(current.running), this.formatChange(base.running, current.running)),
79-
this.formatTime(current.total),
87+
this.formatBytes(current.size),
88+
this.formatPercentageChange(base.size, current.size),
89+
this.formatChange(base.size, current.size),
8090
];
8191
}
8292

8393
parseResults(output) {
8494
const results = JSON.parse(output);
8595

8696
return results.reduce((current, result) => {
87-
let time = {};
88-
89-
if (result.loading !== undefined && result.running !== undefined) {
90-
const loading = +result.loading;
91-
const running = +result.running;
92-
93-
time = {
94-
running,
95-
loading,
96-
total: loading + running,
97-
};
98-
}
99-
10097
return {
10198
// biome-ignore lint/performance/noAccumulatingSpread: <explanation>
10299
...current,
103100
[result.name]: {
104101
name: result.name,
105102
size: +result.size,
106-
...time,
107103
},
108104
};
109105
}, {});
110106
}
111107

112108
hasSizeChanges(base, current, threshold = 0) {
113109
const names = [...new Set([...(base ? Object.keys(base) : []), ...Object.keys(current)])];
114-
const isSize = names.some(name => current[name] && current[name].total === undefined);
115-
116-
// Always return true if time results are present
117-
if (!isSize) {
118-
return true;
119-
}
120110

121111
return !!names.find(name => {
122112
const baseResult = base?.[name] || EmptyResult;
@@ -132,16 +122,12 @@ class SizeLimit {
132122

133123
formatResults(base, current) {
134124
const names = [...new Set([...(base ? Object.keys(base) : []), ...Object.keys(current)])];
135-
const isSize = names.some(name => current[name] && current[name].total === undefined);
136-
const header = isSize ? SIZE_RESULTS_HEADER : TIME_RESULTS_HEADER;
125+
const header = SIZE_RESULTS_HEADER;
137126
const fields = names.map(name => {
138127
const baseResult = base?.[name] || EmptyResult;
139128
const currentResult = current[name] || EmptyResult;
140129

141-
if (isSize) {
142-
return this.formatSizeResult(name, baseResult, currentResult);
143-
}
144-
return this.formatTimeResult(name, baseResult, currentResult);
130+
return this.formatSizeResult(name, baseResult, currentResult);
145131
});
146132

147133
return [header, ...fields];
@@ -165,15 +151,12 @@ async function execSizeLimit() {
165151
return { status, output };
166152
}
167153

168-
const SIZE_RESULTS_HEADER = ['Path', 'Size'];
169-
const TIME_RESULTS_HEADER = ['Path', 'Size', 'Loading time (3g)', 'Running time (snapdragon)', 'Total time'];
154+
const SIZE_RESULTS_HEADER = ['Path', 'Size', 'Change'];
170155

171156
const EmptyResult = {
172157
name: '-',
173158
size: 0,
174-
running: 0,
175-
loading: 0,
176-
total: 0,
159+
change: 0,
177160
};
178161

179162
async function run() {

0 commit comments

Comments
 (0)