我使用webpack2来构建我的应用程序,但是当使用uglify-js + uglifyjs-webpack-plugin时。我遇到了一些问题:
ERROR in vendors.js from UglifyJs
TypeError: Cannot read property 'reset' of undefined
at F:\Github\Program\My-test\Webpack\www2\node_modules\_uglifyjs-webpack-plugin@0.4.3@uglifyjs-webpack-plugin\dist\index.js:99:22
at F:\Github\Program\My-test\Webpack\www2\node_modules\_uglifyjs-webpack-plugin@0.4.3@uglifyjs-webpack-plugin\dist\index.js:231:9
at Array.forEach (native)
at Compilation.<anonymous> (F:\Github\Program\My-test\Webpack\www2\node_modules\_uglifyjs-webpack-plugin@0.4.3@uglifyjs-webpack-plugin\dist\index.js:54:19)
at Compilation.applyPluginsAsyncSeries (F:\Github\Program\My-test\Webpack\www2\node_modules\_tapable@0.2.6@tapable\lib\Tapable.js:142:13)
at self.applyPluginsAsync.err (F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compilation.js:635:10)
at Compilation.applyPluginsAsyncSeries (F:\Github\Program\My-test\Webpack\www2\node_modules\_tapable@0.2.6@tapable\lib\Tapable.js:131:46)
at sealPart2 (F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compilation.js:631:9)
at Compilation.applyPluginsAsyncSeries (F:\Github\Program\My-test\Webpack\www2\node_modules\_tapable@0.2.6@tapable\lib\Tapable.js:131:46)
at Compilation.seal (F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compilation.js:579:8)
at F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compiler.js:493:16
at F:\Github\Program\My-test\Webpack\www2\node_modules\_tapable@0.2.6@tapable\lib\Tapable.js:225:11
at _addModuleChain (F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compilation.js:481:11)
at processModuleDependencies.err (F:\Github\Program\My-test\Webpack\www2\node_modules\_webpack@2.5.1@webpack\lib\Compilation.js:452:13)
at _combinedTickCallback (internal/process/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)
webpack配置可能会给你一些帮助:
module.exports = {
entry: {
app: path.resolve(APP, 'index.js'),
vendors: ['jquery', 'moment']
},
output: {
path: BUILD,
filename: 'bundle.js'
},
plugins: [
new HTMLwebpackPlugin({
title: 'Generate by webpack'
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'vendors',
filename: 'vendors.js'
}),
new UglifyJSPlugin({
compress: true
})
],
module: {
loaders: [
{
test: /\.scss$/,
loaders: 'style-loader!css-loader?sourceMap!sass-loader?sourceMap',
include: APP
},
{
test: /\.(jpg|png)$/,
loaders: 'url-loader?limit=40000'
},
{
test: /\.jsx?$/,
loader: 'babel-loader',
include: APP,
query: {
presets: ['es2015', {"modules": false}]
}
}
]
},
devtool: 'eval-source-map'
}
什么是'重置'是什么意思?我无法在任何地方找到答案...... 谢谢!
答案 0 :(得分:17)
编辑八月2017: 现在uglifyjs-webpack-plugin works with uglify-js@3
安装插件:npm install uglifyjs-webpack-plugin --save-dev
,然后安装在webpack config中:
var UglifyJSPlugin = require('uglifyjs-webpack-plugin')
...
plugins: [
new UglifyJSPlugin()
]
答案 1 :(得分:4)
适合我:
npm install uglifyjs-webpack-plugin --save-dev
plugins: [
new webpack.optimize.UglifyJsPlugin()
]
但这不起作用:
new UglifyJSPlugin({
compress: true
})
同样的问题是在Github,答案是:
目前使用UglifyJS 3打破了插件。它有一个新的API,因此插件必须相应地更改。
答案 2 :(得分:4)
我只是这样才解决它:
npm install --save-dev uglify-js@2
你必须确保你使用的是uglify 2而不是3 我的配置如下所示:
const webpack = require('webpack');
module.exports = {
...
plugins: [
new webpack.optimize.UglifyJsPlugin()
]
};
答案 3 :(得分:2)
使用webpack3 + angular4只需使用
npm install uglify-js@2.8.27 --save-dev
答案 4 :(得分:0)
<强> 2017年7月26日强>
我通过以下方式解决了这个问题:
npm i -D uglifyjs-webpack-plugin@1.0.0-beta.2
原因:uglifyjs-webpack-plugin@0.4.x
,即webpack
使用的版本,不支持uglify-js 3.自uglifyjs-webpack-plugin@1.0.0-beta
以来,它开始支持uglify-js 3.
参考: