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 piler, a popular JS minifier/piler.
/** @const */
var LOG = false;
...
LOG && log('hello world !'); // piler 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);
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 piler, a popular JS minifier/piler.
/** @const */
var LOG = false;
...
LOG && log('hello world !'); // piler 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);
Share
Improve this question
edited May 23, 2017 at 12:06
CommunityBot
11 silver badge
asked Sep 16, 2013 at 21:20
tim petersontim peterson
24.4k63 gold badges186 silver badges303 bronze badges
2
-
log
is a generalized logging function which could simply be a wrapper forconsole.log
or could provide some other type of logging. It was simply used for demonstration. The definition is left as an exercise to the reader. – Chad Killingsworth Commented Sep 16, 2013 at 21:31 - @ChadKillingsworth thanks, I didn't know whether it was an exercise or something I hadn't learned yet about JS. – tim peterson Commented Sep 16, 2013 at 21:37
3 Answers
Reset to default 2If you have your code split up into multiple files, I would think you'd want to have a build process that joins them into one immediately invoked function. Then your var LOG = false;
can just be included once at the top.
If by "each page", you mean that you have separate JavaScript files per page that aren't meant to be joined together, then yes, you'd need to have that code at the top of each, but then you're also not taking as much advantage of Closure Compiler.
Regarding globals, yes, you'd need to use window
when setting, though I would hope you're defining only one global.
The use of log()
implies that someone defined a log()
function so that it's less verbose.
function log() {
return console.log.apply(console, arguments);
}
I think you don't understand the answer from your link. Author suggests you to create a new variable
var LOG = false;
then you simply write LOG && __code__ ;
( coressponds to if(LOG) __code__ ;
) and the code after && will not be processed, because of partial evaluation of boolean expressions. It will be processed, after you set LOG to true. The variable is usually called a "switch" or "flag".
By the way, Closure Compiler may edit your code, but that code must behave the same after "pilation" as before it. That is the main principle of all these minifiers and optimizers (they must not convert your Quick Sort into GTA 5). So don't use Closure Compiler to make your code work differently (it is not possible, if it is - they have a bug in it!).
Taking the OP's example, if you don't want to modify file1.js you can pile it with an additional file (noconsole.js) containing:
console.log = console.debug = console.info = function(){};
Such as:
google-closure-piler --js noconsole.js file1.js --js_output_file file1.out.js
As explained in my previous answer.