react-native-dotenv icon indicating copy to clipboard operation
react-native-dotenv copied to clipboard

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

Open levity opened this issue 7 years ago • 5 comments

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.

levity avatar Feb 23 '17 20:02 levity

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.

zetachang avatar Feb 24 '17 14:02 zetachang

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 [email protected] 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 .

levity avatar Mar 10 '17 23:03 levity

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

lucasbento avatar Mar 22 '17 15:03 lucasbento

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 [email protected] 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 .

levity avatar Mar 23 '17 21:03 levity

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

jamesholcomb avatar Nov 22 '17 16:11 jamesholcomb