Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cjs): build cjs for
loadEnv
(#8305)
- Loading branch information
Showing
5 changed files
with
88 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import fs from 'fs' | ||
import dotenv from 'dotenv' | ||
import dotenvExpand from 'dotenv-expand' | ||
import { arraify, lookupFile } from './utils' | ||
import type { UserConfig } from './config' | ||
|
||
export function loadEnv( | ||
mode: string, | ||
envDir: string, | ||
prefixes: string | string[] = 'VITE_' | ||
): Record<string, string> { | ||
if (mode === 'local') { | ||
throw new Error( | ||
`"local" cannot be used as a mode name because it conflicts with ` + | ||
`the .local postfix for .env files.` | ||
) | ||
} | ||
prefixes = arraify(prefixes) | ||
const env: Record<string, string> = {} | ||
const envFiles = [ | ||
/** mode local file */ `.env.${mode}.local`, | ||
/** mode file */ `.env.${mode}`, | ||
/** local file */ `.env.local`, | ||
/** default file */ `.env` | ||
] | ||
|
||
// check if there are actual env variables starting with VITE_* | ||
// these are typically provided inline and should be prioritized | ||
for (const key in process.env) { | ||
if ( | ||
prefixes.some((prefix) => key.startsWith(prefix)) && | ||
env[key] === undefined | ||
) { | ||
env[key] = process.env[key] as string | ||
} | ||
} | ||
|
||
for (const file of envFiles) { | ||
const path = lookupFile(envDir, [file], { pathOnly: true, rootDir: envDir }) | ||
if (path) { | ||
const parsed = dotenv.parse(fs.readFileSync(path), { | ||
debug: process.env.DEBUG?.includes('vite:dotenv') || undefined | ||
}) | ||
|
||
// let environment variables use each other | ||
dotenvExpand({ | ||
parsed, | ||
// prevent process.env mutation | ||
ignoreProcessEnv: true | ||
} as any) | ||
|
||
// only keys that start with prefix are exposed to client | ||
for (const [key, value] of Object.entries(parsed)) { | ||
if ( | ||
prefixes.some((prefix) => key.startsWith(prefix)) && | ||
env[key] === undefined | ||
) { | ||
env[key] = value | ||
} else if ( | ||
key === 'NODE_ENV' && | ||
process.env.VITE_USER_NODE_ENV === undefined | ||
) { | ||
// NODE_ENV override in .env file | ||
process.env.VITE_USER_NODE_ENV = value | ||
} | ||
} | ||
} | ||
} | ||
return env | ||
} | ||
|
||
export function resolveEnvPrefix({ | ||
envPrefix = 'VITE_' | ||
}: UserConfig): string[] { | ||
envPrefix = arraify(envPrefix) | ||
if (envPrefix.some((prefix) => prefix === '')) { | ||
throw new Error( | ||
`envPrefix option contains value '', which could lead unexpected exposure of sensitive information.` | ||
) | ||
} | ||
return envPrefix | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters