Don't clobber user-defined environment variables

Summary:
Re: javache 's suggestions from https://github.com/facebook/react-native/pull/7878. Didn't want to deal with the merge conflict so I'm opening a separate PR. Here's the original justification:

If I want to set NODE_ENV to "baconator", I should be allowed to. Mutating global state that most devs assume to be immutable is just abysmal dev practice, especially since this mutation only happens when you're building for prod, not running on the simulator.

To test this, run env NODE_ENV=baconator ./gradlew assembleRelease with babel-plugin-transform-inline-environment-variables in your app/.babelrc. You'll see that the final app has NODE_ENV=production.

As a side note, running with babel-plugin-transform-inline-environment-variables in the top-level .babelrc crashes horribly with a compiler error.

For anybody who runs into this bug and doesn't feel like waiting for this to get merged, I wrote a quick babel plugin to remove assignments to process.env, which is sufficient to fix this issue.
Closes https://github.com/facebook/react-native/pull/8057

Differential Revision: D3419950

Pulled By: javache

fbshipit-source-id: dc541cad0a99906433e5c14bbc93ce66b4ed325e
This commit is contained in:
Valeri Karpov 2016-06-10 15:21:30 -07:00 committed by Facebook Github Bot 6
parent 14d0b5c0f1
commit 402e7eaac2
1 changed files with 6 additions and 1 deletions

View File

@ -25,7 +25,12 @@ function buildBundle(args, config, output = outputBundle, packagerInstance) {
// This is used by a bazillion of npm modules we don't control so we don't
// have other choice than defining it as an env variable here.
process.env.NODE_ENV = args.dev ? 'development' : 'production';
if (!process.env.NODE_ENV) {
// If you're inlining environment variables, you can use babel to remove
// this line:
// https://www.npmjs.com/package/babel-remove-process-env-assignment
process.env.NODE_ENV = args.dev ? 'development' : 'production';
}
const options = {
projectRoots: config.getProjectRoots(),