Skip to content

Commit 1e110ff

Browse files
authored
fix: file size logging when detail is disabled (#5125)
1 parent 31a94f9 commit 1e110ff

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

e2e/cases/print-file-size/basic/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ test.describe('should print file size correctly', async () => {
125125
});
126126

127127
expect(logs.some((log) => log.includes('index.html'))).toBeFalsy();
128-
expect(logs.some((log) => log.includes('Total:'))).toBeTruthy();
128+
expect(logs.some((log) => log.includes('Total size (web):'))).toBeTruthy();
129129
});
130130

131131
test('printFileSize.total: false should work', async () => {

packages/core/src/plugins/fileSize.ts

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,12 @@ async function printFileSizes(
9696
environmentName: string,
9797
) {
9898
const logs: string[] = [];
99-
if (options.detail === false && options.total === false) {
99+
100+
let showTotal = options.total !== false;
101+
const showDetail = options.detail !== false;
102+
const exclude = options.exclude ?? excludeAsset;
103+
104+
if (!showTotal && !showDetail) {
100105
return logs;
101106
}
102107

@@ -142,7 +147,6 @@ async function printFileSizes(
142147
groupAssetsByEmitStatus: false,
143148
});
144149

145-
const exclude = options.exclude ?? excludeAsset;
146150
const filteredAssets = (origin.assets || []).filter((asset) => {
147151
const assetInfo: PrintFileSizeAsset = {
148152
name: asset.name,
@@ -176,44 +180,50 @@ async function printFileSizes(
176180
let totalSize = 0;
177181
let totalGzipSize = 0;
178182

183+
// No need to print total size if there is only one asset and detail is true
184+
showTotal = showTotal && !(showDetail && assets.length === 1);
185+
179186
for (const asset of assets) {
180187
totalSize += asset.size;
181188
if (options.compressed) {
182189
totalGzipSize += asset.gzippedSize ?? asset.size;
183190
}
184191
}
185192

186-
const showTotalSize = options.total !== false && assets.length > 1;
187-
const fileHeader = `File (${environmentName})`;
188-
const totalSizeLabel = showTotalSize ? 'Total:' : '';
189-
const totalSizeStr = showTotalSize ? calcFileSize(totalSize) : '';
190-
191-
const maxFileLength = Math.max(
192-
...assets.map((a) => (a.folder + path.sep + a.name).length),
193-
fileHeader.length,
194-
totalSizeLabel.length,
195-
);
196-
const maxSizeLength = Math.max(
197-
...assets.map((a) => a.sizeLabel.length),
198-
totalSizeStr.length,
199-
);
200-
201-
if (options.detail !== false) {
193+
const fileHeader = showDetail ? `File (${environmentName})` : '';
194+
const totalSizeLabel = showTotal
195+
? showDetail
196+
? 'Total:'
197+
: `Total size (${environmentName}):`
198+
: '';
199+
const totalSizeStr = showTotal ? calcFileSize(totalSize) : '';
200+
201+
if (showDetail) {
202+
const maxFileLength = Math.max(
203+
...assets.map((a) => (a.folder + path.sep + a.name).length),
204+
showTotal ? totalSizeLabel.length : 0,
205+
fileHeader.length,
206+
);
207+
208+
const maxSizeLength = Math.max(
209+
...assets.map((a) => a.sizeLabel.length),
210+
totalSizeStr.length,
211+
);
212+
202213
const showGzipHeader = Boolean(
203214
options.compressed && assets.some((item) => item.gzippedSize !== null),
204215
);
216+
205217
logs.push(
206218
getHeader(maxFileLength, maxSizeLength, fileHeader, showGzipHeader),
207219
);
208-
}
209220

210-
for (const asset of assets) {
211-
let { sizeLabel } = asset;
212-
const { name, folder, gzipSizeLabel } = asset;
213-
const fileNameLength = (folder + path.sep + name).length;
214-
const sizeLength = sizeLabel.length;
221+
for (const asset of assets) {
222+
let { sizeLabel } = asset;
223+
const { name, folder, gzipSizeLabel } = asset;
224+
const fileNameLength = (folder + path.sep + name).length;
225+
const sizeLength = sizeLabel.length;
215226

216-
if (options.detail !== false) {
217227
if (sizeLength < maxSizeLength) {
218228
const rightPadding = ' '.repeat(maxSizeLength - sizeLength);
219229
sizeLabel += rightPadding;
@@ -235,21 +245,27 @@ async function printFileSizes(
235245

236246
logs.push(log);
237247
}
238-
}
239248

240-
// only print total size if there are more than one asset
241-
if (options.total !== false && assets.length > 1) {
242-
logs.push('');
249+
if (showTotal) {
250+
logs.push('');
251+
let log = ' ';
252+
log += ' '.repeat(maxFileLength - totalSizeLabel.length);
253+
log += color.magenta(totalSizeLabel);
254+
log += ` ${totalSizeStr}`;
255+
256+
if (options.compressed) {
257+
const colorFn = getAssetColor(totalGzipSize / assets.length);
258+
log += ' '.repeat(maxSizeLength - totalSizeStr.length);
259+
log += ` ${colorFn(calcFileSize(totalGzipSize))}`;
260+
}
243261

244-
let log = ' ';
245-
log += ' '.repeat(maxFileLength - totalSizeLabel.length);
246-
log += color.magenta(totalSizeLabel);
247-
log += ` ${totalSizeStr}`;
262+
logs.push(log);
263+
}
264+
} else if (showTotal) {
265+
let log = ` ${color.magenta(totalSizeLabel)} ${totalSizeStr}`;
248266

249267
if (options.compressed) {
250-
const colorFn = getAssetColor(totalGzipSize / assets.length);
251-
log += ' '.repeat(maxSizeLength - totalSizeStr.length);
252-
log += ` ${colorFn(calcFileSize(totalGzipSize))}`;
268+
log += color.green(` (${calcFileSize(totalGzipSize)} gzipped)`);
253269
}
254270

255271
logs.push(log);

0 commit comments

Comments
 (0)