views:

146

answers:

3

I want to log to the console when I'm using unminimized JS files.

Comments are taken out already when I minimize JS. I'm wondering if there's a way I can write a command that isn't commented out but will still be taken out when I minimize the javascript file.

+1  A: 

Unless whatever you're using to minimize your JS supports conditional statements I don't think you can do this.

Why not just log things if a certain variable is set?

Diodeus
+2  A: 

I think I'd be pretty upset if a Javascript minimizer changed the behaviour of my code based on some funny/clever/odd code construct. How could you ever be sure that code construct isn't there intentionally?

As has been suggested, have a variable that disables logging. Then as part of your minimize script or batch job, you can swap that variable to its non-logging state using sed (for example) before minimizing.

Grant Wagner
+1  A: 

If your goal is just to reduce the js size you can separate you logging functions into a separate file.

In your "main" js add a function function doLogging(object){} then in your separate logging functions file replace the function with function doLogging(object){/*your logging code*/};

Just remember to include your main js before the logging js. When you minify just comment out the logging script tags from the html. This way you will only have one (or a couple of) empty function definitions in the minified js and one line of code calling those functions per loggingn action.

Gene