From 402e7eaac2f15b98bb15ce13198afe2393cb2503 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Fri, 10 Jun 2016 15:21:30 -0700 Subject: [PATCH] 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 --- local-cli/bundle/buildBundle.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/local-cli/bundle/buildBundle.js b/local-cli/bundle/buildBundle.js index d71b7515d27..d0bc3545cb0 100644 --- a/local-cli/bundle/buildBundle.js +++ b/local-cli/bundle/buildBundle.js @@ -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(),