I'd like to make my JS code production ready by stripping out all console.log("blah blah")
debugging statements. I'm confused by this popular SO answer (code below) on how to do this using Google's closure compiler, a popular JS minifier/compiler.
/** @const */
var LOG = false;
...
LOG && log('hello world !'); // compiler will remove this line
...
//this will even work with `SIMPLE_OPTIMALIZATIONS` and no `--define=` is necessary !
Two questions:
Multiples files: How does the above code work with multiple files (basic example below)? It has to be in a closure, right? Doesn't this then mean you have to put this code on each page? Also, doesn't it also then mean you have to change all the variables you want to be global from
var foo='bar'
tovar window.foo='bar';
inside these closures on each page?Minor issue: Shouldn't it be
console.log('...')
rather thanlog('...')
becauselog()
gives an error? Am I missing something obvious here?
<script src='/assets/js/file1.js'></script>
<script src='/assets/js/file2.js'></script>
contents of file1.js:
var foo='bar';
console.log("foo"+foo);
contents of file2.js
var baz='bazzy';
console.log("baz"+baz);