Skip to content

Commit 9512957

Browse files
feat: support function type of performance.printFileSize.total (#5915)
Co-authored-by: Copilot <[email protected]>
1 parent eb2b2d6 commit 9512957

File tree

5 files changed

+144
-23
lines changed

5 files changed

+144
-23
lines changed

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,4 +296,24 @@ dist/static/js/lib-react.[[hash]].js X.X kB X.X kB
296296

297297
await rsbuild.close();
298298
});
299+
300+
test('printFileSize with custom total function should work', async () => {
301+
const rsbuild = await build({
302+
cwd,
303+
rsbuildConfig: {
304+
performance: {
305+
printFileSize: {
306+
total: ({ assets }) => {
307+
return `Generated ${assets.length} files.`;
308+
},
309+
detail: false,
310+
},
311+
},
312+
},
313+
});
314+
315+
await rsbuild.expectLog('Generated 5 files.');
316+
317+
await rsbuild.close();
318+
});
299319
});

packages/core/src/plugins/fileSize.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,20 @@ async function printFileSizes(
187187
: '';
188188
const totalSizeStr = showTotal ? calcFileSize(totalSize) : '';
189189

190+
const getCustomTotal = () => {
191+
if (typeof options.total === 'function') {
192+
return options.total({
193+
assets: assets.map((asset) => ({
194+
name: asset.name,
195+
size: asset.size,
196+
})),
197+
totalSize,
198+
totalGzipSize,
199+
});
200+
}
201+
return null;
202+
};
203+
190204
if (showDetail) {
191205
const maxFileLength = Math.max(
192206
...assets.map((a) => (a.folder + path.sep + a.name).length),
@@ -237,27 +251,42 @@ async function printFileSizes(
237251

238252
if (showTotal) {
239253
logs.push('');
240-
let log = '';
241-
log += ' '.repeat(maxFileLength - totalSizeLabel.length);
242-
log += color.magenta(totalSizeLabel);
243-
log += ` ${totalSizeStr}`;
244254

245-
if (options.compressed) {
246-
const colorFn = getAssetColor(totalGzipSize / assets.length);
247-
log += ' '.repeat(maxSizeLength - totalSizeStr.length);
248-
log += ` ${colorFn(calcFileSize(totalGzipSize))}`;
255+
const customTotal = getCustomTotal();
256+
if (customTotal) {
257+
// Custom total display
258+
logs.push(customTotal);
259+
} else {
260+
// Default total display
261+
let log = '';
262+
log += ' '.repeat(maxFileLength - totalSizeLabel.length);
263+
log += color.magenta(totalSizeLabel);
264+
log += ` ${totalSizeStr}`;
265+
266+
if (options.compressed) {
267+
const colorFn = getAssetColor(totalGzipSize / assets.length);
268+
log += ' '.repeat(maxSizeLength - totalSizeStr.length);
269+
log += ` ${colorFn(calcFileSize(totalGzipSize))}`;
270+
}
271+
272+
logs.push(log);
249273
}
250-
251-
logs.push(log);
252274
}
253275
} else if (showTotal) {
254-
let log = `${color.magenta(totalSizeLabel)} ${totalSizeStr}`;
276+
const customTotal = getCustomTotal();
277+
if (customTotal) {
278+
// Custom total display
279+
logs.push(customTotal);
280+
} else {
281+
// Default total display
282+
let log = `${color.magenta(totalSizeLabel)} ${totalSizeStr}`;
255283

256-
if (options.compressed) {
257-
log += color.green(` (${calcFileSize(totalGzipSize)} gzipped)`);
258-
}
284+
if (options.compressed) {
285+
log += color.green(` (${calcFileSize(totalGzipSize)} gzipped)`);
286+
}
259287

260-
logs.push(log);
288+
logs.push(log);
289+
}
261290
}
262291

263292
logs.push('');

packages/core/src/types/config.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,10 +615,16 @@ export type PrintFileSizeAsset = {
615615

616616
export type PrintFileSizeOptions = {
617617
/**
618-
* Whether to print the total size of all static assets.
618+
* Whether to print the total size of all static assets, or a function to generate custom total output.
619619
* @default true
620620
*/
621-
total?: boolean;
621+
total?:
622+
| boolean
623+
| ((params: {
624+
assets: PrintFileSizeAsset[];
625+
totalSize: number;
626+
totalGzipSize: number;
627+
}) => string);
622628
/**
623629
* Whether to print the size of each static asset.
624630
* @default true

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
type PrintFileSizeOptions =
77
| boolean
88
| {
9-
total?: boolean;
9+
total?: boolean | Function;
1010
detail?: boolean;
1111
compressed?: boolean;
1212
include?: (asset: PrintFileSizeAsset) => boolean;
@@ -50,10 +50,23 @@ You can customize the output format through the options.
5050

5151
### total
5252

53-
- **Type:** `boolean`
53+
- **Type:**
54+
55+
```ts
56+
type Total =
57+
| boolean
58+
| ((params: {
59+
assets: PrintFileSizeAsset[];
60+
totalSize: number;
61+
totalGzipSize: number;
62+
}) => string);
63+
```
64+
5465
- **Default:** `true`
5566

56-
Whether to output the total size of all static assets.
67+
Whether to output the total size of all static assets, or provide a function to customize the output format of the total size.
68+
69+
When set to `false`, the total size will not be output:
5770

5871
```ts
5972
export default {
@@ -69,6 +82,26 @@ export default {
6982
If the current build only generates one static asset, the total size will not be printed.
7083
:::
7184

85+
When set to a function, you can customize the total size output format:
86+
87+
```ts
88+
export default {
89+
performance: {
90+
printFileSize: {
91+
total: ({ assets, totalSize }) => {
92+
return `Generated ${assets.length} files, the total size is ${(totalSize / 1000).toFixed(1)} kB.`;
93+
},
94+
},
95+
},
96+
};
97+
```
98+
99+
Function parameters:
100+
101+
- `assets`: Array of static assets, each containing `name` and `size` properties
102+
- `totalSize`: Total size of all static assets in bytes
103+
- `totalGzipSize`: Total gzip-compressed size of all static assets in bytes
104+
72105
### detail
73106

74107
- **Type:** `boolean`

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

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
type PrintFileSizeOptions =
77
| boolean
88
| {
9-
total?: boolean;
9+
total?: boolean | Function;
1010
detail?: boolean;
1111
compressed?: boolean;
1212
include?: (asset: PrintFileSizeAsset) => boolean;
@@ -50,10 +50,23 @@ export default {
5050

5151
### total
5252

53-
- **类型:** `boolean`
53+
- **类型:**
54+
55+
```ts
56+
type Total =
57+
| boolean
58+
| ((params: {
59+
assets: PrintFileSizeAsset[];
60+
totalSize: number;
61+
totalGzipSize: number;
62+
}) => string);
63+
```
64+
5465
- **默认值:** `true`
5566

56-
是否输出所有静态资源的总体积。
67+
是否输出所有静态资源的总体积,或者提供一个函数来自定义总体积的输出格式。
68+
69+
当设置为 `false` 时,不输出总体积信息:
5770

5871
```ts
5972
export default {
@@ -69,6 +82,26 @@ export default {
6982
如果本次构建只生成了一个静态资源,则不会输出总体积。
7083
:::
7184

85+
当设置为函数时,可以自定义总体积的输出格式:
86+
87+
```ts
88+
export default {
89+
performance: {
90+
printFileSize: {
91+
total: ({ assets, totalSize }) => {
92+
return `Generated ${assets.length} files, the total size is ${(totalSize / 1000).toFixed(1)} kB.`;
93+
},
94+
},
95+
},
96+
};
97+
```
98+
99+
函数参数说明:
100+
101+
- `assets`: 静态资源列表,每个资源包含 `name``size` 属性
102+
- `totalSize`: 所有静态资源的体积
103+
- `totalGzipSize`: 所有静态资源 gzip 压缩后的体积
104+
72105
### detail
73106

74107
- **类型:** `boolean`

0 commit comments

Comments
 (0)