For example, setting MYCONST = true
would lead to the transformation of
if (MYCONST) {
console.log('MYCONST IS TRUE!'); // print important message
}
to
if (true) {
console.log('MYCONST IS TRUE!'); // print important message
}
This tool ideally has a fast node.js accessible API.
Edit: Whoops. Gotta read title first. Ignore me.
Google Closure Compiler has such a feature:
You can combine the @define tag in your code with the
--define
parameter to change variables at "compile" time.The closure compiler will also remove your if-statement, which is probably what you want.
Simply write your code like this:
And call the compiler with the parameter:
Regarding you API request: Closure Compiler has a JSON API.
The Apache Ant build system supports a replace task that could be used to achieve this.
Patch a beautifier, for example
Replace the last line of
function print_token()
by something likeoutput.push(token_text=="MYCONST"?"true":token_text);
js_beautify(your_code)
from within nodejs.google's closure compiler does, among other things, inlining of constants when annotated as such, leaving string content untouched but I am not sure if it's a viable option for you.
A better way to achieve what you want -
Settings.js
MainCode.js
This way, you make a change to one single file.