feat: replace dotenv with native node:util parseEnv (#2181)
Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
@@ -30,7 +30,6 @@
|
||||
"ci-info": "^4.4.0",
|
||||
"consola": "^3.4.2",
|
||||
"defu": "^6.1.4",
|
||||
"dotenv": "^17.3.1",
|
||||
"dotenv-expand": "^12.0.3",
|
||||
"esbuild": "^0.27.1",
|
||||
"filesize": "^11.0.13",
|
||||
|
||||
@@ -1,23 +1,45 @@
|
||||
import { config } from 'dotenv';
|
||||
import { readFileSync, existsSync } from 'node:fs';
|
||||
import { parseEnv } from 'node:util';
|
||||
import { expand } from 'dotenv-expand';
|
||||
import type { TargetBrowser } from '../../types';
|
||||
|
||||
/** Load environment files based on the current mode and browser. */
|
||||
export function loadEnv(mode: string, browser: TargetBrowser) {
|
||||
return expand(
|
||||
config({
|
||||
quiet: true,
|
||||
// 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`,
|
||||
],
|
||||
const envFiles = [
|
||||
// List is ordered with general files first, specific ones last, so the more
|
||||
// specific files override the more general ones in the loop below.
|
||||
`.env`,
|
||||
`.env.local`,
|
||||
`.env.${mode}`,
|
||||
`.env.${mode}.local`,
|
||||
`.env.${browser}`,
|
||||
`.env.${browser}.local`,
|
||||
`.env.${mode}.${browser}`,
|
||||
`.env.${mode}.${browser}.local`,
|
||||
];
|
||||
|
||||
const parsed = Object.fromEntries<string>(
|
||||
envFiles.flatMap((filePath) => {
|
||||
if (!existsSync(filePath)) return [];
|
||||
|
||||
try {
|
||||
const content = readFileSync(filePath, 'utf-8');
|
||||
const parsedEnv = parseEnv(content);
|
||||
return Object.entries(parsedEnv) as [string, string][];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
// Make a copy of `process.env` so that `dotenv-expand` doesn't re-assign the
|
||||
// expanded values to the global `process.env`.
|
||||
const processEnv = { ...process.env } as Record<string, string>;
|
||||
|
||||
expand({
|
||||
parsed,
|
||||
processEnv,
|
||||
});
|
||||
|
||||
return parsed;
|
||||
}
|
||||
|
||||
Generated
-3
@@ -456,9 +456,6 @@ importers:
|
||||
defu:
|
||||
specifier: ^6.1.4
|
||||
version: 6.1.4
|
||||
dotenv:
|
||||
specifier: ^17.3.1
|
||||
version: 17.3.1
|
||||
dotenv-expand:
|
||||
specifier: ^12.0.3
|
||||
version: 12.0.3
|
||||
|
||||
Reference in New Issue
Block a user