How to remove JS logging calls in our prod build of our mvc3 web app?

不羁岁月 提交于 2019-12-08 07:28:54

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/10889237/how-to-remove-js-logging-calls-in-our-prod-build-of-our-mvc3-web-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!