Skip to content

Reading Environment Variables from multiple .env Files #1056

Description

@Eptagone

Clear and concise description of the problem

This was already requested in #652, however the implementation was incomplete because it only support a single env file. There's no way to add multiple .env files.

I'm trying to make tsdown to read an .env file and also an optional .env.local file.

Suggested solution

Support multiple env-files

Alternative

I'm reading the files manually and injecting the values into the env config but it doesn't handle file changes when using watch mode.

import fs from "fs";
import path from "path";
import { defineConfig } from "tsdown/config";
import { parseEnv } from "util";

const DEV_ENV_FILES = [".env", ".env.local"]
    .filter(fs.existsSync)
    .map((filePath) => path.resolve(process.cwd(), filePath));
let env: Record<string, unknown> = {};
for (const filePath of DEV_ENV_FILES) {
    if (fs.existsSync(filePath)) {
        env = Object.assign(env, parseEnv(fs.readFileSync(filePath, "utf-8")));
    }
}

export default defineConfig({
    env,
});

Additional context

No response

Validations

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Fields

    Priority

    None yet

    Start date

    None yet

    Target date

    None yet

    Effort

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions