Uglification failed. Unexpected character '`'
Gulp-uglify has yet no official support for ECMAScript 2015 (aka ES6, aka Harmony) but with a little modification the on-development repository can be used.
How-to:
- Open Console and enter
cd node_modules/gulp-uglify
- Edit package.json
dependencies": {
"uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony"
},
- Console enter:
npm update
And it is ready to run .pipe(uglify())
again
Alternate Solution
- Download the following via
npm
:
npm install --save-dev gulp-uglify gulp-babel babel-preset-es2015
- Add the following requires in the
gulpfile.js
:
var babel = require('gulp-babel'),
uglify = require('gulp-uglify');
- The gulp task will be as follow:
gulp.task('uglify', function(){
gulp.src('*.js')
.pipe(babel({
presets: ['es2015']
}))
.pipe(uglify().on('error', function(e){
console.log(e);
}))
.pipe(gulp.dest('js'));
});
What this does is transpile all the EcmaScript 2015 JS code to EcmaScript5 and then uglifies it.
ES6 is supported in a newer version of gulp-uglify called gulp-uglify-es. If you are using Gulp 4 you can also use .browserlistrc. Below are examples of a project I am using that needs Chrome and Firefox 56 support.
gulpfile.js
const uglify = require('gulp-uglify-es').default;
.pipe(babel({
presets: ['@babel/preset-env'],
compact:false
}))
.pipe(uglify())
.browserlistrc (read more on browserlist)
last 2 chrome versions
firefox 56