Have a single build with possibility of switching between environment variables without actually rebuilding the entire app. Simple, cross-platform and configurable approach of using environment variables for a web application.
Environment variables are inside the .env-cmdrc.json
file. Currently there are 3 environments (dev
, staging
, demo
).
- Define a
.env.public
file, with variables you want set on our environment - Add the variables from
.env.public
to.env-cmdrc.json
under each environment - Build the application
npm run build
- Preview the build on different environments just with different running commands (e.g.:
npm run start:staging
,npm run start:demo
)
Optionally you can define environment variables inside a .env
(e.g.: .env.test
) file (instead of .env-cmdrc.json
) and then run the cli to read and apply the variables from that file (import-meta-env -x .env.public -e .env.test
)
- Using
@import-meta-env/unplugin
that on build time (npm run build
) will substitute allimport.meta.env
from our source code to a predefined placeholder - Then running
env-cmd -e demo
(wheredemo
is a environment from.env-cmdrc.json
) will set up the environment variables for us - Then running
import-meta-env --x .env.public
will substitute the predefined placeholder frombuild
with the actual environment variables
Using env-cmd -e mode
is completely optional, if the environment from where the build is being served has already exposed / defined the required environment variables (from .env.public
) then the build will be served with this variables.