Skip to content

Commit d55869c

Browse files
authored
feat: add printFileSize.exclude option (#4045)
1 parent b7d41ab commit d55869c

File tree

8 files changed

+119
-22
lines changed

8 files changed

+119
-22
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,23 @@ test.describe('should print file size correctly', async () => {
224224
expect(logs.some((log) => log.includes('.js'))).toBeTruthy();
225225
expect(logs.some((log) => log.includes('.css'))).toBeFalsy();
226226
});
227+
228+
test('should allow to custom exclude function', async () => {
229+
await build({
230+
cwd,
231+
rsbuildConfig: {
232+
performance: {
233+
printFileSize: {
234+
exclude: (asset) =>
235+
/\.(?:map|LICENSE\.txt)$/.test(asset.name) ||
236+
/\.html$/.test(asset.name),
237+
},
238+
},
239+
},
240+
});
241+
242+
expect(logs.some((log) => log.includes('index.html'))).toBeFalsy();
243+
expect(logs.some((log) => log.includes('.js'))).toBeTruthy();
244+
expect(logs.some((log) => log.includes('.css'))).toBeTruthy();
245+
});
227246
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: red;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import './App.css';
2+
13
const App = () => <div id="test">Hello Rsbuild!</div>;
24

35
export default App;

packages/core/src/plugins/fileSize.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import zlib from 'node:zlib';
99
import { CSS_REGEX, HTML_REGEX, JS_REGEX } from '../constants';
1010
import { color } from '../helpers';
1111
import { logger } from '../logger';
12-
import type { PrintFileSizeOptions, RsbuildPlugin, Rspack } from '../types';
12+
import type {
13+
PrintFileSizeAsset,
14+
PrintFileSizeOptions,
15+
RsbuildPlugin,
16+
Rspack,
17+
} from '../types';
1318

1419
const gzip = promisify(zlib.gzip);
1520

@@ -18,9 +23,9 @@ async function gzipSize(input: Buffer) {
1823
return data.length;
1924
}
2025

21-
/** Filter source map and license files */
22-
export const filterAsset = (asset: string): boolean =>
23-
!/\.map$/.test(asset) && !/\.LICENSE\.txt$/.test(asset);
26+
/** Exclude source map and license files by default */
27+
export const excludeAsset = (asset: PrintFileSizeAsset): boolean =>
28+
/\.(?:map|LICENSE\.txt)$/.test(asset.name);
2429

2530
const getAssetColor = (size: number) => {
2631
if (size > 300 * 1000) {
@@ -128,15 +133,17 @@ async function printFileSizes(
128133
groupAssetsByEmitStatus: false,
129134
});
130135

136+
const exclude = options.exclude ?? excludeAsset;
131137
const filteredAssets = origin.assets!.filter((asset) => {
132-
if (!filterAsset(asset.name)) {
138+
const assetInfo: PrintFileSizeAsset = {
139+
name: asset.name,
140+
size: asset.size,
141+
};
142+
if (exclude(assetInfo)) {
133143
return false;
134144
}
135145
if (options.include) {
136-
return options.include({
137-
name: asset.name,
138-
size: asset.size,
139-
});
146+
return options.include(assetInfo);
140147
}
141148
return true;
142149
});

packages/core/src/types/config.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,12 @@ export type PrintFileSizeOptions = {
478478
* @default undefined
479479
*/
480480
include?: (asset: PrintFileSizeAsset) => boolean;
481+
/**
482+
* A filter function to exclude static assets from the total size or detailed size.
483+
* If both `include` and `exclude` are set, `exclude` will take precedence.
484+
* @default (asset) => /\.(?:map|LICENSE\.txt)$/.test(asset.name)
485+
*/
486+
exclude?: (asset: PrintFileSizeAsset) => boolean;
481487
};
482488

483489
export interface PreconnectOption {
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { filterAsset } from '../src/plugins/fileSize';
1+
import { excludeAsset } from '../src/plugins/fileSize';
22

33
describe('plugin-file-size', () => {
4-
it('#filterAsset - should filter asset correctly', () => {
5-
expect(filterAsset('dist/a.js')).toBeTruthy();
6-
expect(filterAsset('dist/a.css')).toBeTruthy();
7-
expect(filterAsset('dist/a.js.map')).toBeFalsy();
8-
expect(filterAsset('dist/b.css.map')).toBeFalsy();
9-
expect(filterAsset('dist/a.js.LICENSE.txt')).toBeFalsy();
10-
expect(filterAsset('dist/b.css.LICENSE.txt')).toBeFalsy();
11-
expect(filterAsset('dist/a.png')).toBeTruthy();
4+
it('#excludeAsset - should exclude asset correctly', () => {
5+
expect(excludeAsset({ name: 'dist/a.js', size: 1000 })).toBeFalsy();
6+
expect(excludeAsset({ name: 'dist/a.css', size: 1000 })).toBeFalsy();
7+
expect(excludeAsset({ name: 'dist/a.js.map', size: 1000 })).toBeTruthy();
8+
expect(excludeAsset({ name: 'dist/b.css.map', size: 1000 })).toBeTruthy();
9+
expect(
10+
excludeAsset({ name: 'dist/a.js.LICENSE.txt', size: 1000 }),
11+
).toBeTruthy();
12+
expect(
13+
excludeAsset({ name: 'dist/b.css.LICENSE.txt', size: 1000 }),
14+
).toBeTruthy();
15+
expect(excludeAsset({ name: 'dist/a.png', size: 1000 })).toBeFalsy();
1216
});
1317
});

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,41 @@ export default {
139139
};
140140
```
141141

142-
Or only output `.js` files:
142+
Or only output `.js` files larger than 10kB:
143143

144144
```ts
145145
export default {
146146
performance: {
147147
printFileSize: {
148-
include: (asset) => /\.js$/.test(asset.name),
148+
include: (asset) => /\.js$/.test(asset.name) && asset.size > 10 * 1000,
149+
},
150+
},
151+
};
152+
```
153+
154+
### exclude
155+
156+
- **Type:**
157+
158+
```ts
159+
type Exclude = (asset: PrintFileSizeAsset) => boolean;
160+
```
161+
162+
- **Default:** `(asset) => /\.(?:map|LICENSE\.txt)$/.test(asset.name)`
163+
164+
A filter function to determine which static assets to exclude. If both `include` and `exclude` are set, `exclude` will take precedence.
165+
166+
Rsbuild defaults to excluding source map and license files, as these files do not affect page load performance.
167+
168+
For example, exclude `.html` files in addition to the default:
169+
170+
```ts
171+
export default {
172+
performance: {
173+
printFileSize: {
174+
exclude: (asset) =>
175+
/\.(?:map|LICENSE\.txt)$/.test(asset.name) ||
176+
/\.html$/.test(asset.name),
149177
},
150178
},
151179
};

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,41 @@ export default {
139139
};
140140
```
141141

142-
或者只输出 `.js` 文件:
142+
或者只输出体积大于 10kB 的 `.js` 文件:
143143

144144
```ts
145145
export default {
146146
performance: {
147147
printFileSize: {
148-
include: (asset) => /\.js$/.test(asset.name),
148+
include: (asset) => /\.js$/.test(asset.name) && asset.size > 10 * 1000,
149+
},
150+
},
151+
};
152+
```
153+
154+
### exclude
155+
156+
- **类型:**
157+
158+
```ts
159+
type Exclude = (asset: PrintFileSizeAsset) => boolean;
160+
```
161+
162+
- **默认值:** `(asset) => /\.(?:map|LICENSE\.txt)$/.test(asset.name)`
163+
164+
一个过滤函数,用于确定哪些静态资源需要被排除。如果同时设置了 `include``exclude`,则 `exclude` 优先级更高。
165+
166+
Rsbuild 默认排除 source map 和 license 文件,因为这些文件不会影响页面加载的性能。
167+
168+
例如,额外再排除 `.html` 文件:
169+
170+
```ts
171+
export default {
172+
performance: {
173+
printFileSize: {
174+
exclude: (asset) =>
175+
/\.(?:map|LICENSE\.txt)$/.test(asset.name) ||
176+
/\.html$/.test(asset.name),
149177
},
150178
},
151179
};

0 commit comments

Comments
 (0)