From 1d2db7edef5b91e947df697c5172a9848cfe1e3d Mon Sep 17 00:00:00 2001
From: Nozomu Ikuta <16436160+NozomuIkuta@users.noreply.github.com>
Date: Wed, 27 Nov 2024 22:57:10 +0900
Subject: [PATCH] feat: show more info on server start
---
.../2. feature-section/FeatureTypedAPI.vue | 2 +-
docs/guide/api-javascript.md | 6 ++---
packages/vite/src/node/cli.ts | 4 ++--
packages/vite/src/node/env.ts | 19 +++++++++++++--
packages/vite/src/node/logger.ts | 23 +++++++++++++++----
packages/vite/src/node/preview.ts | 18 +++++++++++++--
packages/vite/src/node/server/index.ts | 16 ++++++++++---
packages/vite/src/node/shortcuts.ts | 4 ++--
playground/cli/.env | 0
playground/cli/__tests__/cli.spec.ts | 2 +-
10 files changed, 73 insertions(+), 21 deletions(-)
create mode 100644 playground/cli/.env
diff --git a/docs/.vitepress/theme/components/landing/2. feature-section/FeatureTypedAPI.vue b/docs/.vitepress/theme/components/landing/2. feature-section/FeatureTypedAPI.vue
index 1c54317778b824..c5088d3101b03a 100644
--- a/docs/.vitepress/theme/components/landing/2. feature-section/FeatureTypedAPI.vue
+++ b/docs/.vitepress/theme/components/landing/2. feature-section/FeatureTypedAPI.vue
@@ -45,7 +45,7 @@ const { isCardActive, startAnimation } = useCardAnimation(
>listen()
server.printInfo()
diff --git a/docs/guide/api-javascript.md b/docs/guide/api-javascript.md
index 46ea0259011a0d..57c311370b0974 100644
--- a/docs/guide/api-javascript.md
+++ b/docs/guide/api-javascript.md
@@ -28,7 +28,7 @@ const server = await createServer({
})
await server.listen()
-server.printUrls()
+server.printInfo()
server.bindCLIShortcuts({ print: true })
```
@@ -248,7 +248,7 @@ const previewServer = await preview({
},
})
-previewServer.printUrls()
+previewServer.printInfo()
previewServer.bindCLIShortcuts({ print: true })
```
@@ -281,7 +281,7 @@ interface PreviewServer {
/**
* Print server urls
*/
- printUrls(): void
+ printInfo(): void
/**
* Bind CLI shortcuts
*/
diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts
index b520b9b5144b46..24ee7fb830f378 100644
--- a/packages/vite/src/node/cli.ts
+++ b/packages/vite/src/node/cli.ts
@@ -214,7 +214,7 @@ cli
},
)
- server.printUrls()
+ server.printInfo()
const customShortcuts: CLIShortcut[] = []
if (profileSession) {
customShortcuts.push({
@@ -394,7 +394,7 @@ cli
open: options.open,
},
})
- server.printUrls()
+ server.printInfo()
server.bindCLIShortcuts({ print: true })
} catch (e) {
createLogger(options.logLevel).error(
diff --git a/packages/vite/src/node/env.ts b/packages/vite/src/node/env.ts
index 897524612f3c7d..475dc7410d029a 100644
--- a/packages/vite/src/node/env.ts
+++ b/packages/vite/src/node/env.ts
@@ -5,13 +5,28 @@ import { type DotenvPopulateInput, expand } from 'dotenv-expand'
import { arraify, normalizePath, tryStatSync } from './utils'
import type { UserConfig } from './config'
-export function getEnvFilesForMode(mode: string, envDir: string): string[] {
+function getEnvFileNamesForMode(mode: string): string[] {
return [
/** default file */ `.env`,
/** local file */ `.env.local`,
/** mode file */ `.env.${mode}`,
/** mode local file */ `.env.${mode}.local`,
- ].map((file) => normalizePath(path.join(envDir, file)))
+ ]
+}
+
+export function getEnvFilesForMode(mode: string, envDir: string): string[] {
+ return getEnvFileNamesForMode(mode).map((fileName) =>
+ normalizePath(path.join(envDir, fileName)),
+ )
+}
+
+export function getLoadedEnvFileNamesForMode(
+ mode: string,
+ envDir: string,
+): string[] {
+ return getEnvFileNamesForMode(mode).filter((fileName) =>
+ tryStatSync(normalizePath(path.join(envDir, fileName)))?.isFile(),
+ )
}
export function loadEnv(
diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts
index 8bfa027c61fcd2..c43dcb62f2f882 100644
--- a/packages/vite/src/node/logger.ts
+++ b/packages/vite/src/node/logger.ts
@@ -4,6 +4,7 @@ import readline from 'node:readline'
import colors from 'picocolors'
import type { RollupError } from 'rollup'
import type { ResolvedServerUrls } from './server'
+import { getLoadedEnvFileNamesForMode } from './env'
export type LogType = 'error' | 'warn' | 'info'
export type LogLevel = LogType | 'silent'
@@ -165,19 +166,31 @@ export function createLogger(
return logger
}
-export function printServerUrls(
+export function printServerInfo(
urls: ResolvedServerUrls,
optionsHost: string | boolean | undefined,
+ mode: string,
+ envDir: string,
info: Logger['info'],
): void {
- const colorUrl = (url: string) =>
- colors.cyan(url.replace(/:(\d+)\//, (_, port) => `:${colors.bold(port)}/`))
+ const formatUrl = (url: string) =>
+ url.replace(/:(\d+)\//, (_, port) => `:${colors.bold(port)}/`)
+
for (const url of urls.local) {
- info(` ${colors.green('➜')} ${colors.bold('Local')}: ${colorUrl(url)}`)
+ info(
+ ` ${colors.green('➜')} ${colors.bold('Local')}: ${colors.cyan(formatUrl(url))}`,
+ )
}
for (const url of urls.network) {
- info(` ${colors.green('➜')} ${colors.bold('Network')}: ${colorUrl(url)}`)
+ info(
+ ` ${colors.green('➜')} ${colors.bold('Network')}: ${colors.cyan(formatUrl(url))}`,
+ )
}
+ info(` ${colors.green('➜')} ${colors.bold('Mode')}: ${mode}`)
+ const envFiles = getLoadedEnvFileNamesForMode(mode, envDir)
+ info(
+ ` ${colors.green('➜')} ${colors.bold('Env')}: ${envFiles.length ? envFiles.join(' ') : 'no env files loaded'}`,
+ )
if (urls.network.length === 0 && optionsHost === undefined) {
info(
colors.dim(` ${colors.green('➜')} ${colors.bold('Network')}: use `) +
diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts
index 2ff3984515a292..594fb0ae53cc58 100644
--- a/packages/vite/src/node/preview.ts
+++ b/packages/vite/src/node/preview.ts
@@ -31,7 +31,7 @@ import {
shouldServeFile,
teardownSIGTERMListener,
} from './utils'
-import { printServerUrls } from './logger'
+import { printServerInfo } from './logger'
import { bindCLIShortcuts } from './shortcuts'
import type { BindCLIShortcutsOptions } from './shortcuts'
import { configDefaults, resolveConfig } from './config'
@@ -89,8 +89,13 @@ export interface PreviewServer {
resolvedUrls: ResolvedServerUrls | null
/**
* Print server urls
+ * @deprecated use `printInfo` instead
*/
printUrls(): void
+ /**
+ * Print server info
+ */
+ printInfo(): void
/**
* Bind CLI shortcuts
*/
@@ -157,8 +162,17 @@ export async function preview(
},
resolvedUrls: null,
printUrls() {
+ return this.printInfo()
+ },
+ printInfo() {
if (server.resolvedUrls) {
- printServerUrls(server.resolvedUrls, options.host, logger.info)
+ printServerInfo(
+ server.resolvedUrls,
+ options.host,
+ config.mode,
+ config.envDir,
+ logger.info,
+ )
} else {
throw new Error('cannot print server URLs before server is listening.')
}
diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts
index 772b163a745eb7..456dbcd71e631b 100644
--- a/packages/vite/src/node/server/index.ts
+++ b/packages/vite/src/node/server/index.ts
@@ -46,7 +46,7 @@ import type { BindCLIShortcutsOptions } from '../shortcuts'
import { ERR_OUTDATED_OPTIMIZED_DEP } from '../../shared/constants'
import { CLIENT_DIR, DEFAULT_DEV_PORT } from '../constants'
import type { Logger } from '../logger'
-import { printServerUrls } from '../logger'
+import { printServerInfo } from '../logger'
import { warnFutureDeprecation } from '../deprecations'
import {
createNoopWatcher,
@@ -340,8 +340,13 @@ export interface ViteDevServer {
close(): Promise
/**
* Print server urls
+ * @deprecated use `printInfo` instead
*/
printUrls(): void
+ /**
+ * Print server info
+ */
+ printInfo(): void
/**
* Bind CLI shortcuts
*/
@@ -679,10 +684,15 @@ export async function _createServer(
server.resolvedUrls = null
},
printUrls() {
+ return this.printInfo()
+ },
+ printInfo() {
if (server.resolvedUrls) {
- printServerUrls(
+ printServerInfo(
server.resolvedUrls,
serverConfig.host,
+ config.mode,
+ config.envDir,
config.logger.info,
)
} else if (middlewareMode) {
@@ -1217,6 +1227,6 @@ export async function restartServerWithUrls(
diffDnsOrderChange(prevUrls, server.resolvedUrls)
) {
logger.info('')
- server.printUrls()
+ server.printInfo()
}
}
diff --git a/packages/vite/src/node/shortcuts.ts b/packages/vite/src/node/shortcuts.ts
index 1bac571627008b..d9ea1d2f661b0e 100644
--- a/packages/vite/src/node/shortcuts.ts
+++ b/packages/vite/src/node/shortcuts.ts
@@ -102,10 +102,10 @@ const BASE_DEV_SHORTCUTS: CLIShortcut[] = [
},
{
key: 'u',
- description: 'show server url',
+ description: 'show server info',
action(server) {
server.config.logger.info('')
- server.printUrls()
+ server.printInfo()
},
},
{
diff --git a/playground/cli/.env b/playground/cli/.env
new file mode 100644
index 00000000000000..e69de29bb2d1d6
diff --git a/playground/cli/__tests__/cli.spec.ts b/playground/cli/__tests__/cli.spec.ts
index 399a05115dfd77..fb646ff29ed11c 100644
--- a/playground/cli/__tests__/cli.spec.ts
+++ b/playground/cli/__tests__/cli.spec.ts
@@ -28,7 +28,7 @@ test.runIf(isServe)('should restart', async () => {
expect(logs).toEqual(
expect.arrayContaining([expect.stringMatching('server restarted')]),
)
- // Don't reprint the server URLs as they are the same
+ // Don't reprint the server info as they are the same
expect(logs).not.toEqual(
expect.arrayContaining([expect.stringMatching('http://localhost')]),
)