Webpack 4,从缩小/压缩中排除第三方库



我的配置中有以下内容:

const viewerConfigProdWeb = merge(common.commonWebConfig, {
output: {
path: outputPath,
filename: common.bundleNameWeb
},
devtool: 'source-map',
mode: 'production',
optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true,
uglifyOptions: {
compress: false,    //<--- if enabled, causes errors
ecma: 6,
mangle: true,
exclude: path.resolve(__dirname, '../js/vendor/tomtom.min.js'),    // <--- it is already minified, want to exclude it somehow. But this approach doesn't work =(
}
})
]
}
});

当我在 uglifyOptions 中将"压缩"更改为 true 时,我收到运行时错误。当 webpack 尝试优化第三方库时,会出现这些错误,该库已被压缩和缩小。如何从优化中排除它?

更新:根据Sin的回答和自述文件,将配置中的优化部分更改为以下内容:

optimization: {
minimizer: [
new UglifyJsPlugin({
cache: true,
parallel: true,
sourceMap: true,
exclude: /.min.js$/,     //<---- moved up and used regex
uglifyOptions: {
compress: true,        //<---- still causes errors when enabled
ecma: 6,
mangle: true
}
})
]
}

这也行不通=(还有其他想法吗?

工作了一段时间后,终于发现exclude选项只检查输出文件名而不是源文件名。有一个 github 问题解决了这个问题。 您可以在那里尝试@hulkish提供的解决方案。


原始答案(不起作用):

尝试将exclude添加到UglifyJsPlugin选项的顶层。并使用RegExpRegExp数组,而不是完整路径。请参阅 uglifyjs-webpack-plugin README

最新更新