How to minify ES6 functions with gulp-uglify?

后端 未结 8 1197
时光说笑
时光说笑 2020-12-23 11:23

When I run gulp I get the following error:

[12:54:14] { [GulpUglifyError: unable to minify JavaScript]
cause:
{ [SyntaxError: Unexpected token: operator (>         


        
8条回答
  •  自闭症患者
    2020-12-23 11:48

    I worked at this for a while before getting it to work. As other answers have stated the problem is that gulp-uglify doesn't support ES6. gulp-uglify-es does, however if is no longer maintained. Terser is recommended by others, but it doesn't play well with gulp and using it with pipe().

    If you use gulp-uglify as I do your gulpfile.js looks something like:

    var uglify = require('gulp-uglify');
    const html2js = () => {
         var source = gulp.src(config.appFiles.templates);
        return source
            .pipe(concat('templates-app.js'))
            .pipe(uglify())
            .pipe(gulp.dest(config.buildDir));
    };
    

    You can however use the gulp-terser package, which is very easy to just replace and get the same functionality:

    var terser = require('gulp-terser');
    const html2js = () => {
         var source = gulp.src(config.appFiles.templates);
        return source
            .pipe(concat('templates-app.js'))
            .pipe(terser())
            .pipe(gulp.dest(config.buildDir));
    };
    

提交回复
热议问题