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?
3 Answers
Answers 1
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 usingnpm install -D uglifyjs-webpack-plugin
. Add it to yourplugins
property in your configuration:const UglifyJSPlugin = require('uglifyjs-webpack-plugin') module.exports = { plugins: [ new UglifyJSPlugin() ] }
Answers 2
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
Answers 3
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.
0 comments:
Post a Comment