Remove debugger keyword during compilation in google closure

193 Views Asked by At

UPDATE: The JS version of closure-compiler is no longer supported or maintained. https://github.com/google/closure-compiler-npm/blob/master/packages/google-closure-compiler-js/readme.md


Im trying to find if there is a way to remove the "debugger" keyword during compilation process, im using the javascript version google-closure-compiler with gulp.

Looking through the documentation it is clear we can set the flag to stop/show error messages during compilation by doing the following.

https://github.com/google/closure-compiler/wiki/Flags-and-Options

--jscomp_off

translating this to gulp, it is:

const googleClosureOptions = {
  ...
  jscomp_error:"checkDebuggerStatement"
}

however this works on stopping the compilation by throwing error or to show a warning.

zyxcdafg.js:1444: ERROR - [JSC_DEBUGGER_STATEMENT_PRESENT] Using the debugger statement can halt your application if the user has a JavaScript debugger running.
                    debugger;
                    ^^^^^^^^^

but what I am trying to achieve is to remove the debugger keyword. Is this possible to achieve using googleclosure. I can not find any flags or options relating to this.


UPDATE: The JS version of closure-compiler is no longer supported or maintained. https://github.com/google/closure-compiler-npm/blob/master/packages/google-closure-compiler-js/readme.md

2

There are 2 best solutions below

1
Graham P Heath On BEST ANSWER

No I don't think so. I'd suggest you use something else to do it. Like sed:

find dist -name "*.js" -exec sed -i 's/\sdebugger;//' {} +

Something like that will find files in your dist folder that end with .js and then exec-ute sed to replace all instances of debugger; with nothing.

You could add that to a script that calls your Closure Compiler build.

0
John On

The compiler doesn't have a command-line api for defining custom code removal passes, but the compiler's architecture does allow for registering custom passes and a pass to remove a debugger statement should be trivial:

    if (n.isDebugger()) {
        compiler.reportChangeToEnclosingScope(n);
        n.detach();
    }

The general structure would follow:

https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/CheckDebuggerStatement.java