GSTJ / react-native-code-push-plugin

Code Push plugin to integrate seamlessly with Expo
15 stars 4 forks source link

Environment Variables #8

Open kibolho opened 8 months ago

kibolho commented 8 months ago

Hey @GSTJ thanks for this package! This is not this package's fault, but it is related to what this package sets out to solve. The package works perfectly, but when I try to add environment variables both in the .env and within the app.json, the bundle generated by the app center CLI does not resolve the variables. Any ideas what can be done?

Trying to bundle with this command:

appcenter codepush release-react -a org/app-name-d Production -t 1.1.1 --description "DESCRIÇÃO DA TASK" --extra-bundler-option="--reset-cache"

Since we are not using expo-cli to create the bundle maybe we need to add the env variable in a different way: https://github.com/lugg/react-native-config/issues/249

GSTJ commented 8 months ago

I use dotenv on most projects, as it's way more customizable than Expo's default env loading. You can use it to change environments dynamically; it's a breeze. EXPO_PUBLIC_ prefixing didn't ever entirely do the job for me anyway.

Here's a good article on it: https://elazizi.com/posts/environment-variables-in-react-native-the-right-way/

GSTJ commented 8 months ago

This does seem to be a Babel plugin on Expo itself, doing the environment resolution. It makes me think this package would resolve correctly if you're prefixing.

Have a look: https://github.com/expo/expo/blob/b77fef403277fb98f63ebcad8dd1bf4ab6b03700/packages/babel-preset-expo/src/inline-env-vars.ts#L23

Can you send some snippets on how this is being used? As long as you have babel-preset-expo installed, I'm confident you should be getting envs correctly.

kibolho commented 7 months ago

I was using the env variables in eas.json:

 "production": {
      "env": {
        "EXPO_PUBLIC_API_BASE_URL":  "my_base_url"
        }
}

And then using it as:

export const API_BASE_URL = process.env.EXPO_PUBLIC_API_BASE_URL;

And I also have babel-preset-expo installed.

But it didn't work with remote updates because it doesn't read the eas.json. I will test using it with .env files and reading it via dotenv. Thanks! @GSTJ

suwu150 commented 4 months ago

Is there a way to directly use expo. env? Currently, my project heavily relies on expo. env @GSTJ