From 476e57241413411b2f0817dcd54d17e869495c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Wed, 10 Jan 2024 08:11:41 +0100 Subject: [PATCH] fix: only watch needed env files (#15365) --- packages/vite/src/node/env.ts | 9 ++++----- packages/vite/src/node/server/hmr.ts | 2 +- packages/vite/src/node/server/index.ts | 7 ++++++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts index 6c29532f02a6f6..481a50db63f105 100644 --- a/packages/vite/src/node/env.ts +++ b/packages/vite/src/node/env.ts @@ -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( @@ -27,11 +27,10 @@ export function loadEnv( } prefixes = arraify(prefixes) const env: Record = {} - 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))) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index ec95dcdd5d6e0b..d5aaffc939ff60 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -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)}`) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index b73ecfa07a2517..2b065479f0b140 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -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' @@ -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)