0

I am new in google closure compiler,and after read the documents in the closure tools site,I create a js and make the test.

However I found that even I use the Advanced Compilation level,the compiled codes are still easy to decompilated.

For example,this is the codes:

//2
window["_NameSpace_"]={};
window["_NameSpaceInternal_"]={};
(function() {
    _NameSpaceInternal_.Class = function() {
        var clazz = function() {
            this["init"] && this["init"].apply(this, arguments);
        };
        var pros = {}, arg;
        for(var c = 0, k = arguments.length; c < k; ++c) {
            arg = arguments[c];
            if( typeof arg === 'function') {
                if(c == 0 && k > 1) {
                    var F = function() {
                    };
                    F.prototype = arg.prototype;
                    pros = new F;
                    arg = arg.prototype;
                }
            } else {
                if(arg.init) {
                    clazz = arg["init"];
                    delete arg["init"]
                }
            }
        }
        for(var p in arg)
            pros[p] = arg[p]
        clazz.prototype = pros;
        return clazz;
    };
})();

(function(d){
    d["Person"]=_NameSpaceInternal_.Class({
        "init":function(name){
            this.name=name; 
        },
        "whatever":function(aaa){
        }
    });
})(_NameSpace_);

After compiled(I make a pretty format for human reading):

window.__MapNS__ = {};
window.__MapNSImpl__ = {};
__MapNSImpl__.a = function() {
    function c() {
        this.init && this.init.apply(this, arguments)
    }
    for (var b = {}, a, d = 0, e = arguments.length; d < e; ++d) if (a = arguments[d], "function" === typeof a) 0 == d && 1 < e && (b = function() {}, b.prototype = a.prototype, b = new b, a = a.prototype);
    else {
        a.b && (c = a.init, delete a.init)
    }
    if (!b || !a) throw Error("You must provide an object to copy from and to");
    for (var f in a) b[f] = a[f];
    c.prototype = b;
    return c
};
(function(c) {
    c.Person = __MapNSImpl__.a({
        init: function(b) {
            this.name = b
        },
        whatever:function(x){
        }
    })
})(__MapNS__);

Now,taks the class defination for "Person" for example, after compiled,all the methods for "Person" are clearly for hacker even these method have to be exposed.

But I wonder if I can make the compiled codes like this;

...........
var xx="init",xxx="whatever",xxxx=="Person";
    c[xxxx] = __MapNSImpl__.a({
        xx: function(b) {
            this.name = b
        },
        xxx:function(x){
        }
    })

...........

Just like the google map's compress.

Any idea?

hguser
  • 35,079
  • 54
  • 159
  • 293

1 Answers1

2

The exact answer you are looking for is to enable the AliasStrings pass in the compiler by either using the Java API or by making a custom build of the compiler. The pass is not enabled by default as it tends to produce larger code when accounting for gzip.

To actually get the same effect that most google products achieve, you'll need to make substantial changes to your code.

  1. Define your objects and methods in the global scope. Assign them to namespaces after declaring them globally. You can use the output_wrapper flag to wrap the compiled code in a function to prevent global scope collisions. Example:

    function a() {}; window['Namespace']['obj'] = a;

  2. Define your objects directly - don't use a helper method. So instead of

    var a = _NameSpaceInternal_.Class({"init":function(name){ this.name=name; });

    You would use something like:

    function a(){}; a.prototype.name = ""; a.prototype.init = function(name) { this.name=name; };

    This avoids using quoted syntax and allows the compiler to rename your properties and methods.

For many more examples of coding style that compiles/renames optimally with Closure-compiler, see the Closure Library.

Chad Killingsworth
  • 14,360
  • 2
  • 34
  • 57
  • In fact,what we want is compress and Obfuscate(the key point) the code,any alternative method? – hguser May 19 '12 at 00:46
  • Enabling AliasStrings is your best option short of re-writing your code. You might also try the experimental type based optimizations: http://code.google.com/p/closure-compiler/wiki/ExperimentalTypeBasedPropertyRenaming – Chad Killingsworth May 19 '12 at 23:41