Skip to content

Commit fef7605

Browse files
authored
fix(fileSize): ensure consistency of output order (#6689)
1 parent 99a75ac commit fef7605

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,17 +71,17 @@ dist/static/js/lib-react.[[hash]].js X.X kB X.X kB
7171
});
7272

7373
expect(extractFileSizeLogs(rsbuild.logs)).toEqual(`
74-
File (node) Size
75-
dist/server/static/image/icon.png X.X kB
76-
dist/server/index.js X.X kB
77-
Total: X.X kB
7874
File (web) Size Gzip
7975
dist/static/css/index.css X.X kB X.X kB
8076
dist/index.html X.X kB X.X kB
8177
dist/static/js/index.js X.X kB X.X kB
8278
dist/static/image/icon.png X.X kB
8379
dist/static/js/lib-react.js X.X kB X.X kB
84-
Total: X.X kB X.X kB`);
80+
Total: X.X kB X.X kB
81+
File (node) Size
82+
dist/server/static/image/icon.png X.X kB
83+
dist/server/index.js X.X kB
84+
Total: X.X kB`);
8585
});
8686

8787
test('should not print logs when printFileSize is false', async ({

packages/core/src/plugins/fileSize.ts

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -406,14 +406,19 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
406406
return;
407407
}
408408

409+
const environments = context.environmentList.filter(
410+
({ config }) => config.performance.printFileSize !== false,
411+
);
412+
413+
// If no environment has printFileSize enabled, skip
414+
if (!environments.length) {
415+
return;
416+
}
417+
409418
// Check if any environment has diff enabled
410-
const showDiff = context.environmentList.some((environment) => {
419+
const showDiff = environments.some((environment) => {
411420
const { printFileSize } = environment.config.performance;
412-
if (typeof printFileSize === 'boolean') {
413-
// uses default (false)
414-
return false;
415-
}
416-
return Boolean(printFileSize.diff);
421+
return typeof printFileSize === 'object' && Boolean(printFileSize.diff);
417422
});
418423

419424
// Load previous build sizes for comparison (only if diff is enabled)
@@ -422,22 +427,15 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
422427
: {};
423428
const newCache: FileSizeCache = {};
424429

425-
const logs: string[] = [];
426-
427-
await Promise.all(
428-
context.environmentList.map(async (environment, index) => {
429-
const { printFileSize } = environment.config.performance;
430-
431-
if (printFileSize === false) {
432-
return;
433-
}
434-
430+
const logs = await Promise.all(
431+
environments.map(async ({ name, index, config, distPath }) => {
432+
const { printFileSize } = config.performance;
435433
const defaultConfig: PrintFileSizeOptions = {
436434
total: true,
437435
detail: true,
438436
diff: false,
439437
// print compressed size for the browser targets by default
440-
compressed: environment.config.output.target !== 'node',
438+
compressed: config.output.target !== 'node',
441439
};
442440

443441
const mergedConfig =
@@ -453,24 +451,26 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
453451
mergedConfig,
454452
statsItem,
455453
api.context.rootPath,
456-
environment.distPath,
457-
environment.name,
454+
distPath,
455+
name,
458456
previousSizes,
459457
);
460458

461-
logs.push(...statsLogs);
462-
463459
// Store current sizes for this environment
464-
newCache[environment.name] = currentSizes;
460+
newCache[name] = currentSizes;
461+
462+
return statsLogs.join('\n');
465463
}),
466464
).catch((err: unknown) => {
467465
logger.warn('Failed to print file size.');
468466
logger.warn(err);
469467
});
470468

471-
logger.log(logs.join('\n'));
469+
if (logs) {
470+
logger.log(logs.join('\n'));
471+
}
472472

473-
// Save current sizes for next build comparison (only if showDiff is enabled)
473+
// Save current sizes for next build comparison (only if diff is enabled)
474474
if (showDiff) {
475475
await saveSizes(api.context.cachePath, newCache);
476476
}

0 commit comments

Comments
 (0)