issues
search
sleepyShen1989
/
blog
MIT License
0
stars
0
forks
source link
webpack5 配置优化选项
#4
Closed
sleepyShen1989
closed
1 year ago
sleepyShen1989
commented
1 year ago
分析工具
内置stats
模块构建耗时
模块依赖关系
Webpack Analysis
官方工具
图形化stats.json
statoscope
模块与chunk关系
Webpack Visualizer
查看模块体积
Webpack Bundle Analyzer
使用率最高的性能工具
Treemap方式展示模块体积
Webpack Dashboard
编辑工程实时展示编译进度
Unused Webpack Plugin
根据stats反查未使用过的文件
speed-measure-webpack-plugin
速度分析
构建效率
持久化缓存
提交二次构建性能(cache-loader...)
并行处理
HappyPack 多进程方式运行资源加载(loader)逻辑
Thread-loader 官方版多进程方式运行资源加载(loader)逻辑
Parallel 多进程方式运行多个Webpack构建实例
TerserWebpackPlugin 支持多进程方式执行代码压缩,uglify功能
减少编译范围
resolve控制资源搜索范围
include
exclude
npm module.noParse
跳过编译 no-parse
npm库提前打包
Vue2 dist/vue.runtime.esm.js
Lodash lodash/lodash.js
lazyCompilation
webpack5新特性
entry 或异步引用模块按需编译
编译步骤提升性能
禁止产物优化(开发期)
TreeShaking (optimization.usedExports)
SplitChunks (optimization.splitChunks)
Minimizer (optimization.minimize)
模块合并 (optimization.concatenateModules)
最小watch范围
watchOptions: { ignored: /node_modules/ }
跳过TS类型检查
ESLint 忽略检查
SourceMap
eval 最佳编译速度
source-map生成环境选用
缩小resolve搜索范围
enhanced-resolve
各种模块导入语句准确定位到模块对应的物理资源路径
resolve.enforceExtension = true
extensions
无后缀越多越慢,强制要求开发者提供明确的模块后缀名
modules配置
module.exports = { resolve: { modules: [path.resolve(__dirname, 'node_modules')] } }
快速编译器
esbuild
swc
babel替换ts-loader
页面性能
SplitChunks分包
单包缺点
页面初始化代码过大,影响首屏渲染性能
无法利用浏览器缓存,任何修改都会导致缓存失效
分包策略
设置分包范围
根据Module使用频率分包
限制分包数量
限制分包体积
缓存组 cacheGroup
代码压缩
减少传输时间
JS TerserWebpackPlugin
CSS CssMinimizerWebpackPlugin
HTML HtmlMinifierTerser
图片压缩
动态加载
减少首屏资源加载
Externals外置依赖
减少应用体积
Tree-shaking删除多余模块
减少应用体积
JS
CSS purecss
ScopeHoisting合并模块
减少应用体积
优化HTTP资源缓存效率
正确使用[hash]占位符
静态资源内联
Serviceworker编程式缓存
兼容性
PostCSS
autoprefixer自动补齐
Polyfill
manifest
Plugin/loader
loader
vue-loader
thread-loader
Plugin
case-sensitive-paths-webpack-plugin
eslint-webpack-plugin
copy-webpack-plugin
DefinePlugin
VueLoaderPlugin
mini-css-extract-plugin
webpack merge
分析工具
构建效率
页面性能
兼容性
Plugin/loader