在React生产版本中删除控制台日志语句? [英] Remove console log statements in react production build?

查看:328
本文介绍了在React生产版本中删除控制台日志语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的React应用(使用 create react app 创建)
我已经浏览了一些相关的链接,例如babel插件安装 npm i babel-plugin-transform-remove-console-保存 使用以下命令删除控制台日志的指南babel插件

I have a basic react app (created using create react app)
I have gone through a few links related such as babel plugin installation npm i babel-plugin-transform-remove-console --save Guide to remove console log using babel plugin

 {
  "env": {
    "production": {
      "plugins": ["transform-remove-console"]
    }
  }
}

我已经将上面的配置包含到babel.rc文件中,但是仍然不能解决我的问题.我可以在生产版本中看到日志.请让我知道我要去哪里了.

I have included the above config to babel.rc file but still, it doesn't solve my problem. I am able to see the logs in the production build. Kindly let me know where I am going wrong.

推荐答案

您可以在src/App.js中尝试此应用的根组件:

You could try this, in src/App.js, the root component of your app:

if (process.env.REACT_APP_STAGE === 'PROD')
  console.log = function no_console() {};

就在return之前.

这篇关于在React生产版本中删除控制台日志语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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