记录的方法对我现有的项目也不起作用,但是在新的测试项目中起作用。我尝试了许多依赖项升级,添加/删除了dep,插件等,试图找出问题的根源,但是没有运气。所以最后我采用了这种解决方法:
module.exports = {
plugins: ['@babel/plugin-proposal-numeric-separator','lodash'].concat(
process.env.NODE_ENV === 'production' ? ['transform-remove-console'] : []
),presets: [
'module:metro-react-native-babel-preset',['@babel/env',{ targets: { node: 6 } }]
]
};
,
这应该有效(推荐方法)
const presets = ['module:metro-react-native-babel-preset'];
const plugins = ['module:react-native-dotenv'];
if (process.env.ENV === 'prod') {
plugins.push('transform-remove-console');
}
module.exports = {presets,plugins};
或者如果你不想为此使用任何包,你可以这样做。
把它放在index.js
if(!__DEV__){
console.log = () => {}
}
,
如果您的项目中不存在.babelrc文件,那么您也可以手动创建一个新的.babelrc文件,并按以下方式进行设置。
{
"presets": ["react-native"],"env": {
"production": {
"plugins": ["transform-remove-console"]
}
}
}
来源:-
https://babeljs.io/docs/en/configuration#babelrc
https://facebook.github.io/react-native/docs/performance#using-consolelog-statements
本文链接:https://www.f2er.com/3162655.html