Skip to content

🚀 Feature Request: Support env variables (process.env) or --define in HTML bundling via bun index.html #25486

@mizulu

Description

@mizulu

What is the problem this feature would solve?

When bundling a project that uses an index.html pointing to a module like main.tsx, there is currently no way to inject env variables directly into the client bundle when running bun index.html. unlike bun build for JS/TS, bun index.html does not accept the --define flag, so I cannot even manually replace values like process.env.PORT. This makes it impossible to inject run-time env configuration into the HTML bundle and client-side code.

Bun supports .env auto-loading for server environments. The expectation is that the requested feature should also leverage Bun’s .env auto-loading mechanism, so variables from a .env file would be automatically available for injection into the client bundle or via --define during HTML bundling. (limited to public env varaibles)

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

I propose to add support for either:

  • Direct injection of env variables (such as process.env.*) into the client HTML bundle
    bun has multiple configuration to decide which env can be bundled and how
    https://bun.com/docs/bundler#env
  1. --define flag in bun index.html, so users could manually map values (e.g., bun --define process.env.PORT="'3000'" index.html). should and could also have worked here.

we need way to

  1. be able to use env variables in client bundle run via bun HTML bundler
  2. be able to define how the env are embedded following the env bundling rules.

What alternatives have you considered?

Manually handling env replacement before building, but this is error-prone and does not leverage Bun’s intended features.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions