Skip to content

Commit 81b80c6

Browse files
authored
feat: add inlineConfig.envFile option (#2475)
1 parent c61b3e4 commit 81b80c6

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

docs/guide/api-javascript.md

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const { createServer } = require('vite')
3333
The `InlineConfig` interface extends `UserConfig` with additional properties:
3434

3535
- `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.
36+
- `envFile`: Set to `false` to disable `.env` files.
3637

3738
## `ViteDevServer`
3839

packages/vite/src/node/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,13 @@ export interface SSROptions {
160160

161161
export interface InlineConfig extends UserConfig {
162162
configFile?: string | false
163+
envFile?: false
163164
}
164165

165166
export type ResolvedConfig = Readonly<
166167
Omit<UserConfig, 'plugins' | 'alias' | 'dedupe' | 'assetsInclude'> & {
167168
configFile: string | undefined
168-
inlineConfig: UserConfig
169+
inlineConfig: InlineConfig
169170
root: string
170171
base: string
171172
publicDir: string
@@ -273,7 +274,7 @@ export async function resolveConfig(
273274
}
274275

275276
// load .env files
276-
const userEnv = loadEnv(mode, resolvedRoot)
277+
const userEnv = inlineConfig.envFile !== false && loadEnv(mode, resolvedRoot)
277278

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

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

+6-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,15 @@ export async function handleHMRUpdate(
4444
const { ws, config, moduleGraph } = server
4545
const shortFile = getShortName(file, config.root)
4646

47-
if (file === config.configFile || file.endsWith('.env')) {
47+
const isConfig = file === config.configFile
48+
const isEnv = config.inlineConfig.envFile !== false && file.endsWith('.env')
49+
if (isConfig || isEnv) {
4850
// auto restart server
4951
debugHmr(`[config change] ${chalk.dim(shortFile)}`)
5052
config.logger.info(
51-
chalk.green('config or .env file changed, restarting server...'),
53+
chalk.green(
54+
`${isConfig ? 'config' : '.env'} file changed, restarting server...`
55+
),
5256
{ clear: true, timestamp: true }
5357
)
5458
await restartServer(server)

0 commit comments

Comments
 (0)