zetachang / react-native-dotenv

A Babel preset let you import application configs from .env file (zero runtime dependency)
MIT License
915 stars 68 forks source link

How to handle different environments (e.g. dev/prod)? #2

Open JulianKingman opened 7 years ago

JulianKingman commented 7 years ago

How do you handle different possible environments? For example locally on my development machine I would want my server URL to be localhost/something, vs. example.com/something. Do you have a solution for that?

react-native-config handles it with different environment files, but as you point out it requires native configuration (as explained here)

zetachang commented 7 years ago

You could set BABEL_ENV as production and the loader will lookup for .env.production file instead of dev one.

The setup is something like https://github.com/zetachang/react-native-dotenv/tree/master/babel-plugin-dotenv/test/fixtures/prod-env.

JulianKingman commented 7 years ago

Forgive me, I don't quite know what I'm looking at here. How do I set BABEL_ENV as production? I see in the test.js file something like this: process.env['BABEL_ENV'] = 'production';

Is that all that's necessary? Then I change that as desired when starting react-native?

zetachang commented 7 years ago

Sorry for not making this more clearer. Actually you can specifying dev=true in your bundle url to make the loader looking up for .env.production.

However, React Native is setting this for you when your are shipping a release build. You can verify this manually by visiting http://localhost:8081/index.ios.bundle?platform=ios&dev=true.

This works on react-native v0.31 last time I've tried.

JulianKingman commented 7 years ago

OK, pretend you're talking to a cat, there's a lot here that's new to me. I want to set the environment to production to use the production server URL, so I [insert steps here] and then run react-native run-ios. What are the steps I'm missing and how do I do them? I.e. how do I set dev=true, or dev=false/production=true, and/or can I set other environments such as beta=true, which will use .env.beta to connect to the beta server?

zetachang commented 7 years ago

Would you mind providing the environment you are using, iOS or Android?

JulianKingman commented 7 years ago

iOS

zetachang commented 7 years ago

Checkout ios/PROJECT_NAME/AppDelegate.m, there will be a line looks like this

jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index.ios" fallbackResource:nil];

Change it too

jsCodeLocation = [RCTBundleURLProvider jsBundleURLForBundleRoot:@"index.ios" packagerHost:@"localhost" enableDev:FALSE enableMinification:FALSE];

Now you can toggle the enableDev to set the dev param in the final bundle url.

JulianKingman commented 7 years ago

To be clear, I'm not too concerned with the bundle, I just want to be able to run it with the flags in the simulator (react-native run-ios).

zetachang commented 7 years ago

Try: react-native run-ios --configuration Release, it's available since since v0.39.

vongohren commented 7 years ago

is it possible to run react-native run-ios --configuration dev? or react-native run-ios --configuration staging?

I have read that the capital letter is quite sensitive

temitope commented 7 years ago

@Snorlock did you figure out if the staging and dev options worked? also wondering what you ended up using as a final solution for your config needs...

As I recall you were in an issue conversation in the react-native-config issue section too yeah? i have the video someone made in response to your question in a tab somewhere...

vongohren commented 7 years ago

I ended up creating my own fork of the react-native-config version. But it was much helpful info from the collaborators.

Here is my version: https://bitbucket.org/enturas/react-native-config-swift

dancomanlive commented 6 years ago

@vongohren npm install react-native-config-swift --save npm ERR! code E404 npm ERR! 404 Not Found: react-native-config-swift@latest

vongohren commented 6 years ago

@dancomanlive im not working there anymore, but I believe they went away from swift.