Remove console log statements in react production

2020-07-18 11:29发布

问题:

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"]
    }
  }
}

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.

回答1:

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

Just before the return.