oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.21k stars 2.77k forks source link

Bundler environment variable inlining should be configurable #9902

Open mangs opened 7 months ago

mangs commented 7 months ago

What is the problem this feature would solve?

In testing our Bun-based lambda while prepping its rollout to production, I realized that despite the documentation saying that environment variable inlining occurs when building, the bundler actually doesn't. This resulted in my PR to remove from the documentation any mention of environment variable inlining until the feature is added back somehow. Being able to choose whether or not this is done would be really helpful and would be a nice way to accomplish the feature without forcing anyone into behavior they don't want.

For frontend apps, having environment variables inlined is helpful. However, in the case of server-side apps on Node.js or Bun, being able to read environment variables at runtime is helpful. This is especially so on serverless platforms with shared execution environments where secret values are best accessed via environment variables.

What is the feature you are proposing to solve the problem?

A configuration flag for the bundler to enable/disable environment variable inlining. For example:

bun build --inline-env-vars[=y,n]

Perhaps target: 'browser' could inline by default and target: 'bun' and target: 'node' could disable inlining, although inconsistent/unexpected behavior between targets will probably confuse more than help; just mentioning this for completeness in case I'm not thinking of a better option.

What alternatives have you considered?

Manual value replacement with a text replacement engine like sed

JonnyBurger commented 6 months ago

I would like it to be configurable

Trying to use Bun as a Rollup replacement for creating ESM versions of browser modules They can be used in development and production, and will be bundled again by Webpack, which also does inlining of process.env.NODE_ENV

So I would like it to preserve process.env.NODE_ENV so that Webpack can later decide what to do with it

mnpenner commented 2 months ago

It appears import.meta.env is not inlined but process.env is.

This is actually convenient for me because then I can use if(process.env.DEV) for stuff I want compiled out of the final input (when combined with --minify only it seems), and I can use import.meta.env.DB_PASSWORD for my passwords.

Is this documented? Can it be relied upon?

Edit: There's also Bun.env. This one is also not inlined.

laytong commented 1 week ago

I believe i've stumbled into the same issue. values from the build environment appear to be inlined into the compiled binary/bundle i generate for any node_modules that may rely on process.env vars leading to unexpected behaviour.

a way to opt out of this would be great since it doesn't appear to do this for my own code, only my dependencies?