-->

How to auto-generate externs for the Google Closur

2020-06-12 02:18发布

问题:

Suppose you are working in a javascript project with several external library dependencies, and want to compile your sources using the Google Closure Compiler in ADVANCED_OPTIMIZATIONS mode.

Since in this mode the compiler will rename your code calls to the external libraries objects and functions, you must provide externs, to prevent this renaming from happening.

But, it is a lot of work to produce the externs by hand, so, what is the best way to auto-generate the appropriate extern from a given javascript lib?

回答1:

See the following URL as a starting point. I would also recommend adding type information to your externs to maximize compiler benefit.

http://www.dotnetwise.com/Code/Externs/



回答2:

you can export your function by: window['myFunc'] = myFunc;

bit more detail here: http://code.google.com/closure/compiler/docs/api-tutorial3.html#no

if you actually want externs for libs you import from elsewhere (ie service wrappers, jquery, etc) then thats where you need to pass the extern in If you are using teh jar compiler, you specify using --externs, more detail in link above