0

Hi everyone I searched and learned console.logs hurts performance.So I choose to disable logs when creating apk.I find away but doesn't worked for me. They all say add this lines to .babelrc file but I don't have any. so I created .babelrc and added this lines but console logs still appering in browser. lines.

I installed this package

npm i babel-plugin-transform-remove-console --save-dev

So what should I change

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

}

project structure

structure

ksav
  • 20,015
  • 6
  • 46
  • 66
masterAvatarr
  • 113
  • 1
  • 10

3 Answers3

1

a simple solution to remove all logs from any where:

if (!__DEV__) {
  for (const iterator of Object.keys(global.console)) {
    global.console[iterator] = () => null;
  }
}
wxsm
  • 556
  • 4
  • 14
0

At very beginning in App.js Paste this code

console.disableYellowBox = true;

if (!__DEV__) {
    console.log = () => {};
    console.warn = () => {};
    console.error = () => {};
}
Zuhair Naqi
  • 1,162
  • 11
  • 21
0

It should be added to babel.config.js instead.

The default babel.config.js looks like this:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
};

You can adapt it further

module.exports = api => {
  const isProd = api.env('production');

  const plugins = isProd ? ["transform-remove-console"] : []

  return {
    presets: ['module:metro-react-native-babel-preset'],
    plugins,
  }
}

When building a production build with the following command, open android/app/src/main/assets/index.android.bundle to see whether there are any console statements.

react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/

This will only remove console statements from your code, any console statements within libraries within node_modules will still be present.

Dan
  • 8,041
  • 8
  • 41
  • 72