Search code examples
gulpterser

terser not minifying function names


So I'm using terser with gulp and have tried several combinations but nothing seems to minify the function names here is an example:

function minify() {
    return gulp.src('js/**/*.js')

        .pipe(gTerser({
            keep_fnames: false,
            mangle: {
                keep_fnames: false,
            }
        }))
        .pipe(gulp.dest(`foo/js/`))
}

Update

this config also helped me achieve what I wanted:

   .pipe(gTerser({
        keep_fnames: false,
        mangle: {
            properties: {
                keep_quoted: true
            },
            keep_fnames: false,
        }
    }))

Solution

  • I have made a small test with your terser task. With the mangle (toplevel) option i get the desired result.

    mangle option toplevel: toplevel (default false) -- Pass true to mangle names declared in the top level scope.

    const gulp = require('gulp');
    const gTerser = require('gulp-terser');
    
    function minify() {
      return gulp.src('./js/**/*.js')
        .pipe(gTerser({
          ecma: 6,
          keep_fnames: false,
          mangle: {
            toplevel: true,
          },
        }))
        .pipe(gulp.dest('./foo/js/'));
    }
    
    gulp.task('default', minify)
    

    Example:

    const minifyMyFunctionName = (num) => {
      console.log(num * 2);
    };
    
    minifyMyFunctionName(2);
    

    Terser result:

    const o=o=>{console.log(2*o)};o(2);