我有 webpack 6.0.1。除了通常的 webpack.config.js 设置之外,我还可以建议以下优化模型(webpack.config.js 生产模式)。更多信息请参见底部与插件相关的optimization、mode configuration 和 cmets:
//webpack.config.js
module.exports = {
...
devtool: 'cheap-module-source-map',
...
plugins : [
...
new webpack.DefinePlugin({ 'process.env.NODE_ENV': JSON.stringify('production') }),
new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.HashedModuleIdsPlugin({
hashFunction: 'sha256',
hashDigest: 'hex',
hashDigestLength: 4
}),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
],
...
optimization: {
namedModules: false,
namedChunks: false,
nodeEnv: 'production',
flagIncludedChunks: true,
occurrenceOrder: true,
sideEffects: true,
usedExports: true,
concatenateModules: true,
splitChunks: {
cacheGroups: {
commons: {
test: /[\\/]node_modules[\\/]/,
name: 'vendor',
chunks: 'all'
}
},
minSize: 30000,
maxAsyncRequests: 5,
maxAsyncRequests: 3,
},
noEmitOnErrors: true,
minimize: true,
minimizer: [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: false,
ecma: 6,
mangle: true
},
sourceMap: true
})
],
removeAvailableModules: true,
removeEmptyChunks: true,
mergeDuplicateChunks: true,
},
...
}
评论:
- webpack.optimize.ModuleConcatenationPlugin() - 连接范围
将所有模块合并到一个闭包中,并允许您的代码具有
在浏览器中更快的执行时间
- webpack.HashedModuleIdsPlugin() - 导致哈希基于
模块的相对路径,生成四个字符串为
模块 ID
- webpack.optimize.OccurrenceOrderPlugin() - 改变
id 的分布以获得经常使用的最小 id 长度
身份证
- webpack.NoEmitOnErrorsPlugin() - 跳过发射阶段
每当编译时出现错误。这确保没有
发出包含错误的资产