我最近升级到 Webpack 5 并在 uglify js 上使用了 terser,但我在生产构建中收到错误,我找不到错误位置,因为错误本身源自webpack-terser-plugin
,我可能能够剖析代码并继续构建直到我到达了我在代码中做错了什么的地方,但这真的是唯一的方法吗?那么我该如何解决这个错误呢?
ERROR in MyProject.min.js from Terser
TypeError: Cannot read property 'option' of undefined
at AST_Dot.may_throw_on_access (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:12980:28)
at AST_Dot.has_side_effects (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:13905:50)
at AST_Binary.has_side_effects (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:13859:26)
at is_undefined (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:12972:33)
at /home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:15539:12
at AST_VarDef.optimize (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:10890:19)
at Compressor.before (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:10875:24)
at AST_VarDef.transform (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:5390:41)
at /home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:5406:21
at doit (/home/ubuntu/environment/public/MyProject/node_modules/terser-webpack-plugin/node_modules/terser/dist/bundle.min.js:112:23)
webpack 5.26.3 compiled with 1 error in 11622 ms
依赖关系
"devDependencies": {
"@babel/core": "^7.9.6",
"@babel/preset-env": "^7.9.6",
"babel-loader": "^8.1.0",
"clean-webpack-plugin": "^3.0.0",
"file-loader": "^6.2.0",
"html-webpack-plugin": "^4.5.0",
"style-loader": "^2.0.0",
"webpack": "^5.24.4",
"webpack-cli": "^4.5.0",
"webpack-log": "^3.0.1"
},
网络包文件:
const path = require("path")
const TerserPlugin = require("terser-webpack-plugin")
let isProduction = process.env.NODE_ENV === "production"
const { CleanWebpackPlugin } = require("clean-webpack-plugin")
module.exports = {
entry: {
"MyProject": "./src/index.js",
},
output: {
path: path.resolve(__dirname, "dist"),
filename: isProduction ? "[name].min.js" : "[name].js",
},
plugins: [new CleanWebpackPlugin()],
mode: isProduction ? "production" : "development",
module: {
rules: [{
test: /.js$/,
exclude: /(node_modules)/,
use: {
loader: "babel-loader",
options: {
plugins: ["@babel/plugin-transform-modules-commonjs"],
},
},
},
{
test: /.css$/i,
use: [
{ loader: "style-loader", options: { injectType: "linkTag" } },
"file-loader",
],
},
],
},
...(isProduction ? {} : { devtool: "eval-source-map" }),
optimization: {
minimize: true,
minimizer: [new TerserPlugin()],
},
}