Define process.env variables within the bundle (similar to Webpack's EnvironmentPlugin, which relies on DefinePlugin).
Add a plugin that reads environment variables and injects them into the bundle such that code can rely on process.env.VAR
in browser.
There doesn't seem to be a simple way to define environment variables, this would add a simple way to add environment variables.
This plugin could use dotenv so that in order to define environment variables you can define a .env
or .env.development
or .env.production
in the root of the project. This would make it pretty straight forward to use environment variables; however, it does raise the question of how to pass environment variables into a project without relying on dotenv (host environment variables).
An internal library relies on a variable being set in process.env - with Webpack, we use the EnvironmentPlugin to add it in.
| Software | Version(s)
| ---------------- | ----------
| Parcel | 1.2.0
| Node | ^8
| npm/Yarn | Yarn 1.3.2
| Operating System | macOS
You can just set environment variables in your shell when building. e.g.
SOME_VAR=test parcel index.html
and process.env.SOME_VAR
will automatically be replaced with the value.
It will be nice if we could have a similar way to set custom environment variables like in Create react app https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#adding-custom-environment-variables
May be just babel-plugin-transform-inline-environment-variables to not bload the code base?
@devongovett that works, but as the original post commented, it'd really be good to have production/development env vars picked up automatically based on whether it's a build
or not. I use this feature heavily in CRA and find it invaluable.
Having said that, I think this project looks _incredibly_ promising, so keep up the good work!
moving to #258. See additional discussion there.
Most helpful comment
You can just set environment variables in your shell when building. e.g.
and
process.env.SOME_VAR
will automatically be replaced with the value.