Skip to content

Add Support for Reading Environment Variables from .env Files #652

@toto6038

Description

@toto6038

Clear and concise description of the problem

Currently, tsdown allows defining compile-time environment variables through two methods: a configuration file or the --env.* <value> command-line option. However, it does not support reading environment variables from .env files yet, which makes it challenging to dynamically manage these constants. Adding this feature would enhance flexibility and streamline the workflow.

I'd be happy to help implement this feature :)

Suggested solution

  1. Add Command-Line Options:
    Introduce the following new command-line options:

    • --env-file <file>: Specifies the path to the .env file to load environment variables from.
    • --env-prefix <prefix>: Defines a prefix for environment variables (default: TSDOWN).

    Additionally, allow the envPrefix option to be set in the configuration file.

  2. Integrate dotenv for Environment Variable Resolution:
    During the resolution of UserConfig, use dotenv to load environment variables from the specified .env file. Apply the prefix filter (if provided) for security to ensure only relevant variables are included.

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions