Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

transform-remove-console exclude options not working on React Native project #950

Open
The0racle opened this issue Apr 8, 2019 · 8 comments

Comments

@The0racle
Copy link

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

@alphacat2018
Copy link

Same problem with React Native 0.59.8

@bareynol
Copy link

bareynol commented Oct 7, 2019

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"]
    }
  }
}

@bareynol
Copy link

bareynol commented Oct 7, 2019

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

@bareynol
Copy link

bareynol commented Oct 8, 2019

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"],
    }
  }
}

@twinkal00
Copy link

how to do this code in react js

@romchambe
Copy link

Thanks @echolocation your solution works for me in RN

@seanleblancicdtech
Copy link

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:

How are you specifying this value on the command line?

@seanleblancicdtech
Copy link

For those using react-native and generating an artifact like an .ipa or a .apk, how do you affirm a before/after for seeing that console.log is not being included in builds that should be stripping it out?
MobSF is still showing our .ipa as possibly containing calls to _NSLog function. This may be coming from other code not our own, however, so I don't know if there is a way to inspect the result (or some intermediate files) to see that console.log is being stripped out for certain builds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants