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

Feature request: .env.development.ios and .env.development.android #11

Open levity opened 7 years ago

levity commented 7 years ago

It would be helpful to be able to have separate .env files for ios vs. android platforms. I would be interested in developing this myself and creating a PR if no one else wants to. Any pointers on how to do this would be appreciated.

zetachang commented 7 years ago

This is really helpful! Would love to help if you want to work on it.

Some solution I have in mind so far is like the following,

// Before transpile
import { FOO } from "react-native-dotenv"
var a = FOO;
// After
var a;
if (require('Platform').OS === 'ios') {
  a = "CONFIG_FOR_IOS"
} else if (require("Platform").OS === 'android') {
  a = "CONFIG_FOR_ANDROID"
}

Transformer from RN will try to inline require('Platform').OS as the actual platform currently bundled for.

levity commented 7 years ago

Cool! I haven't had a lot of time to dig into this so far, but I would like to find some. I'm a big fan of .env files and keeping everything as "12-factor app"-style as I can.

On Fri, Feb 24, 2017 at 6:21 AM David Chang notifications@github.com wrote:

This is really helpful! Would love to help if you want to work on it.

Some solution I have in mind so far is like the following,

// Before transpileimport { FOO } from "react-native-dotenv"var a = FOO;// Aftervar a;if (require('Platform').OS === 'ios') { a = "CONFIG_FOR_IOS" } else if (require("Platform").OS === 'android') { a = "CONFIG_FOR_ANDROID" }

Transformer from RN will try to inline https://github.com/facebook/react-native/blob/f30ab35e9278f120247f49d6a355e263cc357946/packager/src/JSTransformer/worker/__tests__/inline-test.js#L74%5D require('Platform').OS as the actual platform currently bundled for.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/zetachang/react-native-dotenv/issues/11#issuecomment-282302492, or mute the thread https://github.com/notifications/unsubscribe-auth/AAL0gzGsjG1zkS7u7b7Tj-K7EFTec30Nks5rfud4gaJpZM4MKeA3 .

lucasbento commented 7 years ago

This is interesting, I really want to know what your cases are to have different environment variables for each platform, @levity.

levity commented 7 years ago

So far the main use case I've found is that the iOS and Android simulators need different hostnames for API access in development, e.g. iOS can use "localhost" but Android can't.

On Wed, Mar 22, 2017 at 8:50 AM Lucas Bento notifications@github.com wrote:

This is interesting, I really want to know what your cases are to have different environment variables for each platform, @levity https://github.com/levity.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/zetachang/react-native-dotenv/issues/11#issuecomment-288443883, or mute the thread https://github.com/notifications/unsubscribe-auth/AAL0g-xDQUj35HA2694nVdW-crYsvuj5ks5roUNIgaJpZM4MKeA3 .

jamesholcomb commented 6 years ago

You can work around this by declaring two variables in the .env.

ServerUrl_Android=http://localhost:3030
ServerUrl_iOS=http://myserver.local:3030

Then in your app.js

import { ServerUrl_iOS, ServerUrl_Android } from "react-native-dotenv"
export const SERVER_URL = Platform.OS === "ios" ? ServerUrl_iOS : ServerUrl_Android