How to auto-generate externs for the Google Closure Compiler
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 preve开发者_如何学Pythonnt 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?
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/
Many projects include TypeScript-style type definitions (i.e. as index.d.ts). That format isn't directly usable in Closure Compiler, but I found this tool to automatically convert a .d.ts to an extern.
https://www.npmjs.com/package/tsd2cce - A converter for google closure compiler externs from typescript declaration files (.d.ts).
Usage
npx tsd2cce declaration.d.ts create_extern.js
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
精彩评论