Skip to content

Commit 809ab47

Browse files
authored
refactor(cli): improve output aesthetics (#6997)
1 parent 45dba50 commit 809ab47

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

packages/vite/src/node/cli.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -97,24 +97,24 @@ cli
9797

9898
const info = server.config.logger.info
9999

100+
// @ts-ignore
101+
const viteStartTime = global.__vite_start_time ?? false
102+
const startupDurationString = viteStartTime
103+
? colors.dim(
104+
`ready in ${colors.white(
105+
colors.bold(Math.ceil(performance.now() - viteStartTime))
106+
)} ms`
107+
)
108+
: ''
109+
100110
info(
101-
colors.cyan(`\n vite v${VERSION}`) +
102-
colors.green(` dev server running at:\n`),
103-
{
104-
clear: !server.config.logger.hasWarned
105-
}
111+
`\n ${colors.green(
112+
`${colors.bold('VITE')} v${VERSION}`
113+
)} ${startupDurationString}\n`,
114+
{ clear: !server.config.logger.hasWarned }
106115
)
107116

108117
server.printUrls()
109-
110-
// @ts-ignore
111-
if (global.__vite_start_time) {
112-
// @ts-ignore
113-
const startupDuration = performance.now() - global.__vite_start_time
114-
info(
115-
`\n ${colors.cyan(`ready in ${Math.ceil(startupDuration)}ms.`)}\n`
116-
)
117-
}
118118
} catch (e) {
119119
createLogger(options.logLevel).error(
120120
colors.red(`error when starting dev server:\n${e.stack}`),

packages/vite/src/node/logger.ts

+29-9
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,21 @@ function printServerUrls(
171171
base: string,
172172
info: Logger['info']
173173
): void {
174+
const urls: Array<{ label: string; url: string }> = []
175+
174176
if (hostname.host === '127.0.0.1') {
175-
const url = `${protocol}://${hostname.name}:${colors.bold(port)}${base}`
176-
info(` > Local: ${colors.cyan(url)}`)
177+
urls.push({
178+
label: 'Local',
179+
url: colors.cyan(
180+
`${protocol}://${hostname.name}:${colors.bold(port)}${base}`
181+
)
182+
})
183+
177184
if (hostname.name !== '127.0.0.1') {
178-
info(` > Network: ${colors.dim('use `--host` to expose')}`)
185+
urls.push({
186+
label: 'Network',
187+
url: colors.dim(`use ${colors.white(colors.bold('--host'))} to expose`)
188+
})
179189
}
180190
} else {
181191
Object.values(os.networkInterfaces())
@@ -189,14 +199,24 @@ function printServerUrls(
189199
// Node >= v18
190200
(typeof detail.family === 'number' && detail.family === 4))
191201
)
192-
.map((detail) => {
193-
const type = detail.address.includes('127.0.0.1')
194-
? 'Local: '
195-
: 'Network: '
202+
.forEach((detail) => {
196203
const host = detail.address.replace('127.0.0.1', hostname.name)
197204
const url = `${protocol}://${host}:${colors.bold(port)}${base}`
198-
return ` > ${type} ${colors.cyan(url)}`
205+
const label = detail.address.includes('127.0.0.1') ? 'Local' : 'Network'
206+
207+
urls.push({ label, url: colors.cyan(url) })
199208
})
200-
.forEach((msg) => info(msg))
201209
}
210+
211+
const length = urls.reduce(
212+
(length, { label }) => Math.max(length, label.length),
213+
0
214+
)
215+
urls.forEach(({ label, url: text }) => {
216+
info(
217+
` ${colors.green('➜')} ${colors.bold(label)}: ${' '.repeat(
218+
length - label.length
219+
)}${text}`
220+
)
221+
})
202222
}

0 commit comments

Comments
 (0)