如何利用 Webpack 来优化前端性能
配置
- 开发环境时开启缓存,在 config 中将 cache 设为 true
- 生产环境时关闭不必要的环节,可以关闭 source-map 源代码映射
loader
- 优化 loader 配置,精简 test、include、exclude
plugins
tree-shaking 移除未使用的代码,减小文件体积
通过
CommonsChunkPlugin
提取公共代码通过 plugins 压缩代码
- uglifyjs-webpack-plugin 压缩 js
- extract-text-webpack-plugin 压缩 css