1

We've got a lot of calls to our logging methods (that just wrap console.log) all throughout our JS in our MVC3 web app and I'd like to remove them from the JavaScript when we build our test and production builds.

Currently we're using the bundling and minification nuget package to bundle and minify our JS into one big minified file but I'd like to have it rip out the calls to the logging methods as well.

We do have a mechanism in place that replaces the logging methods with empty functions so they won't do any work in production, but they are still called and various arguments are passed in. On top of this, there are "large" strings that are passed and those could be removed, reducing filesize.

The ideal solution in my mind would be to somehow parse the JavaScript and detect / remove the calls to those methods. Preferably in some sort of JavaScript engine and not just a regular expression.

Either way, I just want my calls to my logging methods removed in the final JavaScript that is served up in production. Does anyone know how I'd accomplish this additional minification?

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
  • 1
    Have a look here: http://stackoverflow.com/questions/9816845/how-to-instruct-ajax-minifier-to-remove-console-log-from-javascript – Valamas Jun 04 '12 at 22:25
  • very nice, that looks really promising. I'll have to thoroughly investigate that – Allen Rice Jun 04 '12 at 22:30
  • Valamas hey that "preprocessor" stuff works great, can you post that as an answer and i'll accept it? – Allen Rice Jun 04 '12 at 22:40
  • thanks, but you should delete this question. It is not my answer to give. have a good one. – Valamas Jun 04 '12 at 23:04
  • Well one thing that is interesting that I've discovered is that this prepropcessor type stuff was added to the microsoft ajax minifier. I was not aware that they re-used the same minifier for the `IBundleTransform` work that they did for MVC4. Technically this question is somewhat different because I'm specifically interested in the new bundling and minification stuff, not the ajax minifier. Since the new stuff is likely to be more popular and come up in searches more, I'll go ahead and leave this question. – Allen Rice Jun 04 '12 at 23:09

2 Answers2

5

Yep, the IBundleTransform interface was designed for this scenario. In RC bits here's what we envisioned:

new Bundle("~/bundles/js", new LogRemoverTransform(), new JsMinify());

Basically, you construct a bundle and chain two transforms, first stripping your log methods, and then running through normal minification. Prior to RC, you would have to do the composition inside of your IBundleTransform.

Ry-
  • 218,210
  • 55
  • 464
  • 476
Hao Kung
  • 28,040
  • 6
  • 84
  • 93
0

You could write your own implementation of IBundleTransform that first removes calls to your logging methods via a regular expression and then calls the default bundling and minification functionality. As long as your calls are fairly simple, it shouldn't be hard to come up with. It might get tricky though, depending on how you call your logging code.

For example, it'd be fairly hard (for me) to build a regex that would catch the entirety of a logging call like this:

NS.log(function () { return "this is going to be hard to parse"; }());

But, as long as you don't log like that, it shouldn't be a difficult regex to write.

Allen Rice
  • 19,068
  • 14
  • 83
  • 115