Skip to content

Commit

Permalink
feat: show mode on server start and add env debugger (#18808)
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomuikuta authored Feb 20, 2025
1 parent 73987f2 commit c575b82
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
6 changes: 5 additions & 1 deletion packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ cli

const info = server.config.logger.info

const modeString =
options.mode && options.mode !== 'development'
? ` ${colors.bgGreen(` ${colors.bold(options.mode)} `)}`
: ''
const viteStartTime = global.__vite_start_time ?? false
const startupDurationString = viteStartTime
? colors.dim(
Expand All @@ -215,7 +219,7 @@ cli
info(
`\n ${colors.green(
`${colors.bold('VITE')} v${VERSION}`,
)} ${startupDurationString}\n`,
)}${modeString} ${startupDurationString}\n`,
{
clear: !hasExistingLogs,
},
Expand Down
13 changes: 12 additions & 1 deletion packages/vite/src/node/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import fs from 'node:fs'
import path from 'node:path'
import { parse } from 'dotenv'
import { type DotenvPopulateInput, expand } from 'dotenv-expand'
import { arraify, normalizePath, tryStatSync } from './utils'
import { arraify, createDebugger, normalizePath, tryStatSync } from './utils'
import type { UserConfig } from './config'

const debug = createDebugger('vite:env')

export function getEnvFilesForMode(mode: string, envDir: string): string[] {
return [
/** default file */ `.env`,
Expand All @@ -19,6 +21,9 @@ export function loadEnv(
envDir: string,
prefixes: string | string[] = 'VITE_',
): Record<string, string> {
const start = performance.now()
const getTime = () => `${(performance.now() - start).toFixed(2)}ms`

if (mode === 'local') {
throw new Error(
`"local" cannot be used as a mode name because it conflicts with ` +
Expand All @@ -29,6 +34,8 @@ export function loadEnv(
const env: Record<string, string> = {}
const envFiles = getEnvFilesForMode(mode, envDir)

debug?.(`loading env files: %O`, envFiles)

const parsed = Object.fromEntries(
envFiles.flatMap((filePath) => {
if (!tryStatSync(filePath)?.isFile()) return []
Expand All @@ -37,6 +44,8 @@ export function loadEnv(
}),
)

debug?.(`env files loaded in ${getTime()}`)

// test NODE_ENV override before expand as otherwise process.env.NODE_ENV would override this
if (parsed.NODE_ENV && process.env.VITE_USER_NODE_ENV === undefined) {
process.env.VITE_USER_NODE_ENV = parsed.NODE_ENV
Expand Down Expand Up @@ -69,6 +78,8 @@ export function loadEnv(
}
}

debug?.(`using resolved env: %O`, env)

return env
}

Expand Down

0 comments on commit c575b82

Please # to comment.