How to minify ES6 functions with gulp-uglify?

后端 未结 8 1146
时光说笑
时光说笑 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));
    };
    
    0 讨论(0)
  • 2020-12-23 11:54

    The accepted answer doesn't really answer how to minify straight es6. If you want to minify es6 without transpiling, gulp-uglify v3.0.0 makes that possible:

    Update March 2019

    Using my original answer, you definitely want to replace the uglify-es package with terser, as it seems uglify-es is no longer being maintained.

    Original answer, still works:

    1.) First, upgrade your gulp-uglify package to > 3.0.0 If you're using yarn and want to update to the latest version:

    yarn upgrade gulp-uglify --latest
    

    2.) Now you can use uglify-es, the "es6 version" of uglify, as so:

    const uglifyes = require('uglify-es');
    const composer = require('gulp-uglify/composer');
    const uglify = composer(uglifyes, console);
    
    gulp.task('compress', function () {
        return gulp.src('src/*.js')
        .pipe(uglify())
        .pipe(gulp.dest('dist'))
    });
    

    For more info: https://www.npmjs.com/package/gulp-uglify

    0 讨论(0)
  • 2020-12-23 11:56

    You can leverage gulp-babel as such...

    const gulp = require('gulp');
    const babel = require('gulp-babel');
    const uglify = require('gulp-uglify');
    
    gulp.task('minify', () => {
      return gulp.src('src/**/*.js')
        .pipe(babel({
          presets: ['es2015']
        }))
        .pipe(uglify())
        // [...]
    });
    

    This will transpile your es6 early in the pipeline and churn out as widely supported "plain" javascript by the time you minify.


    May be important to note - as pointed out in comments - the core babel compiler ships as a peer dependency in this plugin. In case the core lib is not being pulled down via another dep in your repo, ensure this is installed on your end.

    Looking at the peer dependency in gulp-babel the author is specifying @babel/core (7.x). Though, the slightly older babel-core (6.x) will work as well. My guess is the author (who is the same for both projects) is in the midsts of reorganizing their module naming. Either way, both npm installation endpoints point to https://github.com/babel/babel/tree/master/packages/babel-core, so you'll be fine with either of the following...

    npm install babel-core --save-dev
    

    or

    npm install @babel/core --save-dev
    
    0 讨论(0)
  • 2020-12-23 11:56

    gulp-uglify:

    For ES6 and newer.

    1. install: npm install --save-dev gulp-uglify
    2. install: npm install --save-dev gulp-babel @babel/core @babel/preset-env

    Usage:

    const gulp = require('gulp'); 
    const uglify = require('gulp-uglify');
    const babel = require('gulp-babel');
    
    gulp.task('script', () => {
        return gulp.src('src/**/*.js')
            .pipe(babel({
                presets: ['@babel/env']
            }))
            .pipe(uglify())
            .pipe(gulp.dest('src/dist'))
    });
    
    0 讨论(0)
  • 2020-12-23 11:59

    Using gulp-uglify-es instead of gulp-uglify helped me perfectly to accomplish same as you're asking for

    0 讨论(0)
  • 2020-12-23 12:00
    module.exports = {
      ...
      optimization: {
        minimize: true
      },
      ...
    }
    

    webpack can do the job

    0 讨论(0)
提交回复
热议问题