File tree Expand file tree Collapse file tree 3 files changed +44
-2
lines changed
Expand file tree Collapse file tree 3 files changed +44
-2
lines changed Original file line number Diff line number Diff line change 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 体积)并在超限时失败。
Original file line number Diff line number Diff line change 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"
Original file line number Diff line number Diff line change @@ -3,18 +3,33 @@ import react from '@vitejs/plugin-react'
33import dts from 'vite-plugin-dts'
44import path from 'path'
55import { fileURLToPath } from 'url'
6+ import { visualizer } from 'rollup-plugin-visualizer'
67
78const __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
921export 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' ) ,
You can’t perform that action at this time.
0 commit comments