TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸

Remove all console logs from a Staging/Production build of React Native

March 8, 2021

Ziel

console.log (und Co.) in Staging/Production entfernen, aber in Dev behalten.

Plugin

code
npm i -D babel-plugin-transform-remove-console

Basis Config:

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

Optional bestimmte Logs behalten:

code
{
  "plugins": [["transform-remove-console", { "exclude": ["error", "warn"] }]]
}

Environments mit .env

Wenn du nicht auf NODE_ENV vertraust, kannst du je Umgebung .env einlesen.

Beispiel:

code
npm i -D babel-plugin-inline-dotenv
code
module.exports = function (api) {
  api.cache(true);

  return {
    presets: ["module:metro-react-native-babel-preset"],
    env: {
      development: {
        plugins: [["inline-dotenv", { path: ".env" }]],
      },
      staging: {
        plugins: [["inline-dotenv", { path: ".env.staging" }], "transform-remove-console"],
      },
      production: {
        plugins: [["inline-dotenv", { path: ".env.production" }], "transform-remove-console"],
      },
    },
  };
};

Hinweise

  • In React Native ist NODE_ENV in Release Builds normalerweise production.
  • Wenn du console.error behalten willst, nutze exclude.

Links