Skip to content

Commit 0020efa

Browse files
committed
feat: 构建lib瘦身
1 parent 1c98b23 commit 0020efa

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

docs/BUILD_BASELINE.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
## 构建瘦身与体积分布基线
2+
3+
### 目标
4+
- 通过 Vite 构建产出组件库,并按需生成体积分布报告,作为后续瘦身的基线。
5+
- 保持默认构建轻量,只有显式开启分析时才生成报告文件。
6+
7+
### 使用方式
8+
- 常规库构建:
9+
```bash
10+
npm run build:lib
11+
```
12+
- 生成 bundle 体积分布报告(treemap,含 gzip/brotli):
13+
```bash
14+
npm run build:lib:analyze
15+
# 等价命令:npm run analyze:lib
16+
```
17+
输出:`dist-lib/bundle-report.html`
18+
19+
### 实现要点
20+
- `vite.config.lib.ts` 中集成 `rollup-plugin-visualizer`,用 `USE_ANALYZE=1` 控制是否启用,避免常规构建额外开销。
21+
- 报告文件随构建生成,可用于 PR 附件或本地对比。
22+
23+
### 后续可选
24+
- 在 CI 中为 release 或特定分支自动生成报告并上传为构建工件。
25+
- 配置 size budget(如限制单 bundle 体积)并在超限时失败。

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@
102102
"build-storybook": "storybook build -o storybook-static",
103103
"postbuild-storybook": "node ./scripts/add-storybook-base.js",
104104
"build:lib": "vite build -c vite.config.lib.ts",
105+
"build:lib:analyze": "cross-env USE_ANALYZE=1 vite build -c vite.config.lib.ts",
106+
"analyze:lib": "cross-env USE_ANALYZE=1 vite build -c vite.config.lib.ts",
105107
"prepublishOnly": "npm run build:lib",
106108
"pub": "npm publish --access public",
107109
"pub:beta": "npm publish --tag beta --access public"

vite.config.lib.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,33 @@ import react from '@vitejs/plugin-react'
33
import dts from 'vite-plugin-dts'
44
import path from 'path'
55
import { fileURLToPath } from 'url'
6+
import { visualizer } from 'rollup-plugin-visualizer'
67

78
const __dirname = path.dirname(fileURLToPath(import.meta.url))
9+
const useAnalyze = Boolean(process.env.USE_ANALYZE)
10+
11+
const analyzePlugin =
12+
useAnalyze &&
13+
visualizer({
14+
filename: 'dist-lib/bundle-report.html',
15+
title: 'pro-react-admin library bundle',
16+
gzipSize: true,
17+
brotliSize: true,
18+
template: 'treemap',
19+
})
820

921
export default defineConfig({
1022
plugins: [
1123
react(),
1224
dts({
1325
insertTypesEntry: true,
1426
tsconfigPath: './tsconfig.json',
15-
include: ['src/components'],
27+
// 需要覆盖 .module.less 声明(位于 src/vite-env.d.ts),因此包含整个 src
28+
include: ['src'],
1629
}),
17-
],
30+
// 仅在 USE_ANALYZE=1 时生成体积分布报告
31+
analyzePlugin,
32+
].filter(Boolean),
1833
resolve: {
1934
alias: {
2035
'@': path.resolve(__dirname, 'src'),

0 commit comments

Comments
 (0)