打包构建
- 代码压缩gzip(html, js, css)
const CompressionWebpackPlugin = require('compression-webpack-plugin')
configureWebpack:{
plugins:[
new CompressionWebpackPlugin({
algrithm:"gzip",
test:/\.(js)|(html)$/,
minRatio:1
})
]
}
- 离线化
const PrerenderSpaPlugin = require('prerender-spa-plugin')
plugins:[
new PrerenderSpaPlugin({
path.join(__dirname,"../dist")
['/','/detail'.....]
})
]
- resolve 优化
- tree shaking去除冗余代码
- 代码分块
module.exports = {
optimization: {
splitChunks: {
chunks: 'all',
},
},
};
- 提取组件的 CSS
npm install mini-css-extract plugin
资源加载
- 避免CSS阻塞
尽快加载CSS - 避免JS阻塞
使用 async、defer 进行异步加载 - 静态资源缓存
经常变动的资源使用协商缓存、变动较少的资源使用强缓存 - 减少回流和重绘
- 图片压缩、雪碧图
- 预加载 prefetch
<meta http-equiv="x-dns-prefetch-control" />
代码编写
- 事件委托
- 节流和防抖
- DOM操作尽量在documentFragment中完成
- 路由懒加载、图片懒加载
- 组件按需引入、异步加载
- web worker多线程