[英]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.