- 在生产环境配置文件下,路径写上contentHash值,这样在下一次打包的时候,会进行哈希值比对,判断是否命中缓存
- url-loader:会把在阈值内的文件打包生成dataURL的形式,减少http请求
- 如何配置多入口
entry入口:
entry: {
index: path.join(srcPath, 'index.js'),
other: path.join(srcPath, 'other.js')
},
plugins:
new HtmlWebpackPlugin({
template: path.join(srcPath, 'index.html'),
filename: 'index.html',
chunks: ['index']
}),
new HtmlWebpackPlugin({
template: path.join(srcPath, 'other.html'),
filename: 'other.html',
chunks: ['other']
})
- 抽离css文件
plugin:
new MiniCssExtractPlugin({
filename: 'css/main.[contenthash:8].css'
})
optimization: {
minimizer: [new TerserJSPlugin({}), new OptimizeCSSAssetsPlugin({})],
}
- 抽离公共代码
splitChunks: {
chunks: 'all',
cacheGroups: {
vendor: {
name: 'vendor',
priority: 1,
test: /node_modules/,
minSize: 0,
minChunks: 1
},
common: {
name: 'common',
priority: 0,
minSize: 0,
minChunks: 2
}
}
}
- 懒加载
import('url').then()