简体   繁体   English

"在反应生产构建中删除控制台日志语句?"

[英]Remove console log statements in react production build?

I have a basic react app (created using create react app<\/strong> )我有一个基本的反应应用程序(使用create react app 创建<\/strong>)
I have gone through a few links related such as babel plugin installation npm i babel-plugin-transform-remove-console --save<\/strong> Guide to remove console log using babel plugin<\/a>我已经浏览了一些相关的链接,例如 babel 插件安装npm i babel-plugin-transform-remove-console --save<\/strong> Guide to remove console log using babel plugin<\/a>

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

You could try this, in src/App.js , the root component of your app:你可以在src/App.js尝试这个,你的应用程序的根组件:

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

Just before the return .就在return之前。

You can try those packages to override the config:您可以尝试使用这些包来覆盖配置:

Note : from the document ofreact-app-rewired , this would break the the "guarantees" that CRA provides.注意:从react-app-rewired的文档来看,这会破坏 CRA 提供的“保证”。 So you should be careful before using it.所以你在使用之前应该小心。

npm i -D customize-cra react-app-rewired babel-plugin-transform-remove-console

Modify your package.json, replace react-scripts to react-app-rewired except the reject .修改你的 package.json,将react-scripts替换为react-app-rewired除了reject Once complete, your scripts should look like this:完成后,您的脚本应如下所示:

"scripts": {
 "start": "react-app-rewired start",
 "build": "react-app-rewired build",
 "test": "react-app-rewired test",
 "eject": "react-scripts eject"
}

Then create a file:然后创建一个文件:

touch config-overrides.js
// config-overrides.js
const { override, addBabelPlugins } = require('customize-cra')

module.exports = override(
  addBabelPlugins(
    "transform-remove-console"
  )
)

Finally, after run npm run build , all console.log will be removed.最后,运行npm run build ,所有console.log将被删除。

Also, I have another answer regarding this similar question , you can check out other answers as well.另外,对于这个类似的问题,我还有另一个答案,您也可以查看其他答案。

@pmiranda 's solution should be changed to: @pmiranda 的解决方案应更改为:

if (process.env.NODE_ENV === "production")
    console.log = function no_console() {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 EXTJS:Sencha CMD Production版本不会删除console.log语句 - EXTJS: Sencha CMD Production build doesnot remove console.log statements 在 Visual Studio 2019 中进行发布构建时,有没有办法从 js 文件中删除 debugger &amp; console.log 语句? - Is there a way to remove debugger & console.log statements from js files when doing a release build in Visual studio 2019? uglify-js可以删除console.log语句吗? - Can uglify-js remove the console.log statements? React 不会更新下拉选项,并且不匹配 console.log 语句 - React will not update dropdown options,and does not match console.log statements 如何在发布版本中删除 console.log 行? - How to remove console.log lines on release build? 如何从生产汇总构建中删除 React PropTypes? - How to remove React PropTypes from production rollup build? React App,从生产版本中删除调试代码 - React App, remove debug code from production build 如何从缩小或合并的 javascript 文件中删除所有 console.log 语句 - How to remove all console.log statements from your minified or combined javascript file 从生产构建中删除依赖项 - Remove dependency from a production build NuxtJS:在生产环境中禁用 console.log - NuxtJS: Disable console.log in production env
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM