使用ng build --prod剥离所有注释和console.logs,可能吗? [英] Stripping all comments and console.logs with ng build --prod, possible?

查看:78
本文介绍了使用ng build --prod剥离所有注释和console.logs,可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MEAN,Angular 2,Node/Express,Angular-CLI和ng build --prod来构建我的应用程序,并且我在整个应用程序中都有大量注释掉的废弃代码和十亿个调试console.log语句.有没有一种方法可以让构建过程在构建时删除所有注释和console.log语句?手动执行此操作的想法很可怕!

I am using MEAN, Angular 2, Node/Express, Angular-CLI and ng build --prod to build my app and I have a travesty of commented out throwaway code and a billion debugging console.log statements throughout my app. Is there a way to have the build process remove all comments and console.log statements when it builds? The thought of doing it manually is scary!

推荐答案

我有一个简单的解决方法.将此代码放入main.ts

I have simple fix. Put this code in main.ts

if(env === 'prod') { // assuming you have env variable configured
  // check if window exists, if you render backend window will not be available 
  if(window){
      window.console.log = function(){};
   }
}  

这篇关于使用ng build --prod剥离所有注释和console.logs,可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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