1.1.1 • Published 3 years ago
expo-env-ts v1.1.1
expo-env
.env solution for React Native apps Built with EXPO
Expo does not give us much in terms of managing multiple environment based configuration. Atleast other than
productionanddevelopment. To overcome that, this is simply a workaround until a proper support is released by good folks at Expo.io.
How it works
Expopasses the options available inapp.jsonfile at the root of your React Native application, along with some other details, called asmanifestand can be accessed byExpo.Constants.manifest- We simply use this to pass our environment based variables and configuration using the
extrafield inside thatmanifest. - We create a new
app.jsonfile reading the[environment].config.jsfile and passing it within thatextraparameter.
What it involves
- Firstly we would need to make our
app.jsonto not be pushed to version control (i.egit), because whenever we will change our environment, we would be updating this file, so it will not make much sense to check this into you repository. - You will also create a configuration file for each of the environments you have. For ex. if you have
staging,productionanddevenvironments, you would create 3 files namedstaging.config.js,production.config.jsanddev.config.js - Every time there is a need to change the
envwe would simply run this module so that it injects properoptionswithinapp.json, and then restart ourpackagerusing eitherexpCLI or through theXDE GUItool.
Making it more seamless
- TO make it more seamless, we can simple add these as
npm scriptsin ourExpoapp'spackage.json, such as:
package.json
...
"scripts": {
...pre existing scripts,
"env": "expo-env --env=development",
"env:prod": "expo-env --env=prod"
}
...sample config file - i.e staging.config.js
module.exports = {
"endpoint": "https://some-endpoint.someapi.com",
"AWSKey": "Avsgvdye523"
}You would be getting the above config/ENV vars inside your application if you use Expo.Constants.manifest.extra to get those values.
Options / Arguments
--env=[env]: Specifies the environment config to be picked up. (default:development)--configPath=[path]: The path to all theenvfiles. for ex.--configPath=./configwill look into theconfigfolder. (default:./)--template=[templatename]: The Name postfix for the env files. i.e env.templatename. If values passed is.env.jsit will look for[env].env.jsin theconfigPath. (default:config.js)
Thank You!
1.1.1
3 years ago