Skip to content

Commit b7d41ab

Browse files
authored
feat: add printFileSize.include option (#4044)
1 parent e3350e3 commit b7d41ab

File tree

5 files changed

+237
-37
lines changed

5 files changed

+237
-37
lines changed

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ test.describe('should print file size correctly', async () => {
103103
).toBeTruthy();
104104
});
105105

106-
test('printFileSize: false should work', async () => {
106+
test('printFileSize: false should not print logs', async () => {
107107
await build({
108108
cwd,
109109
rsbuildConfig: {
@@ -190,4 +190,38 @@ test.describe('should print file size correctly', async () => {
190190
expect(logs.some((log) => log.includes('Total:'))).toBeTruthy();
191191
expect(logs.some((log) => log.includes('gzip:'))).toBeFalsy();
192192
});
193+
194+
test('should allow to filter assets by name', async () => {
195+
await build({
196+
cwd,
197+
rsbuildConfig: {
198+
performance: {
199+
printFileSize: {
200+
include: (asset) => asset.name.endsWith('.js'),
201+
},
202+
},
203+
},
204+
});
205+
206+
expect(logs.some((log) => log.includes('index.html'))).toBeFalsy();
207+
expect(logs.some((log) => log.includes('.css'))).toBeFalsy();
208+
expect(logs.some((log) => log.includes('.js'))).toBeTruthy();
209+
});
210+
211+
test('should allow to filter assets by size', async () => {
212+
await build({
213+
cwd,
214+
rsbuildConfig: {
215+
performance: {
216+
printFileSize: {
217+
include: (asset) => asset.size > 10 * 1000,
218+
},
219+
},
220+
},
221+
});
222+
223+
expect(logs.some((log) => log.includes('index.html'))).toBeFalsy();
224+
expect(logs.some((log) => log.includes('.js'))).toBeTruthy();
225+
expect(logs.some((log) => log.includes('.css'))).toBeFalsy();
226+
});
193227
});

packages/core/src/plugins/fileSize.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,18 @@ async function printFileSizes(
128128
groupAssetsByEmitStatus: false,
129129
});
130130

131-
const filteredAssets = origin.assets!.filter((asset) =>
132-
filterAsset(asset.name),
133-
);
131+
const filteredAssets = origin.assets!.filter((asset) => {
132+
if (!filterAsset(asset.name)) {
133+
return false;
134+
}
135+
if (options.include) {
136+
return options.include({
137+
name: asset.name,
138+
size: asset.size,
139+
});
140+
}
141+
return true;
142+
});
134143

135144
const distFolder = path.relative(rootPath, distPath);
136145

@@ -244,7 +253,7 @@ export const pluginFileSize = (): RsbuildPlugin => ({
244253

245254
const multiStats = 'stats' in stats ? stats.stats : [stats];
246255

247-
const defaultConfig = {
256+
const defaultConfig: PrintFileSizeOptions = {
248257
total: true,
249258
detail: true,
250259
compressed: true,

packages/core/src/types/config.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,18 @@ export type BuildCacheOptions = {
442442
cacheDigest?: Array<string | undefined>;
443443
};
444444

445+
export type PrintFileSizeAsset = {
446+
/**
447+
* The name of the asset.
448+
* @example 'index.html', 'static/js/index.[hash].js'
449+
*/
450+
name: string;
451+
/**
452+
* The size of the asset in bytes.
453+
*/
454+
size: number;
455+
};
456+
445457
export type PrintFileSizeOptions = {
446458
/**
447459
* Whether to print the total size of all static assets.
@@ -459,6 +471,13 @@ export type PrintFileSizeOptions = {
459471
* @default true
460472
*/
461473
compressed?: boolean;
474+
/**
475+
* A filter function to determine which static assets to print.
476+
* If returned `false`, the static asset will be excluded and not included in the
477+
* total size or detailed size.
478+
* @default undefined
479+
*/
480+
include?: (asset: PrintFileSizeAsset) => boolean;
462481
};
463482

464483
export interface PreconnectOption {

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

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@
66
type PrintFileSizeOptions =
77
| boolean
88
| {
9-
/**
10-
* Whether to print the total size of all static assets.
11-
* @default true
12-
*/
139
total?: boolean;
14-
/**
15-
* Whether to print the size of each static asset.
16-
* @default true
17-
*/
1810
detail?: boolean;
19-
/**
20-
* Whether to print the gzip-compressed size of each static asset.
21-
* @default true
22-
*/
2311
compressed?: boolean;
12+
include?: (asset: PrintFileSizeAsset) => boolean;
2413
};
2514
```
2615

@@ -32,7 +21,7 @@ Whether to print the file sizes after production build.
3221

3322
The default output log is as follows:
3423

35-
```bash
24+
```
3625
File (web) Size Gzip
3726
dist/static/js/lib-react.b0714b60.js 140.4 kB 45.0 kB
3827
dist/static/js/index.f3fde9c7.js 1.9 kB 0.97 kB
@@ -54,11 +43,35 @@ export default {
5443
};
5544
```
5645

57-
## Custom Outputs
46+
## Options
5847

5948
You can customize the output format through the options.
6049

61-
- If you don't need to output the size of each static asset, you can set `detail` to false. In this case, only the total size will be output:
50+
### total
51+
52+
- **Type:** `boolean`
53+
- **Default:** `true`
54+
55+
Whether to output the total size of all static assets.
56+
57+
```ts
58+
export default {
59+
performance: {
60+
printFileSize: {
61+
total: false,
62+
},
63+
},
64+
};
65+
```
66+
67+
### detail
68+
69+
- **Type:** `boolean`
70+
- **Default:** `true`
71+
72+
Whether to output the size of each static asset.
73+
74+
If you don't need to view the size of each static asset, you can set `detail` to false. In this case, only the total size will be output:
6275

6376
```ts
6477
export default {
@@ -70,7 +83,14 @@ export default {
7083
};
7184
```
7285

73-
- If you don't need to output the gzipped size, you can set `compressed` to false. This can save some gzip computation time for large projects:
86+
### compressed
87+
88+
- **Type:** `boolean`
89+
- **Default:** `true`
90+
91+
Whether to output the gzip-compressed size of each static asset.
92+
93+
If you don't need to view the gzipped size, you can set `compressed` to false. This can save some gzip computation time for large projects:
7494

7595
```ts
7696
export default {
@@ -81,3 +101,52 @@ export default {
81101
},
82102
};
83103
```
104+
105+
### include
106+
107+
- **Type:**
108+
109+
```ts
110+
type PrintFileSizeAsset = {
111+
/**
112+
* The name of the static asset.
113+
* @example 'index.html', 'static/js/index.[hash].js'
114+
*/
115+
name: string;
116+
/**
117+
* The size of the static asset in bytes.
118+
*/
119+
size: number;
120+
};
121+
type Include = (asset: PrintFileSizeAsset) => boolean;
122+
```
123+
124+
- **Default:** `undefined`
125+
126+
A filter function to determine which static assets to print.
127+
128+
If returned `false`, the static asset will be excluded and not included in the total size or detailed size.
129+
130+
For example, only output static assets larger than 10kB:
131+
132+
```ts
133+
export default {
134+
performance: {
135+
printFileSize: {
136+
include: (asset) => asset.size > 10 * 1000,
137+
},
138+
},
139+
};
140+
```
141+
142+
Or only output `.js` files:
143+
144+
```ts
145+
export default {
146+
performance: {
147+
printFileSize: {
148+
include: (asset) => /\.js$/.test(asset.name),
149+
},
150+
},
151+
};
152+
```

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

Lines changed: 85 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,10 @@
66
type PrintFileSizeOptions =
77
| boolean
88
| {
9-
/**
10-
* 是否输出所有静态资源的总体积
11-
* @default true
12-
*/
139
total?: boolean;
14-
/**
15-
* 是否输出每个静态资源的体积
16-
* @default true
17-
*/
1810
detail?: boolean;
19-
/**
20-
* 是否输出 gzip 压缩后的体积
21-
* @default true
22-
*/
2311
compressed?: boolean;
12+
include?: (asset: PrintFileSizeAsset) => boolean;
2413
};
2514
```
2615

@@ -32,7 +21,7 @@ type PrintFileSizeOptions =
3221

3322
默认输出的日志如下:
3423

35-
```bash
24+
```
3625
File (web) Size Gzip
3726
dist/static/js/lib-react.b0714b60.js 140.4 kB 45.0 kB
3827
dist/static/js/index.f3fde9c7.js 1.9 kB 0.97 kB
@@ -54,11 +43,35 @@ export default {
5443
};
5544
```
5645

57-
## 自定义输出
46+
## 选项
5847

5948
你可以通过选项来自定义输出的格式。
6049

61-
- 如果不需要输出每个静态资源文件的体积,可以把 `detail` 设置为 false,此时仅输出总体积:
50+
### total
51+
52+
- **类型:** `boolean`
53+
- **默认值:** `true`
54+
55+
是否输出所有静态资源的总体积。
56+
57+
```ts
58+
export default {
59+
performance: {
60+
printFileSize: {
61+
total: false,
62+
},
63+
},
64+
};
65+
```
66+
67+
### detail
68+
69+
- **类型:** `boolean`
70+
- **默认值:** `true`
71+
72+
是否输出每个静态资源的体积。
73+
74+
如果你不需要查看每个静态资源文件的体积,可以把 `detail` 设置为 false,此时仅输出总体积:
6275

6376
```ts
6477
export default {
@@ -70,7 +83,14 @@ export default {
7083
};
7184
```
7285

73-
- 如果不需要输出 gzip 压缩后的体积,可以把 `compressed` 设置为 false,这在大型项目中可以节省一些 gzip 计算的耗时:
86+
### compressed
87+
88+
- **类型:** `boolean`
89+
- **默认值:** `true`
90+
91+
是否输出 gzip 压缩后的体积。
92+
93+
如果你不需要查看 gzip 压缩后的体积,可以把 `compressed` 设置为 false,这在大型项目中可以节省一些 gzip 计算的耗时:
7494

7595
```ts
7696
export default {
@@ -81,3 +101,52 @@ export default {
81101
},
82102
};
83103
```
104+
105+
### include
106+
107+
- **类型:**
108+
109+
```ts
110+
type PrintFileSizeAsset = {
111+
/**
112+
* 静态资源名称
113+
* @example 'index.html', 'static/js/index.[hash].js'
114+
*/
115+
name: string;
116+
/**
117+
* 静态资源体积,单位为 bytes
118+
*/
119+
size: number;
120+
};
121+
type Include = (asset: PrintFileSizeAsset) => boolean;
122+
```
123+
124+
- **默认值:** `undefined`
125+
126+
一个过滤函数,用于确定哪些静态资源需要输出。
127+
128+
如果返回 `false`,则该静态资源将被排除,不会被包含在总体积或详细体积中。
129+
130+
例如,只输出体积大于 10kB 的静态资源:
131+
132+
```ts
133+
export default {
134+
performance: {
135+
printFileSize: {
136+
include: (asset) => asset.size > 10 * 1000,
137+
},
138+
},
139+
};
140+
```
141+
142+
或者只输出 `.js` 文件:
143+
144+
```ts
145+
export default {
146+
performance: {
147+
printFileSize: {
148+
include: (asset) => /\.js$/.test(asset.name),
149+
},
150+
},
151+
};
152+
```

0 commit comments

Comments
 (0)