I am trying to use gulp in order to minify a folder containing JS files. However, one of the files has the above error, preventing it from being minified.
I managed to catch and print the error, which I've partially printed here:
JS_Parse_Error {
message: 'SyntaxError: Unexpected token: punc ())',
filename: 'ex.js',
line: 189,
col: 25,
pos: 6482,
stack: Error\n at new JS_Parse_Error (eval at <anonymous> ... )
plugin: 'gulp-uglify',
fileName: '.../js/ex.js',
showStack: false
}
The file in question contains the following, shortened:
function() {
...
$.confirm({
buttons: {
confirm: function() {
$.post('/ajax-handler', {
...
})
.done( function(response) {
var data = filterResponse(response);
if (data['status'] == 'success') {
sleep(1000).then(() => {
* ...
});
sleep(5000).then(() => {
...
});
} else {
console.log('Oops!');
}
})
.fail( function(err, status, response) {
...
});
},
cancel: function() {}
}
});
...
}
I added the "*" above in order to indicate the exact position listed by JS_Parse_Error.
If you got this error using Grunt (grunt-contrib-uglify) the solution is to install ES6 version of the plugin:
I am having the same issue, i found a great answers here that helped me to reach the the file that was causing the error.
Go to Rails Console and Paste:
Hope it helps someone!
Add the
babel-preset-es2015
dependency to fix this.And also add
'es2015'
in.babelrc
file.// Update
From the comments ~ @imolit
Original answer before the update...
I hope you can get inspired by this solution which works with webpack. (link below)
Simply teach UglifyJS ES6
There are two versions of UglifyJS - ES5 and ES6 (Harmony), see on git
ES5 version comes by default with all the plugins, but if you install a Harmony version explicitly, those plugins will use it instead.
package.json
or
Webpack
To use it with webpack install also the webpack plugin
then import the manually installed plugin
and replace it in code
For more webpack info (Installation/Usage) see https://github.com/webpack-contrib/uglifyjs-webpack-plugin#install
npm install uglifyjs-webpack-plugin --save-dev
is not enoughThe main problem is
"uglifyjs-webpack-plugin": "^0.4.6"
in webpack's package.jsonAccording to semver,
^0.4.6 := >=0.4.6 <0.5.0
. Because of the leading zero,webpack
will never use the1.0.0-beta.2
.So after running
npm i -D uglifyjs-webpack-plugin@beta
, you need to do one more step which isrm -rf node_modules/webpack/node_modules/uglifyjs-webpack-plugin
. Then webpack will pick up the version fromnode_modules/uglifyjs-webpack-plugin
instead ofnode_modules/webpack/node_modules/uglifyjs-webpack-plugin
Update on 2018-04-18: webpack v4 does not have this issue
Add stage-3 to presets in .babelrc file.