Closure compiler exports all prototypes and static methods

Is there an easy way so that the closure compiler can export the class and all its prototypes and static methods and save the names as a public API? By default, the advanced option renames all variables, but you can export the material to the global area, for example:

window['MyClass'] = MyClass; 

However, this only exports MyClass to the global scope, all prototypes and static methods are renamed. One would think that you could go through prototypes and export them, bu no:

 for (var i in MyClass.prototype) { window['MyClass'].prototype[i] = MyClass.prototype[i]; } 

This does not work. The only way I know is to manually add them as follows:

 window['MyClass'].prototype['myFunction'] = MyClass.prototype.myFunction; 

I want to exhibit about 50 prototypes, so this method is not preferred. Does anyone know how easy it is to export the whole class?

+4
source share
2 answers

What you describe is really what the outside parties are for: Prevent the Google Closure compiler from renaming settings objects

Here you can see an example of a large externs file: http://code.google.com/p/closure-compiler/source/browse/trunk/contrib/externs/jquery-1.6.js

You can leave all comments and simply use statements such as:

 jQuery.prototype.add = function(arg1, context) {}; 

So that the add method is not renamed. You need to either include @externs in the comments of the externs file, or pass it as -externs in the Closure Compiler so that everything can work correctly.

+1
source

Check out the @export annotation, which is documented in a JavaScript style guide: http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml?showone=Comments#Comments

+1
source

Source: https://habr.com/ru/post/1339617/


All Articles