How to minify ES6 functions with gulp-uglify?

后端 未结 8 1147
时光说笑
时光说笑 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 12:12

    You actually can uglify ES6 code without transpilation. Instead of gulp-uglify plugin, use gulp-uglifyes plugin.

    var gulp = require('gulp');
    var rename = require('gulp-rename'); 
    var uglify = require('gulp-uglifyes');
    var plumber = require('gulp-plumber');
    var plumberNotifier = require('gulp-plumber-notifier');
    var sourcemaps = require('gulp-sourcemaps');
    var runSequence = require('run-sequence').use(gulp);
    
    gulp.task('minjs', function () {
      return gulp.src(['/dist/**/*.js', '!/dist/**/*.min.js'])
        .pipe(plumberNotifier())
        .pipe(sourcemaps.init())
        .pipe(uglify({ 
           mangle: false, 
           ecma: 6 
        }))
        .pipe(rename(function (path) {
          path.extname = '.min.js';
        }))
        .pipe(sourcemaps.write('.'))
        .pipe(gulp.dest('/dist'));
    });
    
    0 讨论(0)
  • 2020-12-23 12:13

    unfortunately, as per now, you can't use uglify with es-next code, you can:

    1. Transpile to ES5using Babel
    2. Use Babili instead of Uglify.
    0 讨论(0)
提交回复
热议问题