0

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.

Anuj Yadav
  • 980
  • 11
  • 20

1 Answers1

0

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
}))
YD1m
  • 5,845
  • 2
  • 19
  • 23
  • Yes, I went through uglify-js documentation a bit, but did not get much help. I tried your suggestion but it did not help. Thanks for your help :) – Anuj Yadav Jun 29 '17 at 15:46
  • @Anuj Yadav Ok. You can try another solution - make your constants a global variables. – YD1m Jun 29 '17 at 15:54