Skip to content

Commit

Permalink
feat: add inlineConfig.envFile option (#2475)
Browse files Browse the repository at this point in the history
  • Loading branch information
egoist committed Apr 14, 2021
1 parent c61b3e4 commit 81b80c6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions docs/guide/api-javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const { createServer } = require('vite')
The `InlineConfig` interface extends `UserConfig` with additional properties:

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

## `ViteDevServer`

Expand Down
5 changes: 3 additions & 2 deletions packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ export interface SSROptions {

export interface InlineConfig extends UserConfig {
configFile?: string | false
envFile?: false
}

export type ResolvedConfig = Readonly<
Omit<UserConfig, 'plugins' | 'alias' | 'dedupe' | 'assetsInclude'> & {
configFile: string | undefined
inlineConfig: UserConfig
inlineConfig: InlineConfig
root: string
base: string
publicDir: string
Expand Down Expand Up @@ -273,7 +274,7 @@ export async function resolveConfig(
}

// load .env files
const userEnv = loadEnv(mode, resolvedRoot)
const userEnv = inlineConfig.envFile !== false && loadEnv(mode, resolvedRoot)

// Note it is possible for user to have a custom mode, e.g. `staging` where
// production-like behavior is expected. This is indicated by NODE_ENV=production
Expand Down
8 changes: 6 additions & 2 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,15 @@ export async function handleHMRUpdate(
const { ws, config, moduleGraph } = server
const shortFile = getShortName(file, config.root)

if (file === config.configFile || file.endsWith('.env')) {
const isConfig = file === config.configFile
const isEnv = config.inlineConfig.envFile !== false && file.endsWith('.env')
if (isConfig || isEnv) {
// auto restart server
debugHmr(`[config change] ${chalk.dim(shortFile)}`)
config.logger.info(
chalk.green('config or .env file changed, restarting server...'),
chalk.green(
`${isConfig ? 'config' : '.env'} file changed, restarting server...`
),
{ clear: true, timestamp: true }
)
await restartServer(server)
Expand Down

0 comments on commit 81b80c6

Please sign in to comment.