使用 console.log 语句是 JavaScript 应用程序(包括 React Native 应用程序)中最常见的调试模式之一。发布 React Native 应用程序时将控制台语句保留在源代码中可能会导致 JavaScript 线程出现一些大瓶颈。
npm install babel-plugin-transform-remove-console --save-dev
yarn add babel-plugin-transform-remove-console -D
编辑 babel.config.js
module.exports = {
presets: ['module:metro-react-native-babel-preset'],
env: {
production: {
plugins: ["transform-remove-console"], //removing consoles.log from app during release (production) versions
},
},
};
或.babelrc
{
"env": {
"production": {
"plugins": ["transform-remove-console"]
}
}
}
只需将这些行放入
index.js
文件中:
if (!__DEV__) {
console.log = () => {}
}