11

It would be nice if I could put debug/console log statements in my javascript, then have a js minifier/compacter remove these when it compiles.

Does this exist?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • I am not sure if I unedrstood your question. Which minifer and why? – genesis Aug 01 '11 at 22:25
  • removing debug statements is a simple regex if the arguments are always on a single line. Shouldn't be hard to do a global search for `alert` and `console` to be able to find any and all debugger calls. – zzzzBov Aug 01 '11 at 22:33
  • I have some code that I want to remove comments and debug statements from, but not otherwise minify to preserve the ability to debug. I couldn't find what I wanted so I ended up writing my own python script to do exactly what I wanted. My brain tells me that there should be something out there that could do less than full minification, but I didn't find it last time I looked. – jfriend00 Aug 01 '11 at 23:32

3 Answers3

4

Not that I'm aware of, though Google's Closure Compiler will allow you to accomplish something similar if you wish:

/** @define {boolean} */
var DEBUG_MODE = true;

var debug;
if (DEBUG_MODE) {
    /** @param {...} args */
    debug = function(args) { console.log.apply(console, arguments); }
} else {
    /** @param {...} args */
    debug = function(args) {}
}

debug('foo', {a: 5});

If you set DEBUG_MODE to false (you can even do this on the command-line if you wish), then when advanced optimizations are enabled (you must do some work if you want to use these, but they're handy), the empty implementation of debug will be used instead, which will cause calls to it to be "inlined" (optimized out of existence).

You could extend this to have more complicated debugging functions than the above (which simply forwards to console.log); for instance you could make one that accepted a function argument that is called in debug mode, and not called outside of debug mode.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
0

None of the major minifiers implement that, I think its because its not part of any standard (they are undefined in Internet Explorer for example). They expect that you might have declared your own debug/javascript function.

Charles Barbier
  • 835
  • 6
  • 15
  • they are identified in internet explorer if your have debugger flag set at the internet options and you have visual studio http://stackoverflow.com/questions/170164/debugging-javascript-in-ie/170170#170170 and http://www.slideshare.net/jboutelle/how-to-debug-javascript-in-ie – Felipe Sabino Aug 01 '11 at 22:33
0

In Perl with JavaScript::Minifier

#!/usr/bin/perl -w
use strict;
use JavaScript::Minifier qw(minify);

open(INFILE, $ARGV[0]) or die;
open(OUTFILE, ">$ARGV[1]") or die;
my $minJs =  minify(input => *INFILE,  stripDebug => 1);
$minJs =~ s#console.log\(.*\);##g;
$minJs =~ s#alert\(.*\);##g;
print OUTFILE $minJs;
close(INFILE);
close(OUTFILE); 

example usage: ./jsMinifier.pl js.js js-min.js

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62