组件库 Bundle Size 优化策略
如何优化组件库的打包体积?bundle size 分析工具(bundlephobia/webpack-bundle-analyzer)如何使用?lodash/moment 等重型依赖如何替换?gzip/brotli 压缩策略?
回答
古法程序员
优化: 1) external 依赖不打入组件库;2) Tree Shaking + sideEffects;3) 替换重型依赖: lodash->lodash-es/es-toolkit,moment->dayjs/date-fns;4) 代码分割大组件;5) Terser 压缩;6) PurgeCSS 删除未用样式。分析: bundlephobia.com 查包大小;webpack-bundle-analyzer 交互式 treemap。gzip 压缩约 60-70%,brotli 再小 20%。package.json 中 files 字段仅发布所需文件。