报错内容如下
js/app.6ff5e7c3.js from Terser
Unexpected token: keyword «const» [js/app.6ff5e7c3.js:1,4460]
error
js/chunk-2c925c54.8731960b.js from Terser
Unexpected token: punc «(» [js/chunk-2c925c54.8731960b.js:1,1959]
error
js/chunk-3a55163c.6c8c81ab.js from Terser
Unexpected token: operator «>» [js/chunk-3a55163c.6c8c81ab.js:1,5448]
error
js/chunk-56fb77e9.eb13d476.js from Terser
Unexpected token: punc «(» [js/chunk-56fb77e9.eb13d476.js:1,1690]
解决方法
TerserPlugin的配置参入改成如下
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
/**
* Webpack 配置
*/
configureWebpack: () => ({
plugins: [],
optimization: {
minimizer: [
new TerserPlugin({
terserOptions: {
ecma: undefined,
parse: {},
compress: {
drop_console: true, // 传true就是⼲掉所有的console.*这些函数的调⽤.
drop_debugger: true, // ⼲掉那些debugger;
pure_funcs: ['console.log'] // 如果你要⼲掉特定的函数⽐如console.info ,⼜想删掉后保留其参数中的副作⽤,那⽤pure_funcs来处理
},
mangle: true, // Note `mangle.properties` is `false` by default.
module: false,
// Deprecated
output: null,
format: null,
toplevel: false,
nameCache: null,
ie8: false,
keep_classnames: undefined,
keep_fnames: false,
safari10: false
}
})
]
}
}),
}
原先报错的写法
const path = require('path')
const TerserPlugin = require('terser-webpack-plugin')
module.exports = {
/**
* Webpack 配置
*/
configureWebpack: () => ({
plugins: [],
optimization: {
minimizer: [
new TerserPlugin({
minify: (file, sourceMap) => {
const uglifyJsOptions = {
compress: {
drop_console: true,
drop_debugger: true
}
}
if (sourceMap) {
uglifyJsOptions.sourceMap = {
content: sourceMap
}
}
return require('uglify-js').minify(file, uglifyJsOptions)
}
})
]
}
}),
}
其他说明:
我的webpack是版本5(cmd执行webpack -v)
webpack: 5.72.1
terser-webpack-plugin官网:https://webpack.docschina.org/plugins/terser-webpack-plugin/