Skip to content

Commit

Permalink
refactor: change location of server start log messages
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann committed Sep 22, 2021
1 parent 7c0188f commit 05a15b4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 33 deletions.
57 changes: 54 additions & 3 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { cac } from 'cac'
import chalk from 'chalk'
import { AddressInfo, Server } from 'net'
import { BuildOptions } from './build'
import { ServerOptions } from './server'
import { createLogger, LogLevel } from './logger'
import { resolveConfig } from '.'
import { createLogger, LogLevel, printServerUrls } from './logger'
import { resolveConfig, ResolvedConfig } from '.'
import { preview } from './preview'
import { resolveHostname } from './utils'

const cli = cac('vite')

Expand Down Expand Up @@ -52,6 +54,26 @@ function cleanOptions<Options extends GlobalCLIOptions>(
return ret
}

export function printHttpServerUrls(
server: Server,
config: ResolvedConfig,
options: ServerOptions
): void {
const address = server.address()
const isAddressInfo = (x: any): x is AddressInfo => x.address
if (isAddressInfo(address)) {
const hostname = resolveHostname(options.host)
const protocol = config.server.https ? 'https' : 'http'
printServerUrls(
hostname,
protocol,
address.port,
config.base,
config.logger.info
)
}
}

cli
.option('-c, --config <file>', `[string] use specified config file`)
.option('-r, --root <path>', `[string] use specified root directory`)
Expand Down Expand Up @@ -90,6 +112,33 @@ cli
clearScreen: options.clearScreen,
server: cleanOptions(options)
})

const info = server.config.logger.info

info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
)

if (!server.httpServer) {
throw new Error('HTTP server not available')
}

printHttpServerUrls(server.httpServer, server.config, options)

// @ts-ignore
if (global.__vite_start_time) {
info(
chalk.cyan(
// @ts-ignore
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
)
}

await server.listen()
} catch (e) {
createLogger(options.logLevel).error(
Expand Down Expand Up @@ -222,7 +271,9 @@ cli
'serve',
'production'
)
await preview(config, cleanOptions(options))
const server = await preview(config, cleanOptions(options))

printHttpServerUrls(server, config, options)
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting preview server:\n${e.stack}`),
Expand Down
14 changes: 4 additions & 10 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import path from 'path'
import sirv from 'sirv'
import chalk from 'chalk'
import connect from 'connect'
import compression from 'compression'
import { Server } from 'http'
import { ResolvedConfig, ServerOptions } from '.'
import { Connect } from 'types/connect'
import {
Expand All @@ -13,13 +13,12 @@ import {
import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'
import { printServerUrls } from './logger'
import { resolveHostname } from './utils'

export async function preview(
config: ResolvedConfig,
serverOptions: Pick<ServerOptions, 'port' | 'host'>
): Promise<void> {
): Promise<Server> {
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
config.server,
Expand Down Expand Up @@ -64,13 +63,6 @@ export async function preview(
logger
})

logger.info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
)

printServerUrls(hostname, protocol, serverPort, base, logger.info)

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(
Expand All @@ -79,4 +71,6 @@ export async function preview(
logger
)
}

return httpServer
}
20 changes: 0 additions & 20 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,26 +588,6 @@ async function startServer(
logger: server.config.logger
})

info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
)

printServerUrls(hostname, protocol, serverPort, base, info)

// @ts-ignore
if (global.__vite_start_time) {
info(
chalk.cyan(
// @ts-ignore
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
)
}

// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
Expand Down

0 comments on commit 05a15b4

Please sign in to comment.