feat(env): Load env from .env.[browser] variants (#1078)

Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
Craig Slusher
2024-10-22 09:36:24 -04:00
committed by GitHub
parent d06f8128f5
commit c031c6e82e
3 changed files with 19 additions and 4 deletions
@@ -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:
+1 -1
View File
@@ -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(),
+14 -3
View File
@@ -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`,
],
});
}