Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure Compiler - obfuscate public methods - inconsistent behavior?

I just started using Closure Compiler, and I'm seeing some inconsistent behavior regarding obfuscation of public object methods.

I'm using grunt-closure-compiler - here's my grunt config:

module.exports = function(grunt) {

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        'closure-compiler': {
            app: {
                closurePath: 'closure-compiler',
                js: 'src/app.js',
                jsOutputFile: 'js/app.min.js',
                maxBuffer: 500,
                options: {
                    compilation_level: 'ADVANCED_OPTIMIZATIONS',
                    language_in: 'ECMASCRIPT5_STRICT',
                    formatting: 'PRETTY_PRINT'
                }
            }
        }
    });


    grunt.loadNpmTasks('grunt-closure-compiler');

};

And here's app.js the file I want to obfuscate:

(function() {
  /**
   * @constructor
   */
  function MyClass() {
    var map = {};
    this.put = function(name, val) {
      map[name] = val;
    };
    this.get = function(name) {
      return map[name];
    };
  }
  var mapper = new MyClass();
  mapper.put("first", 123);
  alert(mapper.get("first"));

})();

(function() {
  /**
   * @constructor
   */
  function Foo() {
    var private_member_1 = 'bla';

    this.someMethod = function(param1, param2) {
      // do some stuff
      console.log('abc');
      return private_member_1;
    };
    // etc...
  }

  var foo = new Foo();
  var value = foo.someMethod();
  console.log(value + value);
})();

And this is the output after running grunt closure-compiler:app

'use strict';(function() {
  var a = new function() {
    var a = {};
    this.put = function(b, c) {
      a[b] = c;
    };
    this.get = function(b) {
      return a[b];
    };
  };
  a.put("first", 123);
  alert(a.get("first"));
})();
(function() {
  var a = (new function() {
    this.a = function() {
      console.log("abc");
      return "bla";
    };
  }).a();
  console.log(a + a);
})();

Notice that in the first closure the methods put and get were not obfuscated, while in the second closure the method someMethod was obfuscated.

Why does this happen? And how can I cause all public methods in all of my classes (like put and get) to be obfuscated as well?

Please note -

  • I am willing to use a different tool other than Closure Compiler to achieve this.
  • I want to use this with TypeScript, i.e. obfuscate the compiled TypeScript code, this is why I have many public methods.
  • Some specific methods are API methods that should not be obfuscated. So I need a way to tell the obfuscator which methods to skip.
like image 889
Malki Avatar asked Mar 03 '26 04:03

Malki


2 Answers

Update

As of the 20150315 release of Closure-compiler, the type based optimizations are enabled by default.


This is actually covered in the project FAQ.

The option you want is use_types_for_optimization. This will enable property renaming even if a property of the same name is defined on an unrelated object.

The comprehensive list of properties which can only be renamed with this flag is huge: any property name defined on any object in an extern file.

like image 128
Chad Killingsworth Avatar answered Mar 06 '26 02:03

Chad Killingsworth


I figured it out - get and put are considered reserved words, and closure compiler ignores them on purpose. I ran across a list of these reserved words earlier, but I can't find it now... would appreciate if someone posts a link to the complete list.

like image 25
Malki Avatar answered Mar 06 '26 03:03

Malki



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!