From 37bce389a0cfed5901d5601df971764b4e6c776e Mon Sep 17 00:00:00 2001 From: robobun Date: Thu, 27 Nov 2025 22:51:11 -0800 Subject: [PATCH] docs: document inlining process.env.* values in static HTML bundling (#25084) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary - Add documentation for the `env` option that inlines `process.env.*` values in frontend code when bundling HTML files - Document runtime configuration via `bunfig.toml` `[serve.static]` section for `bun ./index.html` - Document production build configuration via CLI (`--env=PUBLIC_*`) and `Bun.build` API (`env: "PUBLIC_*"`) - Explain prefix filtering to avoid exposing sensitive environment variables ## Test plan - [x] Verify documentation renders correctly in local preview - [x] Cross-reference with existing `env` documentation in bundler/index.mdx 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Michael H Co-authored-by: Claude Bot Co-authored-by: Claude Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- docs/bundler/fullstack.mdx | 22 +++++++++ docs/bundler/html-static.mdx | 87 ++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) diff --git a/docs/bundler/fullstack.mdx b/docs/bundler/fullstack.mdx index 6b108d9866..c28241d8ba 100644 --- a/docs/bundler/fullstack.mdx +++ b/docs/bundler/fullstack.mdx @@ -492,6 +492,28 @@ Bun will lazily resolve and load each plugin and use them to bundle your routes. the CLI. +## Inline Environment Variables + +Bun can replace `process.env.*` references in your frontend JavaScript and TypeScript with their actual values at build time. Configure the `env` option in your `bunfig.toml`: + +```toml title="bunfig.toml" icon="settings" +[serve.static] +env = "PUBLIC_*" # only inline env vars starting with PUBLIC_ (recommended) +# env = "inline" # inline all environment variables +# env = "disable" # disable env var replacement (default) +``` + + + This only works with literal `process.env.FOO` references, not `import.meta.env` or indirect access like `const env = + process.env; env.FOO`. + +If an environment variable is not set, you may see runtime errors like `ReferenceError: process + is not defined` in the browser. + + + +See the [HTML & static sites documentation](/bundler/html-static#inline-environment-variables) for more details on build-time configuration and examples. + ## How It Works Bun uses `HTMLRewriter` to scan for `