I’m working on a modular component framework and have different needs for the code in my test environment vs production. Each component has its own js file and these are being compiled together for production use, but left separate in my dev environment.
In production I want to trap errors in various stages of initialization so as to not break the call stack. In my development (uncompressed javascript), I do not want to trap the errors, so I can quickly get line numbers, etc to find where the breaks occur. Is there any way to use the closure compiler to manipulate a javascript file (such as removing a flagged line such as a comment opener and closer) that might allow me to do this?
For instance:
proto.instantiateChildFromDescriptor = function(childObj, callback, customDiv){
/*ProductionErrorTrap
try{
/**/
//code for instantiating a module
/*ProductionErrorTrap
}catch(e){
console.log("problem instantiating child in " + this.getName());
console.error(e);
}
/**/
}
replacing /*ProductionErrorTrap with /*ProductionErrorTrap*/ would work nicely (so is removing my stars on the second comment block, but they are there)
I can’t do this with a find and replace on the files themselves because it would mess with github. I suppose I could create a new copy of the files, run a find and replace on them and compile *those files, but It would be nice if I could do it all in the closure compiler.
Thanks
I think messing with compiled results, matching comments to emulate ifdefs is pretty unelegant.
You could actually do something like this:
Then while debugging you do:
And in production:
Closure Compiler is pretty smart. With the first debugTry the example compiles to:
With the second debugTry it compiles to:
As you see, the unnecessary code is detected and eliminated.
Of course, things would be easier if you didn’t conditionally include a try block. If it’s just some statements included conditionally, not changing the structure of other code, you can do:
The alert will be compiled away.