javascript - Removing debug code from inside a function using Closure Compiler simple optimisations -
I am exploring a method of stripping the debug code from the function so that I will add a test hook for the closure. I have read and tested the debug code with the following:
/ ** @define {boolean} * / var DEBUG = true; If (DEBUG) {console.log ('remove me'); } - Simplified optimization with defined = 'DEBUG = false' to var DEBUG =! 1; . The same applies to: / ** @const * / var DEBUG = false; If (DEBUG) {console.log ('remove me'); } Wherever I run into trouble, this conference is being used inside the function:
/ ** @const * / var DEBUG = False; In the function log () {if (DEBUG) {console.log ('remove me'); }} This lowers the following:
var DEBUG =! 1; In the function log () {DEBUG & amp; Console.log ("delete me")}; I hope this will reduce it by:
var DEBUG =! 1; Function logMe () {}; Is there a reason that is not working as expected? I'm actually looking for a clean way for debug code bars and not ready to get into advanced optimization.
Update Reply @John, I implemented my compiler and found that the following configuration @define To delete code (DEBUG) {} from inside and outside of the code: compiler option option = new compiler option (); CompilationLevel.SIMPLE_OPTIMIZATIONS.setOptionsForCompilationLevel (option); //options.setInlineConstantVars(true); Options.setInlineVariables (CompilerOptions.Reach.ALL); Options.setDefineToBooleanLiteral ("DEBUG", incorrect); It works adequately for a file with the following limits:
- defined in each
var DEBUG The file that needs to be done, which is poor practice. - When you can combine more than one file, you can only have one single
var DEBUG or the compiler can not be optimized around it. It can be avoided by compiling each file individually and merging them. - Because the value is defined at the beginning of the file, so there is no flexibility to get the value.
I have considered extracting all the var DEBUG definitions from files and it is injected into source or external before execution, but I have two Has participated in issues: - Definition in Exteine appears to be nothing.
- Unsafe code throws a reference error in the undefined
DEBUG browser. The ideal option is to test window.DEBUG , which does not return a reference error. Unfortunately, while / ** @const * / var window = {} injection; / ** @const * / window.DEBUG = false; works at the top level, reduces , if put in the (window.DEBUG) {} function, then optimization is actually withdrawn. As long as the other compiler option works as the only option that is actually understood to go with window.DEBUG and before compiling / ** @ Const * / var DEBUG = false; Replace the global DEBUG with and / \ bwindow.DEBUG \ b / . Is there a better way?
A custom build of the compiler will allow you to do this. You basically want to "inline in continuous variables":
options.setInlineConstantVars (true);
You can apply it here, apply the prefimilation option:
Or you can use the Java API and add options (without modifying the code of the compiler). Here's an example of how Michael Bolin did this:
Comments
Post a Comment