Unexpected token: operator (>) from UglifyJs

做~自己de王妃 提交于 2019-12-01 03:13:31

Add

"uglifyjs-webpack-plugin": "v1.0.0-beta.1",

to your dev dependencies and update your webpack.config.js file to use this version explicitly:

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  plugins: [
    new UglifyJSPlugin()
  ]
}

uglifyjs-webpack-plugin latest stable release (v0.4.6) uses older version of uglify-js instead of uglify-es that is capable of transpiling ES6. This dependency was updated in 1.0.0-beta.1 release.

https://github.com/webpack-contrib/uglifyjs-webpack-plugin/releases/tag/v1.0.0-beta.1

This solved the problem for me. I installed uglifyjs-webpack-plugin

npm install uglifyjs-webpack-plugin --save-dev

Then added this to my webpack.config

const UglifyJSPlugin = require('uglifyjs-webpack-plugin')

module.exports = {
  plugins: [
    new UglifyJSPlugin()
  ]
}

The version of uglify that you are using probably doesn't support ES6.

https://github.com/mishoo/UglifyJS2/tree/harmony is the es6 version as of now

if you want to use the webpack plugin, make sure to pay attention to the install section regarding es6

Important! The plugin has a peer dependency to uglify-js, so in order to use the plugin, also uglify-js has to be installed. The currently (2017/1/25) available uglify-js npm packages; however, do not support minification of ES6 code. In order to support ES6, an ES6-capable, a.k.a. harmony, version of UglifyJS has to be provided.

If your minification target is ES6:

yarn add git://github.com/mishoo/UglifyJS2#harmony-v2.8.22 --dev

After changing my babel-loader config, it worked.

Babel-loader config

{
  test: /\.js$/,
  loader: 'babel-loader',
  include: [path.join(__dirname, '../../Lib/src'), resolve('src'), resolve('test')]
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!