如何在我们的mvc3网络应用程序的产品版本中删除JS日志记录调用? [英] How to remove JS logging calls in our prod build of our mvc3 web app?

查看:85
本文介绍了如何在我们的mvc3网络应用程序的产品版本中删除JS日志记录调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在MVC3网络应用程序中的整个JS中都调用了许多日志记录方法(仅包装console.log),我想在构建测试和生产版本时将它们从JavaScript中删除构建.

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.

当前,我们正在使用捆绑和缩小nuget包进行捆绑和缩小将我们的JS压缩成一个较小的大文件,但我也想让它也删除对日志记录方法的调用.

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.

在我看来,理想的解决方案是解析JavaScript并检测/删除对这些方法的调用.最好是在某种JavaScript引擎中,而不仅仅是正则表达式.

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.

无论哪种方式,我都只希望在生产中提供的最终JavaScript中删除对日志记录方法的调用.有人知道我将如何完成这一额外的任务吗?

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?

推荐答案

是的,IBundleTransform接口是针对这种情况而设计的.在RC位中,这是我们所设想的:

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());

基本上,您构造了一个捆绑和链接两个转换,首先剥离日志方法,然后进行常规缩小.在进行RC之前,您必须在IBundleTransform内部进行合成.

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.

这篇关于如何在我们的mvc3网络应用程序的产品版本中删除JS日志记录调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆