-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feature] Use environment variables inside the HTML #1209
Comments
+1 We would like to use different favicons to signify our dev environment versus our production environment so that when we have many tabs open, it's easy to pick out where you're looking. Having access to an env variable in the HTML would allow us to do this easily. |
Would also be useful for additional script tags (service worker, Google analytics, etc) that you don't need when developing/designing your app; but obviously need when deployed. I think this could be resolved by allowing the entry point to be a template file like Handlebars, EJS, or Pug. Parcel automatically sets the Those of us coming from Webpack got used to UPDATE: This is actually already supported. I created a simple example using Pug. You do have to
doctype html
html(lang='en')
head
meta(charset='utf-8')
meta(name='viewport', content='width=device-width, initial-scale=1, shrink-to-fit=no')
title Parcel
body
if process.env.NODE_ENV === 'production'
h1 PRODUCTION
else
h1 DEVELOPMENT Running
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Parcel</title>
</head>
<body>
<h1>PRODUCTION</h1>
</body>
</html> Therefor, to resolve @GaelS 's issue, you could do something like this: body
a(href=process.env.LINK_HREF) Ok LINK_HREF='http://example.com' parcel build index.pug <body>
<a href="http://example.com">Ok</a>
<body> Pretty cool and still zero config 👍 |
You can use The example bellow uses
|
If you’re using something that runs JS (like react) as mentioned you can just reference
And voila, you have environment variables reflected in your HTML. Also, one gotcha: I’m not seeing changes to .env triggering a re-build. You’ll have to edit something to trigger a re-build or stop parcel, @breier in your example, are you using some extra asset-type handler to handle that ruby syntax |
@mrcoles I've used |
This isn't something we'll support out of the box since it isn't standard HTML syntax. But you could easily use a PostHTML plugin for this, or another template language like Pug. |
@dandv, sorry that was a typo, I updated my comment so all |
Sorry for the really late reply, but for the record, here it is...
Basically, any custom env var just have to start with VUE_APP_ in order to be auto loaded and assimilated. I hope it helps someone ;) |
@mrcoles, were you able to use posthtml.config.jsconsole.log(process.env.NODE_ENV)
module.expors = {}; $ npx parcel --version
2.0.0-nightly.462
$ NODE_ENV=test npx parcel build --no-cache index.html
console: production
⠸ Building index.html...
|
For Parcel v2, you will have to call const dotenv = require("dotenv");
dotenv.config();
module.exports = {
plugins: {
"posthtml-expressions": {
locals: {
HOME_URL: process.env.HOME_URL,
... |
Unfortunately, it seems to read only
Whereas Parcel explicitly loads parcel/packages/core/core/src/loadDotEnv.js Lines 18 to 26 in 4df85e3
All this sounds like a show-stopper as of now. Switching to PUG is a bit… overkill to just inject 2-3 environment variables into HTML. I'd better consider writing a simple parcel transformer for this… or a PostHTML plugin. |
I’m trying out Parcel2 and using my prior posthtml-expressions example and also building off the two prior comments from @talor-hammond and @anantakrishna where it’s identified that we need to load the environment variables ourselves, I was able to get things working by doing the following: const dotenv = require('dotenv');
const NODE_ENV = process.env.NODE_ENV;
const dotenvFiles = [
'.env',
// Don't include `.env.local` for `test` environment
// since normally you expect tests to produce the same
// results for everyone
NODE_ENV === 'test' ? null : '.env.local',
`.env.${NODE_ENV}`,
`.env.${NODE_ENV}.local`,
].filter(Boolean);
const env = {};
for (let dotenvFile of dotenvFiles) {
const config = dotenv.config({ path: dotenvFile });
if (config.parsed) {
Object.assign(env, config.parsed);
}
}
module.exports = {
plugins: {
'posthtml-expressions': {
locals: {
HOME_URL: env.HOME_URL,
},
},
},
}; Given this extra, brittle-feeling code and also the big disclaimers that keep popping up about how a |
Hello, accessing |
Seconded ^ |
Putting (I have API keys for things like Google Maps, and I want to use different keys for development and production. That means simple text substitution won't work; I need Here's what I'm currently doing... First, I installed npm install -D @plasmohq/parcel-transformer-inject-env Then I added it to {
"extends": "@parcel/config-default",
"transformers": {
"*.html": [
"@plasmohq/parcel-transformer-inject-env",
"..."
]
}
} Then I created GOOGLE_MAPS_API_KEY=... Then I referenced those keys using
The way the transformer works may not quite suit your needs — in particular, there's no escape mechanism — but the implementation is actually quite simple; see here and here. If you need to, you can probably create your own version pretty easily. The advantage of this approach over |
Just to put the experience of someone who tried the most liked strategies of implementation in this topic:
Best solution to me was the one that came from @michaellenaghan. It is indeed a pain to push an environment variable through dotenv to index.html using parcel, tons of trial and error. I applied @michaellenaghan solution here: https://github.com/henriquenovais/Typescript101. |
Thank you @michaellenaghan we used what you proposed internally and works perfectly! |
🙋 feature request!
First thing first: Thanks for the nice work ! 👍
I have a use case where an anchor tag redirects to an external URL based on the environment (
localhost:3333
if in DEV for instance andhttps://whatever.com
in PROD).Therefore, it would be really convenient to be able to use the environment variables directly inside the
index.html
without going through any javascript code.Just like this :
<a href=%MY_ENV_VARIABLE%>Ok</a>
The
%
are used randomly just to show some kind of formatting to handle ENV variables at build time.Maybe I have missed something but I was not able to find any kind of documentation or issues (open or close) dealing with this kind of things. Maybe I'm totally missing something but with this type of use cases, this feature would be very convenient.
The text was updated successfully, but these errors were encountered: