Skip to content

Commit bca89e1

Browse files
OnlyWicksapphi-red
andauthored
feat(env): add false option for envDir to disable env loading (#19503)
Co-authored-by: 翠 / green <[email protected]>
1 parent 31e93ed commit bca89e1

File tree

7 files changed

+32
-22
lines changed

7 files changed

+32
-22
lines changed

docs/config/shared-options.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,10 +459,10 @@ Set to `false` to prevent Vite from clearing the terminal screen when logging ce
459459

460460
## envDir
461461

462-
- **Type:** `string`
462+
- **Type:** `string | false`
463463
- **Default:** `root`
464464

465-
The directory from which `.env` files are loaded. Can be an absolute path, or a path relative to the project root.
465+
The directory from which `.env` files are loaded. Can be an absolute path, or a path relative to the project root. `false` will disable the `.env` file loading.
466466

467467
See [here](/guide/env-and-mode#env-files) for more about environment files.
468468

docs/guide/api-javascript.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ parentServer.use(vite.middlewares)
7777
The `InlineConfig` interface extends `UserConfig` with additional properties:
7878

7979
- `configFile`: specify config file to use. If not set, Vite will try to automatically resolve one from project root. Set to `false` to disable auto resolving.
80-
- `envFile`: Set to `false` to disable `.env` files.
8180

8281
## `ResolvedConfig`
8382

packages/vite/src/node/config.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ export interface UserConfig extends DefaultEnvironmentOptions {
408408
* root.
409409
* @default root
410410
*/
411-
envDir?: string
411+
envDir?: string | false
412412
/**
413413
* Env variables starts with `envPrefix` will be exposed to your client source code via import.meta.env.
414414
* @default 'VITE_'
@@ -547,6 +547,7 @@ export interface InlineConfig extends UserConfig {
547547
configFile?: string | false
548548
/** @experimental */
549549
configLoader?: 'bundle' | 'runner' | 'native'
550+
/** @deprecated */
550551
envFile?: false
551552
forceOptimizeDeps?: boolean
552553
}
@@ -587,7 +588,7 @@ export interface ResolvedConfig
587588
/** @internal list of bundle entry id. used to detect recursive worker bundle. */
588589
bundleChain: string[]
589590
isProduction: boolean
590-
envDir: string
591+
envDir: string | false
591592
env: Record<string, any>
592593
resolve: Required<ResolveOptions> & {
593594
alias: Alias[]
@@ -1288,12 +1289,15 @@ export async function resolveConfig(
12881289
)
12891290

12901291
// load .env files
1291-
const envDir = config.envDir
1292-
? normalizePath(path.resolve(resolvedRoot, config.envDir))
1293-
: resolvedRoot
1294-
const userEnv =
1295-
inlineConfig.envFile !== false &&
1296-
loadEnv(mode, envDir, resolveEnvPrefix(config))
1292+
// Backward compatibility: set envDir to false when envFile is false
1293+
let envDir = config.envFile === false ? false : config.envDir
1294+
if (envDir !== false) {
1295+
envDir = config.envDir
1296+
? normalizePath(path.resolve(resolvedRoot, config.envDir))
1297+
: resolvedRoot
1298+
}
1299+
1300+
const userEnv = loadEnv(mode, envDir, resolveEnvPrefix(config))
12971301

12981302
// Note it is possible for user to have a custom mode, e.g. `staging` where
12991303
// development-like behavior is expected. This is indicated by NODE_ENV=development

packages/vite/src/node/env.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,25 @@ import type { UserConfig } from './config'
77

88
const debug = createDebugger('vite:env')
99

10-
export function getEnvFilesForMode(mode: string, envDir: string): string[] {
11-
return [
12-
/** default file */ `.env`,
13-
/** local file */ `.env.local`,
14-
/** mode file */ `.env.${mode}`,
15-
/** mode local file */ `.env.${mode}.local`,
16-
].map((file) => normalizePath(path.join(envDir, file)))
10+
export function getEnvFilesForMode(
11+
mode: string,
12+
envDir: string | false,
13+
): string[] {
14+
if (envDir !== false) {
15+
return [
16+
/** default file */ `.env`,
17+
/** local file */ `.env.local`,
18+
/** mode file */ `.env.${mode}`,
19+
/** mode local file */ `.env.${mode}.local`,
20+
].map((file) => normalizePath(path.join(envDir, file)))
21+
}
22+
23+
return []
1724
}
1825

1926
export function loadEnv(
2027
mode: string,
21-
envDir: string,
28+
envDir: string | false,
2229
prefixes: string | string[] = 'VITE_',
2330
): Record<string, string> {
2431
const start = performance.now()

packages/vite/src/node/server/hmr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ export async function handleHMRUpdate(
389389
)
390390

391391
const isEnv =
392-
config.inlineConfig.envFile !== false &&
392+
config.envDir !== false &&
393393
getEnvFilesForMode(config.mode, config.envDir).includes(file)
394394
if (isConfig || isConfigDependency || isEnv) {
395395
// auto restart server

packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ const c = () => {
14541454
test('combine mappings', async () => {
14551455
const server = await createServer({
14561456
configFile: false,
1457-
envFile: false,
1457+
envDir: false,
14581458
logLevel: 'error',
14591459
plugins: [
14601460
{

packages/vite/src/node/ssr/runnerImport.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export async function runnerImport<T>(
2121
const config = await resolveConfig(
2222
mergeConfig(inlineConfig || {}, {
2323
configFile: false,
24-
envFile: false,
24+
envDir: false,
2525
cacheDir: process.cwd(),
2626
environments: {
2727
inline: {

0 commit comments

Comments
 (0)