Skip to content

Commit

Permalink
feat(env): Load env from .env.[browser] variants (#1078)
Browse files Browse the repository at this point in the history
Co-authored-by: Aaron <[email protected]>
  • Loading branch information
sleekslush and aklinker1 authored Oct 22, 2024
1 parent d06f812 commit c031c6e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions docs/guide/essentials/config/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ WXT supports [dotenv files the same way as Vite](https://vite.dev/guide/env-and-
.env.local
.env.[mode]
.env.[mode].local
.env.[browser]
.env.[browser].local
.env.[mode].[browser]
.env.[mode].[browser].local
```

And any environment variables listed inside them will be available at runtime:
Expand Down
2 changes: 1 addition & 1 deletion packages/wxt/src/core/resolve-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function resolveConfig(
const mode = mergedConfig.mode ?? COMMAND_MODES[command];
const env: ConfigEnv = { browser, command, manifestVersion, mode };

loadEnv(mode); // Load any environment variables used below
loadEnv(mode, browser); // Load any environment variables used below

const root = path.resolve(
inlineConfig.root ?? userConfig.root ?? process.cwd(),
Expand Down
17 changes: 14 additions & 3 deletions packages/wxt/src/core/utils/env.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
import { config } from 'dotenv';
import type { TargetBrowser } from '../../types';

/**
* Load environment files based on the current mode.
* Load environment files based on the current mode and browser.
*/
export function loadEnv(mode: string) {
export function loadEnv(mode: string, browser: TargetBrowser) {
return config({
path: [`.env.${mode}.local`, `.env.${mode}`, `.env.local`, `.env`],
// Files on top override files below
path: [
`.env.${mode}.${browser}.local`,
`.env.${mode}.${browser}`,
`.env.${browser}.local`,
`.env.${browser}`,
`.env.${mode}.local`,
`.env.${mode}`,
`.env.local`,
`.env`,
],
});
}

0 comments on commit c031c6e

Please sign in to comment.