Skip to content

Commit 43144d2

Browse files
authored
refactor(fileSize): rename showDiff to diff (#6688)
1 parent ba4c73f commit 43144d2

File tree

3 files changed

+26
-33
lines changed

3 files changed

+26
-33
lines changed

packages/core/src/plugins/fileSize.ts

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -104,19 +104,12 @@ const getAssetColor = (size: number) => {
104104
function getHeader(
105105
maxFileLength: number,
106106
maxSizeLength: number,
107-
maxDiffLength: number,
108107
fileHeader: string,
109-
showDiffHeader: boolean,
110108
showGzipHeader: boolean,
111109
) {
112110
const lengths = [maxFileLength, maxSizeLength];
113111
const rowTypes = [fileHeader, 'Size'];
114112

115-
if (showDiffHeader) {
116-
rowTypes.push('Diff');
117-
lengths.push(maxDiffLength);
118-
}
119-
120113
if (showGzipHeader) {
121114
rowTypes.push('Gzip');
122115
}
@@ -169,7 +162,7 @@ async function printFileSizes(
169162
const logs: string[] = [];
170163
const showDetail = options.detail !== false;
171164
let showTotal = options.total !== false;
172-
const showDiff = options.showDiff === true;
165+
const showDiff = options.diff === true;
173166

174167
if (!showTotal && !showDetail) {
175168
return { logs, currentSizes: {} };
@@ -320,14 +313,7 @@ async function printFileSizes(
320313
);
321314

322315
logs.push(
323-
getHeader(
324-
maxFileLength,
325-
maxSizeLength,
326-
0,
327-
fileHeader,
328-
false,
329-
showGzipHeader,
330-
),
316+
getHeader(maxFileLength, maxSizeLength, fileHeader, showGzipHeader),
331317
);
332318

333319
for (const asset of assets) {
@@ -413,31 +399,33 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
413399
name: 'rsbuild:file-size',
414400

415401
setup(api) {
416-
api.onAfterBuild(async ({ stats, environments, isFirstCompile }) => {
402+
api.onAfterBuild(async ({ stats, isFirstCompile }) => {
417403
const { hasErrors } = context.buildState;
418404
// No need to print file sizes if there is any compilation error
419405
if (!stats || hasErrors || !isFirstCompile) {
420406
return;
421407
}
422408

423-
// Check if any environment has showDiff enabled
424-
const hasShowDiff = Object.values(environments).some((environment) => {
409+
// Check if any environment has diff enabled
410+
const showDiff = context.environmentList.some((environment) => {
425411
const { printFileSize } = environment.config.performance;
426-
if (printFileSize === false) return false;
427-
if (printFileSize === true) return false; // uses default (false)
428-
return printFileSize.showDiff === true;
412+
if (typeof printFileSize === 'boolean') {
413+
// uses default (false)
414+
return false;
415+
}
416+
return Boolean(printFileSize.diff);
429417
});
430418

431-
// Load previous build sizes for comparison (only if showDiff is enabled)
432-
const previousSizes = hasShowDiff
419+
// Load previous build sizes for comparison (only if diff is enabled)
420+
const previousSizes = showDiff
433421
? await loadPreviousSizes(api.context.cachePath)
434422
: {};
435423
const newCache: FileSizeCache = {};
436424

437425
const logs: string[] = [];
438426

439427
await Promise.all(
440-
Object.values(environments).map(async (environment, index) => {
428+
context.environmentList.map(async (environment, index) => {
441429
const { printFileSize } = environment.config.performance;
442430

443431
if (printFileSize === false) {
@@ -447,10 +435,9 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
447435
const defaultConfig: PrintFileSizeOptions = {
448436
total: true,
449437
detail: true,
438+
diff: false,
450439
// print compressed size for the browser targets by default
451440
compressed: environment.config.output.target !== 'node',
452-
// disable diff by default to avoid breaking existing output expectations
453-
showDiff: false,
454441
};
455442

456443
const mergedConfig =
@@ -484,7 +471,7 @@ export const pluginFileSize = (context: InternalContext): RsbuildPlugin => ({
484471
logger.log(logs.join('\n'));
485472

486473
// Save current sizes for next build comparison (only if showDiff is enabled)
487-
if (hasShowDiff) {
474+
if (showDiff) {
488475
await saveSizes(api.context.cachePath, newCache);
489476
}
490477
});

packages/core/src/types/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ export type PrintFileSizeOptions = {
691691
* Whether to show file size difference compared to the previous build.
692692
* @default false
693693
*/
694-
showDiff?: boolean;
694+
diff?: boolean;
695695
};
696696

697697
export interface PreconnectOption {

website/docs/en/config/performance/print-file-size.mdx

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ type PrintFileSizeOptions =
1111
compressed?: boolean;
1212
include?: (asset: PrintFileSizeAsset) => boolean;
1313
exclude?: (asset: PrintFileSizeAsset) => boolean;
14-
showDiff?: boolean;
14+
diff?: boolean;
1515
};
1616
```
1717

@@ -33,7 +33,7 @@ The default output log is as follows:
3333
Total: 143.0 kB 46.3 kB
3434
```
3535

36-
When `showDiff` is enabled, size changes are shown inline in parentheses:
36+
When `diff` is enabled, size changes are shown inline in parentheses:
3737

3838
```
3939
File (web) Size Gzip
@@ -250,7 +250,7 @@ export default {
250250
};
251251
```
252252

253-
### showDiff
253+
### diff
254254

255255
- **Type:** `boolean`
256256
- **Default:** `false`
@@ -265,7 +265,7 @@ To enable the diff display:
265265
export default {
266266
performance: {
267267
printFileSize: {
268-
showDiff: true,
268+
diff: true,
269269
},
270270
},
271271
};
@@ -286,3 +286,9 @@ The output will show size changes inline:
286286
- Size decreases are shown in **green** with a `-` prefix
287287
- New files are marked with a **cyan** `(NEW)` label
288288
- Unchanged files don't show any diff
289+
290+
## Version history
291+
292+
| Version | Changes |
293+
| ------- | ------------------- |
294+
| v1.6.13 | Added `diff` option |

0 commit comments

Comments
 (0)