Minify: `transform-remove-console` exclude options not working on React Native project

Created on 8 Apr 2019  路  6Comments  路  Source: babel/minify

Describe the bug

I'm using the exclude options but it doesn't seem to have any effect as all logs are removed.

To Reproduce

.babelrc

{
  "presets": ["react-native", "react-native-dotenv"],
  "env": {
    "production": {
        "plugins": [ ["transform-remove-console", { "exclude": [ "error", "warn", "info"] }] ]
    }
  }
}

Actual Output

All console.* output is removed.

Expected Output

console.warn, console.info and console.error should still work.

Configuration

How are you using babel-minify?

"metro-minify-uglify"

Additional context

React Native 0.54.4

Most helpful comment

For anyone that happens upon this, and has been affected by it, I've found that using the following as my babel.config.js works properly to include transform-remove-console in production builds for react native projects, and properly uses the exclude property if specified:

module.exports = function(api) {
  api.cache(true);
  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", {"exclude": ["error", "warn", "info"]}]]
    }
  } else {
    return {
      "presets": ["module:metro-react-native-babel-preset"],
    }
  }
}

All 6 comments

Same problem with React Native 0.59.8

I am using React Native 0.60.5, and transform-remove-console is not removing debugging statements at all. My babel.config.js looks like this:

module.exports = {
  "presets": ["module:metro-react-native-babel-preset"],
  "env": {
    production: {
      plugins: ["react-native-paper/babel", "transform-remove-console"]
    }
  }
}

I updated to react-native 0.61.2 and modified my babel.config.js to return a function with the signature function(api)

module.exports = function(api) {
  console.log("BABELCONFIG", api.version, api.env("production"), api.env(), process.env.NODE_ENV, process.env.BABEL_ENV);
  return {
    "presets": ["module:metro-react-native-babel-preset"],
    "plugins": ["react-native-paper/babel", "transform-remove-console"]
  }
}

During build I get the following output:

transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined
transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 true production production production
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined
transform[stdout]: BABELCONFIG 7.6.2 false undefined production undefined

using plugins at top level allows the listed plugins to work, but using the env: {production: { plugins: [...]}} format does not work.

It appears as though BABEL_ENV is sometimes set to 'production' and sometimes undefined, and NODE_ENV is not being used when BABEL_ENV is undefined. I'm at a loss as to why

For anyone that happens upon this, and has been affected by it, I've found that using the following as my babel.config.js works properly to include transform-remove-console in production builds for react native projects, and properly uses the exclude property if specified:

module.exports = function(api) {
  api.cache(true);
  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", {"exclude": ["error", "warn", "info"]}]]
    }
  } else {
    return {
      "presets": ["module:metro-react-native-babel-preset"],
    }
  }
}

how to do this code in react js

Thanks @echolocation your solution works for me in RN

Was this page helpful?
0 / 5 - 0 ratings

Related issues

boopathi picture boopathi  路  3Comments

GabbeV picture GabbeV  路  3Comments

jacobmischka picture jacobmischka  路  5Comments

curiousdannii picture curiousdannii  路  3Comments

bosens-China picture bosens-China  路  4Comments