19

I am trying to use UglifyJS to minimize/compress my bundle.js file. When I run webpack -p, I am getting the following:

ERROR in bundle.js from UglifyJs Name expected [bundle.js:105519,6]

Line 105519 is as follows:

const {M, l, pattern} = __webpack_require__(862).

I am using React w/ ES6. Any thoughts as to what is wrong?

vijay
  • 10,276
  • 11
  • 64
  • 79
Colby Cox
  • 217
  • 1
  • 2
  • 7
  • I feel like I've seen this in gulp-land before, psure the solution was to transpile everything first then run uglify on the results of the transpile. – John Jones Sep 05 '17 at 19:23

3 Answers3

23

Every version of Webpack has a built-in version of UglifyJS (0.4.6) which doesn't support ES6. This version supports ES5 syntax only.

There are two possible solutions:

  • Make transpiler target es5
  • Don't use the built-in version of uglifyjs-webpack-plugin and install the latest version using npm install -D uglifyjs-webpack-plugin. Add it to your plugins property in your configuration:

    const UglifyJSPlugin = require('uglifyjs-webpack-plugin')
    
    module.exports = {
      plugins: [
        new UglifyJSPlugin()
      ]
    }
    
Andrew Li
  • 55,805
  • 14
  • 125
  • 143
Michał Pietraszko
  • 5,666
  • 3
  • 21
  • 27
0

This error comes when uglifyjs-webpack-plugin is not able to Uglify particular dependency

So, How to find library that creates such error ?

I was using react , so i deleted all forms in my app and kept just 1 form and imported all dependencies in it, and remove/add those dependencies one by one and run command

webpack -p

SO In My case it was browser-history creating such error .Now you can report this issue to that library's author with some replicating example

working Package.json & .babelrc

Rahul Kadukar
  • 858
  • 3
  • 15
  • 35
vijay
  • 10,276
  • 11
  • 64
  • 79
0

Definitely an issue with the version of uglifyjs and the javascript target you are trying to compile to. It could be 2 things, your webpack setup and your babel setup causing this.

If you are using the latest version of webpack v3.5.5 it comes with uglifyjs-webpack-plugin ^0.4.6 which doesn't support a target of es6 or above.

Referring to the current Webpack docs about UglifyjsWebpackPlugin options it covers how to use the latest beta version of uglify-js-webpack-plugin v1.0.0-beta.2. But isnt that clear on how to install that version.

To use it with the Webpack do

yarn add uglifyjs-webpack-plugin@beta --dev

As you don't mention what your Babel setup is. You might be or want to use babel-preset-env as your preset. There is an option for uglifyjs.

Be great to see a repo or a Gist.

andykenward
  • 924
  • 7
  • 17