已经2天无法解决问题。我需要在 webpack 中安装 uglifyJsPlugin 。但 webpack 抛出错误
webpack.config.js:25
module.exports.plugins.push(
TypeError: Cannot read property 'push' of undefined
请帮助我如何解决这个问题。?
这是我的 webpack.config.js
const NODE_ENV = process.env.NODE_ENV || "development";
module.exports = {
entry: "./common",
output: {
path: __dirname + "/dist",
filename:"bundle"
},
watch:NODE_ENV == "development",
devtool:NODE_ENV == "development" ? "cheap-inline-module-source-map" : null,
module: {
rules: [{
test: /\.js$/,
loader:'babel-loader?optional[]=runtime',
options: { presets: ['es2015'] }
}]
}
};
if (NODE_ENV == "production") {
module.exports.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console:true,
unsafe:true
}
})
)
}
答案 0 :(得分:3)
您的配置中似乎没有定义plugins
。
添加此项,你应该好好去
plugins: [],
您可能希望稍微更改导出,如下所示:
var config = {
entry: "./common",
output: {
path: __dirname + "/dist",
filename:"bundle"
},
watch:NODE_ENV == "development",
devtool:NODE_ENV == "development" ? "cheap-inline-module-source-map" : null,
module: {
rules: [{
test: /\.js$/,
loader:'babel-loader?optional[]=runtime',
options: { presets: ['es2015'] }
}]
},
plugins: []
};
if (NODE_ENV == "production") {
config.plugins.push(
new webpack.optimize.UglifyJsPlugin({
compress: {
warnings: false,
drop_console:true,
unsafe:true
}
})
);
}
modules.exports = config;