Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

javascript - How to solve this minification error on Gulp?

I'm getting below error running this command

gulp.task('minify', function () {
    return gulp
      .src('public/app/js/myapp.bundle.js')
      .pipe(uglify())
      .pipe(gulp.dest('public/app/js/myapp.bundle.min.js'));
});

GulpUglifyError: unable to minify JavaScript Caused by: SyntaxError: Unexpected token: name (MenuItem) (line: 1628, col: 18, pos: 53569)

Code on that location is this

 setters: [],
 execute: function () {
     class MenuItem {  // <-- line 1628

What's wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

UglifyJS does not currently support EcmaScript 6 structures like classes.

You'll probably need to run your JavaScript through a transpiler step first, or find a minifier that knows what to do with ES6 code.

Update 2017-06-17

The branch of UglifyJS that is designed to work with ES6 is now published as uglify-es on npm.

Update 2018-09-10

terser is the new uglify-es, uglify-es is no longer maintained.

If using gulp both npmjs gulp-uglify-es and npmjs gulp-terser packages support terser.

npm install gulp-terser --save-dev
const gulp = require('gulp');
const terser = require('gulp-terser');
 
function es(){
  return gulp.src('./src/index.js')
    .pipe(terser())
    .pipe(gulp.dest('./build'))
}

gulp.task('default', es);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...