Skip to content

feat: show mode on server start and add env debugger #18808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,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 @@ -208,7 +212,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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we show process.env values (internal values) too? What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this current log is fine for now 👍


return env
}

Expand Down
Loading