Skip to content Skip to sidebar Skip to footer

Google Closure Compiler - How To Create An Extern For A Variable (variable Name Can't Change As It Is In An Eval)

I am using Google Closure Compiler in 'SIMPLE_OPTIMIZATIONS' mode. The JavaScript uses an 'Eval' statement with the variable '_u' embedded in the string. When Google Closure Compil

Solution 1:

You can only prevent renaming of global variables or properties with externs. Locals will always be renamed. You can modify the code in a number of ways to fix this, including using a function constructor:

new Function("_u", "... function body ...")

But I would rewrite the code to avoid using eval to construct the regex. This would work:

_u=_u.replace(new RegExp(_aA[i], "g"), _aA[i+5]);

You also may be interested in:

Escape string for use in Javascript regex

General documentation regarding the Closure Compiler limitations are here:

https://developers.google.com/closure/compiler/docs/limitations

Solution 2:

It's pretty easy. You just need a separate externs file:

sampleextern.js

/** @externs */var _u;

Externs are valid javascript. See http://blogs.missouristate.edu/web/2013/09/12/how-to-write-closure-compiler-extern-files-part-1-the-basics/

Post a Comment for "Google Closure Compiler - How To Create An Extern For A Variable (variable Name Can't Change As It Is In An Eval)"