Skip to content

Commit

Permalink
fix: only watch needed env files (#15365)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre authored Jan 10, 2024
1 parent 2836276 commit 476e572
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
9 changes: 4 additions & 5 deletions packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { expand } from 'dotenv-expand'
import { arraify, tryStatSync } from './utils'
import type { UserConfig } from './config'

export function getEnvFilesForMode(mode: string): string[] {
export function getEnvFilesForMode(mode: string, envDir: string): string[] {
return [
/** default file */ `.env`,
/** local file */ `.env.local`,
/** mode file */ `.env.${mode}`,
/** mode local file */ `.env.${mode}.local`,
]
].map((file) => path.join(envDir, file))
}

export function loadEnv(
Expand All @@ -27,11 +27,10 @@ export function loadEnv(
}
prefixes = arraify(prefixes)
const env: Record<string, string> = {}
const envFiles = getEnvFilesForMode(mode)
const envFiles = getEnvFilesForMode(mode, envDir)

const parsed = Object.fromEntries(
envFiles.flatMap((file) => {
const filePath = path.join(envDir, file)
envFiles.flatMap((filePath) => {
if (!tryStatSync(filePath)?.isFile()) return []

return Object.entries(parse(fs.readFileSync(filePath)))
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export async function handleHMRUpdate(

const isEnv =
config.inlineConfig.envFile !== false &&
getEnvFilesForMode(config.mode).includes(fileName)
getEnvFilesForMode(config.mode, config.envDir).includes(fileName)
if (isConfig || isConfigDependency || isPackageJson || isEnv) {
// auto restart server
debugHmr?.(`[config change] ${colors.dim(shortFile)}`)
Expand Down
7 changes: 6 additions & 1 deletion packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type { Logger } from '../logger'
import { printServerUrls } from '../logger'
import { createNoopWatcher, resolveChokidarOptions } from '../watch'
import { initPublicFiles } from '../publicDir'
import { getEnvFilesForMode } from '../env'
import type { PluginContainer } from './pluginContainer'
import { ERR_CLOSED_SERVER, createPluginContainer } from './pluginContainer'
import type { WebSocketServer } from './ws'
Expand Down Expand Up @@ -414,7 +415,11 @@ export async function _createServer(
const watcher = watchEnabled
? (chokidar.watch(
// config file dependencies and env file might be outside of root
[...new Set([root, ...config.configFileDependencies, config.envDir])],
[
root,
...config.configFileDependencies,
...getEnvFilesForMode(config.mode, config.envDir),
],
resolvedWatchOptions,
) as FSWatcher)
: createNoopWatcher(resolvedWatchOptions)
Expand Down

0 comments on commit 476e572

Please sign in to comment.