Ingo Bürk
Ingo Bürk

Reputation: 20033

Closure Compiler (advanced mode) -- How to design classes?

I have a class that has roughly this structure:

function MyClass() {
    // constructur stuff
}

MyClass.prototype.myFunc = function () {
    // example function
};

MyClass.myStaticFunc = function () {
    // example static function
};

I spent some time now setting up the closure compiler annotations and finally got rid of all warnings. And what do you know, it reduces the size by a spectacular 100%. So then I read about exporting functions, but window['MyClass'] = MyClass will only export the constructor. To be honest, I'd rather not export every single method individually. I thought the compiler would export and not obfuscate all publicly available methods but those with a @private annotation.

What's the best way to teach the closure compiler to do that and not have to export every method individually?

Upvotes: 0

Views: 1186

Answers (2)

Chad Killingsworth
Chad Killingsworth

Reputation: 14411

Using ADVANCED_OPTIMIZATIONS you must export EVERY public method and property. If you do not want the public methods and properties renamed, then use SIMPLE_OPTIMIZATIONS.

See my Which Compilation Level is Right for Me post for more details.

Upvotes: 2

Ingo Bürk
Ingo Bürk

Reputation: 20033

I believe I found the answer: I can annotate methods with @export and run the compiler with --generate_exports. But maybe someone has an even better way.

Upvotes: 1

Related Questions