[英]React Native 0.60.3 babel-plugin-transform-remove-console not working
I am trying to remove console.log outputs from my react-native application's output, but when I run我正在尝试从我的 react-native 应用程序的输出中删除 console.log 输出,但是当我运行时
ENVFILE=.env.production react-native run-android --variant=release
ENVFILE=.env.production react-native run-android --variant=release
and和
adb logcat
亚行日志猫
I still see my app's data being logged to the console.我仍然看到我的应用程序的数据被记录到控制台。
I used the following documentation: https://facebook.github.io/react-native/docs/performance.html#using-consolelog-statements .我使用了以下文档: https : //facebook.github.io/react-native/docs/performance.html#using-consolelog-statements 。
Here is my .babelrc file:这是我的 .babelrc 文件:
{
"presets": ["react-native"],
"env": {
"production": {
"plugins": ["transform-remove-console"]
}
}
}
What am I missing ?我错过了什么? Im on react-native 0.60.3 and using "babel-plugin-transform-remove-console": "^6.9.4",
我在 react-native 0.60.3 上使用 "babel-plugin-transform-remove-console": "^6.9.4",
I have "@babel/core": "^7.5.5"
and "react-native": "^0.60.5"
我有
"@babel/core": "^7.5.5"
和"react-native": "^0.60.5"
The approach descibed in React Native Documentation was not working for me. React Native 文档中描述的方法对我不起作用。
After many try and error and exploring issues on GitHub I got it working :经过多次尝试和错误并在 GitHub 上探索问题后,我得到了它的工作:
In babel.config.js add this -在babel.config.js添加这个 -
module.exports = api => {
const babelEnv = api.env();
const plugins = [];
//change to 'production' to check if this is working in 'development' mode
if (babelEnv !== 'development') {
plugins.push(['transform-remove-console', {exclude: ['error', 'warn']}]);
}
return {
presets: ['module:metro-react-native-babel-preset'],
plugins,
};
};
To see changes run using
npm start -- --reset-cache
要查看使用
npm start -- --reset-cache
运行的更改
More Info at更多信息在
Using babel.config.js
instead of .babelrc
, it seems that process.env.BABEL_ENV
is used to determine whether to include configs listed under env.production
.使用
babel.config.js
而不是.babelrc
,似乎process.env.BABEL_ENV
用于确定是否包含env.production
配置。 However, process.env.BABEL_ENV
is set to undefined
during build.但是,
process.env.BABEL_ENV
在构建期间设置为undefined
。
To get around this, I'm returning a different object depending on if process.env.BABEL_ENV
OR process.env.NODE_ENV
indicate production build.为了解决这个问题,我根据
process.env.BABEL_ENV
OR process.env.NODE_ENV
指示生产版本返回一个不同的对象。
YMMV.天啊。
module.exports = function(api) {
api.cache(true); // necessary
if (process.env.NODE_ENV === 'production' || process.env.BABEL_ENV === 'production') {
return {
"presets": ["module:metro-react-native-babel-preset"],
"plugins": ["react-native-paper/babel", "transform-remove-console"]
}
} else {
return {
"presets": ["module:metro-react-native-babel-preset"],
}
}
}
install babel-plugin-transform-remove-console安装babel-plugin-transform-remove-console
yarn add babel-plugin-transform-remove-console -D
then add follow code in babel.config.js like this然后在babel.config.js 中添加如下代码
module.exports = function (api) {
const babelEnv = api.env();
api.cache(true);
const plugins = [
[];
if (babelEnv === 'production') {
plugins.push(['transform-remove-console', {exclude: ['error', 'warn']}]);
}
return {
presets: ['babel-preset-expo'],
plugins,
};
};
then run this command然后运行这个命令
yarn start --reset-cache
this will remove console.log in the production build.这将删除生产版本中的 console.log。 if you wanna a test in development you can pass development instead of production
如果你想在开发中进行测试,你可以通过开发而不是生产
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.