Open terrablue opened 3 months ago
I'm also looking to support .env files in my binaries.
This is what I expected to happen when running:
bun build --compile --env-file=.env
I found a temporary work around:
Create a function in a separate file that reads the environment variables, either using Bun.env
or process.env
. Then, import the function in your main file using
import { getApiKey } from "./get-env" with { type: "macro" }
const apiKey = getApiKey()
This will include the environment variables at bundle time, which will be included in the executable.
@joglr awesome work there, and thank you! I think I can use this in my framework to generate different code depending on build target.
What is the problem this feature would solve?
When compiling apps using environment variables, if the env vars are not built-in into the program during compilation, and are instead loaded during runtime, the program would fail to run outside of the compilation directory.
What is the feature you are proposing to solve the problem?
The semantics of using
Bun.env
together withbun build --compile
would be to use values read in during compilation process, similar to howDeno.env.get
modifies its behaviour during compilation.Deno has implemented this distinction in v1.45: https://deno.com/blog/v1.45#deno-compile-supports---env-flag
What alternatives have you considered?
Relying on Deno to compile apps, or trying to circumvent the issue by not using environment variables, and feeding in buildtime values using another mechanism to the app (say, a JSON import).