I am using Google's closure compiler optimization (simple optimization) to reduce the size of some of my JS script files. Whilst this works well I have run into issues with eval statements in some of my functions where replacement of local variables by the compiler wrecks havoc. I could try to recode the offending functions to beat the compiler but that is liable to be painful - and in the long term dangerous since the tricks I use today may well not work tomorrow.
It would be nicer if I could simply mark the bits of code I want the compiler to leave untouched. At present I am contemplating taking all functions that use evals, putting them in a separate file and tagging that file oat the end of the compiler output. However, before I do that I thought it worth asking here - is there a way to tell the compiler to skip optimizations on certain functions. e.g.
// @compilation_level SIMPLE_OPTIMIZATIONS
function test(one,two)
{
}
function testTwo(alpha,beta)
{
}
// @Closure:Skip
function evalFunc(one,two)
{
//eval code here
}
//@Closure:EndSkip
The end result - the code between the Skip, EndSkip sections passes through the compiler without any changes.
I have looked through the documentation but have not found anything that might do just this.
There are a few hints about this problem in the documentation. The proposed skipping feature probably wouldn't solve anything in most cases for the same reason compiling eval usually fails.
If you aren't accessing any variables outside of the scope of evalFunc
though, you could try refactoring all local variables to properties using bracket notation and putting all property names in quotes, so they won't be renamed.
For example,
function evalFunc(one) {
this['foo'] = one;
eval('alert(foo);');
}
evalFunc.call({}, 1);
will roughly compile to
function(a) {
this.foo = a;
eval("alert(foo);");
};
a.call({}, 1);
keeping the eval working. It works both on simple and advanced mode.