parcel: Environment variables are not updated

๐Ÿ› bug report

Iโ€™m using dotenv to load environment variables from a .env file ; thatโ€™s convenient to change their value quickly. Though, right now I always need to restart parcel in order to have them changed inside the bundle.

๐ŸŽ› Configuration (.babelrc, package.json, cli command)

parcel watch server/index.js -d dist/server --target=node

๐Ÿค” Expected Behavior

The bundle should contain updated environment variables.

๐Ÿ˜ฏ Current Behavior

Only environment variables from when parcel was launched are alive.

๐Ÿ’ Possible Solution

Maybe donโ€™t cache env vars ?

๐ŸŒ Your Environment

Software Version(s)
Parcel latest of 1.8.x
Node 8 LTS
npm/Yarn latest npm
Operating System Linux ce3280140caf 4.9.87-linuxkit-aufs #1 SMP Wed Mar 14 15:12:16 UTC 2018 x86_64 GNU/Linux

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 27 (10 by maintainers)

Most upvoted comments

We should probably add the env variables to the cache key

Hum. Not for me.

index.html

<script src="./index.js"></script>

index.js

console.log(process.env.FOO)

cmd

> rm -rf .parcel-cache dist
> FOO=bar parcel build src/index.html --cache-dir .parcel-cache
> http-server dist

โœจ  Built in 23.04s.

dist/src.050c5e28.map        โš ๏ธ  5.67 MB     338ms
dist/src.050c5e28.js          โš ๏ธ  1.9 MB    22.16s
dist/src.6a665fea.css          23.09 KB     6.17s
dist/favicon.8d872578.ico      16.56 KB     307ms
dist/index.html                   535 B     460ms

result

// bar

Now change it

cmd

^Chttp-server stopped.
> FOO=something parcel build src/index.html --cache-dir .parcel-cache
> http-server dist

โœจ  Built in 1.91s.

dist/src.050c5e28.map        โš ๏ธ  5.67 MB    376ms
dist/src.050c5e28.js          โš ๏ธ  1.9 MB    1.36s
dist/src.6a665fea.css          23.09 KB     36ms
dist/favicon.8d872578.ico      16.56 KB     16ms
dist/index.html                   535 B      9ms

Wrong result

// bar

Now try with a .env file

.env

FOO=something_else

cmd

^Chttp-server stopped.
> parcel build src/index.html --cache-dir .parcel-cache
> http-server dist

โœจ  Built in 2.11s.

dist/src.050c5e28.map        โš ๏ธ  5.67 MB    402ms
dist/src.050c5e28.js          โš ๏ธ  1.9 MB    1.37s
dist/src.6a665fea.css          23.09 KB     23ms
dist/favicon.8d872578.ico      16.56 KB     21ms
dist/index.html                   535 B    278ms

Wrong result

// bar

NB1: mind the fact that the digest in the asset slug name did not change after the rebuild NB2: dotenv or inline environment variables changes nothing

In case its help to anyone having similar issues, we had to update our babel.config and specifically tell transform-inline-environment-variables to exclude which env vars we didnโ€™t want to be inlined (and therefor cached).

  plugins: [
    ['transform-inline-environment-variables', {
      'exclude': ['VARIABLE_ONE', 'VARIABLE_TWO'],
    }],
  ]

I may take an hour to contribute on this, @DeMoorJasper @mischnic can you point me to the right direction to โ€œadd that environment change to the cache key ?โ€

EDIT: I found this old issue #66, resolved with this PR #521, merged in this commit #6c3d34f, and integrated inside 1.5.0

This seems like a regression then ?