javascript - How do you define node_modules as externs in Closure Compiler? -


i have node.js project want compile closure compiler. not want run in browser/use browserify. want utility of type checking. got compiler work correctly using following:

java -jar compiler.jar -w verbose                         --language_in ecmascript5_strict                         --externs closure-externs.js                         --js="lib/**.js" 

where closure-externs.js manually defined variables , functions using node.js in rather crude way:

// closure-externs.js  /** @constructor */function buffer(something){} function require(path){} var process = {}; [...] 

it turns out worked through sheer luck. there no dependency tracking between files, can have cases return type {foo} , compiler complain doesn't exist (depending on machine, depending on compile order). found out doing wrong , should using --process_common_js_modules compiler dependency tracking require("foo"). invoking compiler so:

java -jar compiler.jar -w verbose                         --language_in ecmascript5_strict                         --externs externs/fs.js                         --js="lib/**.js"                        --process_common_js_modules                         --common_js_entry_module app.js 

but failing with:

 error - required entry point "module$crypto" never provided  error - required entry point "module$dgram" never provided  error - required entry point "module$extend" never provided  error - required entry point "module$fs" never provided  error - required entry point "module$net" never provided  error - required entry point "module$q" never provided 

some of these modules native node.js (e.g. fs) whereas others contained in node_modules q. don't want run these external modules through compiler, know need set externs file(s) them. know there https://github.com/dcodeio/node.js-closure-compiler-externs common node.js externs, , know how invoke them on compiler, reason when --externs externs/fs.js error module$fs remains. doing wrong?

i know there's other flags --module , --common_js_module_path_prefix i'm not sure if need use them work or not. google-fu has failed come answers on correct incantation here. :(

the issue wish compiler somehow recognize require calls internal, namely required module should processed compiler source, , others external should left alone. there isn't way handle situation currently.

workarounds

use post-processing add external require statements

in scenario omit require statements external modules. compiler process code internal require statements , modules. after compilation, prepend external require statements:

header js prepended

var crypto = require('crypto'); 

source compiled

console.log(crypto); 

because crypto declared in extern, compiler correctly recognize type , symbol name.

alias require calls

when --process_common_js_modules specified, compiler recognizes require statements , expands them in similar fashion way macros work in other languages. aliasing require statements should remain external, compiler not recognize them , not expand them.

source compiled

var externalrequire = require; /** @suppress {duplicate} defined in externs */ var crypto = externalrequire('crypto'); console.log(crypto) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -