Reputation: 108500
Is there an easy way for the closure compiler to be able to export a class and all it's prototypes & static methods and keep the names as a public API? Per default, the advanced option renames all variables, but you can export stuff to the global scope like:
window['MyClass'] = MyClass;
However, this only exports MyClass to the global scope, all prototypes and static methods are renamed. One would think that you can loop through through the prototypes and export them, bu no:
for (var i in MyClass.prototype) {
window['MyClass'].prototype[i] = MyClass.prototype[i];
}
This doesnt work. The only way I know is to manually add them like this:
window['MyClass'].prototype['myFunction'] = MyClass.prototype.myFunction;
I want to expose about 50 prototypes so this method is not preferred. Does anyone know how to export the entire class in a handy way?
Upvotes: 4
Views: 3234
Reputation: 37967
What you describe is really what externs is for: Prevent Google Closure Compiler from renaming settings objects
You can see an example of a large externs file here: http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.6.js
You can leave out all the comments and just use statements like:
jQuery.prototype.add = function(arg1, context) {};
To ensure the add method isn't renamed. You do need to either include @externs in the comments of the externs file or pass it as --externs to the Closure Compiler for things to work properly.
Upvotes: 1
Reputation: 1101
Check out the @export annotation, which is documented in the JavaScript style guide: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Comments#Comments
Upvotes: 1