Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

terser not minifying function names

Tags:

gulp

terser

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,
        }
    }))
like image 299
CommonSenseCode Avatar asked Jul 26 '26 14:07

CommonSenseCode


1 Answers

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);
like image 124
d-h-e Avatar answered Jul 30 '26 08:07

d-h-e



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!