Unexpected token: operator (>) from UglifyJs
Asked Answered
E

4

15

I have 2 Vue-Cli webpack projects (ClientApp and Lib). Lib is my components library (shared with other projects)

Problem

When I build my project ClientApp npm run build, I have the following error:

ERROR in static/js/app.d08a24ce0e8d0438ce68.js from UglifyJs
Unexpected token: operator (>) [C:/.../Lib/src/tools/escape-key.js:3,0][static/js/app.d08a24ce0e8d0438ce68.js:17468,38]

Questions

It seems like the error comes from an arrow function in the file escape-key.js. This is ES6 syntax and UglifyJS can't parse this. Shouldn't Babel go first, before Uglify? Note that is works well with *.vue files.

Project structure

ClientApp
   | - build
   | - config
   | - src
       | - App.Vue // import EscapeKey from '~lib/tools/escape-key';



Lib
  | -src
     | - tools
         | - escape-key.js

ClientApp's webpack.base.conf.js file

Note there is an alias to Lib.

resolve: {
    extensions: ['.js', '.vue', '.json'],
    alias: {
      '@': resolve('src'),
      '~lib': path.join(__dirname, '../../lib/src'),
    }
  },

Please feel free to ask for more details if required.

Embower answered 10/5, 2017 at 9:26 Comment(0)
E
3

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')]
}
Embower answered 10/5, 2017 at 13:2 Comment(1)
The webpack simple repository works perfectly fine: github.com/vuejs-templates/webpack-simple/blob/master/template/… So this might work for you, but this is not really the answer people are probably looking for. I am running into the same problem so I will try to figure out what is going on here.Derris
M
11

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

Maltzman answered 5/9, 2017 at 12:11 Comment(2)
There is 1.0.0 version released, so you can use that one as well.Maltzman
You're my hero bro. I use Webpack 3 + Node 10.13 for anyone want to knowRecede
D
5

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

Dulin answered 12/7, 2017 at 15:20 Comment(1)
I have Babel running before Uglify to transpile es6 to es5. Babel did not transpiled some files because those files were not in include property of Babel-LoaderEmbower
A
5

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()
  ]
}
Agape answered 26/10, 2017 at 12:50 Comment(0)
E
3

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')]
}
Embower answered 10/5, 2017 at 13:2 Comment(1)
The webpack simple repository works perfectly fine: github.com/vuejs-templates/webpack-simple/blob/master/template/… So this might work for you, but this is not really the answer people are probably looking for. I am running into the same problem so I will try to figure out what is going on here.Derris

© 2022 - 2024 — McMap. All rights reserved.