gulp-minify is replacing javascript variable name with there value in function call

1.2k Views Asked by At

I have code like:

define('identity', function () {
    // LOT OF CONSTANTS AND USAGE HAS BEEN REMOVED FOR BREVITY
    'use strict';
    var CREATE_ACCOUNT = 'CreateAccount';
    var ACCOUNT = 'Account';
    var CONNECT = 'Connect';
    var SWITCH = 'Switch';
    var FB = 'FB';
    var PIPE_SEPARATOR = ' | ';
    var DOT_SEPARATOR = '.';
    var COMMA_SEPARATOR = ', ';

    function concatenateWithDotSeparater(array) {
        return concatenateWithSeparator(array,DOT_SEPARATOR);
    }

    function concatenateWithPipeSeparater(array) {
        return concatenateWithSeparator(array,PIPE_SEPARATOR);
    }

    function concatenateWithCommaSeparater(array) {
        return concatenateWithSeparator(array, COMMA_SEPARATOR);
    }

    function concatenateWithSeparator(array, separator) {
        return array.join(separator);
    }

    return {
        signUp: {
            facebookConnect : concatenateWithDotSeparater([CREATE_ACCOUNT, FB, CONNECT]),
            }
    };
}); 

Essentially, I had lots of constants which were repeating and need to be concatenated to produce actual value. So, I created constants to hold repeating values and then concatenated them via function.

But when I tried to minify the JS using gulp-minify version 1.0.0 the result was like: return{signUp:{facebookConnect:e(["CreateAccount","FB","Connect"])}}

It injected actual values. How can I prevent this?

I like to get an output like: return{signUp:{facebookConnect:e([s,d,e])}}

I am using minify like:

.pipe(minify({
    ext: {
        src: '.js',
        min: '-min.js'
    }
}))

Please help.

1

There are 1 best solutions below

2
On

You can avoid renaming some variables if you set option mangle where you should to set list of names you want to save unchangeble. Under the hood the gulp-minify packet uses the uglify-es packet. Look at it for detailed documentation.

Please try with this options:

.pipe(minify({
    ext: {
        src: '.js',
        min: '-min.js'
    },
    mangle: { reserved: ['CREATE_ACCOUNT', 'ACCOUNT', 'CONNECT'] // etc
}))