11

I'm using console.assert to test/debug but I'd like to remove this in production code. I'm basically overwriting console.assert to be a noop function right now but wondering if there's a better way. It would be ideal if there was some javascript preprocessor to remove this.

jhchen
  • 14,355
  • 14
  • 63
  • 91
  • @wukong Except it's ... JavaScript :-) –  Sep 13 '11 at 06:35
  • 4
    What about using a test-framework and a debugger? :-) Don't even let that `console.assert` get in there! –  Sep 13 '11 at 06:36
  • 4
    I do that too but found asserts really useful catching errors very early by enforcing expected state wherever I can – jhchen Sep 13 '11 at 18:13

3 Answers3

5

UglifyJS2 does this easily: When running the uglifyjs command, enable the compressor and tell it to discard console.* calls:

uglifyjs [input files] --compress drop_console

Quick example:

function doSomething() {
    console.log("This is just a debug message.");
    process.stdout.write("This is actual app code.\n");
}
doSomething();

... gives this when compiled with the above command:

function doSomething(){process.stdout.write("This is actual app code.\n")}doSomething();

This might be an UglifyJS2 bug, but be careful about side effects in those calls!

function doSomething() {
    var i = 0;
    console.log("This is just a debug message." + (++i));
    process.stdout.write("This is actual app code." + i + "\n");
}
doSomething();

...compiles to

function doSomething(){var i=0;process.stdout.write("This is actual app code."+i+"\n")}doSomething();

... which writes i as 0 instead of 1!

  • I guess, one can have some heavy checks in an assert. If you just substitute the call to "nothing", the heavy check will still be processed. Therefore, removing an assert only has meaning if you remove the call and all javascript inside the parenthesis. Of course, inside "console" calls, there should be no side-effects! The program should work the same with or without the whole assertion/log. – André Caldas Aug 01 '20 at 22:36
3

Try Closure Compiler, in advanced mode it removes empty functions (and much more).

kapex
  • 28,903
  • 6
  • 107
  • 121
  • I tried to compile console.assert(1, "It works"); and closure turns it into console.a(1,"It works"); – jhchen Sep 13 '11 at 18:38
  • 1
    Also closure will not remove empty functions if I call those empty functions with parameters. Ex. Try compiling console.log = function() {};console.log(1, Math.random()); – jhchen Sep 13 '11 at 19:10
  • Maybe `console` is something special to the compiler (or just unknown), but if you declare a variable before using it, the compiler removes everything (just as it is expected and should be): the object, the function and the call, leaving nothing behind. I tested `var console = {}; console.log = function(x,y) {}; console.log(1, Math.random());` - edit: There's no warning for `foo(1,Math.random());` and the line isn't touched by the compiler, so I guess it's assumed to be defined elsewhere and therefore shouldn't be optimized to aggressively (just as `console`). – kapex Sep 13 '11 at 20:31
  • See this thread http://stackoverflow.com/questions/2934509/exclude-debug-javascript-code-during-minification – Indolering Nov 07 '13 at 21:06
1

Another tool is UglifyJS which is used with nodejs. It's fast and got a lot of options for you to check out.

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Does UglifyJS also remove empty functions? – jhchen Sep 13 '11 at 18:17
  • @jcmoney: I don't think so but look through the function list in the link. Do you want to keep the empty function in your nonproduction code or do you want to detect it (for removal) there as well? – chelmertz Sep 13 '11 at 19:38