Skip to content

Commit

Permalink
refactor: change location of server start log messages (vitejs#5016)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored and aleclarson committed Nov 8, 2021
1 parent 926d0c5 commit 61c8aff
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 38 deletions.
34 changes: 32 additions & 2 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { cac } from 'cac'
import chalk from 'chalk'
import { performance } from 'perf_hooks'
import { BuildOptions } from './build'
import { ServerOptions } from './server'
import { createLogger, LogLevel } from './logger'
import { createLogger, LogLevel, printHttpServerUrls } from './logger'
import { resolveConfig } from '.'
import { preview } from './preview'

Expand Down Expand Up @@ -90,6 +91,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
performance.now() - global.__vite_start_time
)
)
}

await server.listen()
} catch (e) {
createLogger(options.logLevel).error(
Expand Down Expand Up @@ -222,7 +250,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
27 changes: 25 additions & 2 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* eslint no-console: 0 */

import chalk from 'chalk'
import readline from 'readline'
import { AddressInfo, Server } from 'net'
import os from 'os'
import readline from 'readline'
import { RollupError } from 'rollup'
import { Hostname } from './utils'
import { ResolvedConfig } from '.'
import { ServerOptions } from './server'
import { Hostname, resolveHostname } from './utils'

export type LogType = 'error' | 'warn' | 'info'
export type LogLevel = LogType | 'silent'
Expand Down Expand Up @@ -137,6 +140,26 @@ export function createLogger(
return logger
}

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
)
}
}

export function printServerUrls(
hostname: Hostname,
protocol: string,
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,7 +13,6 @@ import {
import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'
import { printServerUrls } from './logger'
import { resolveHostname } from './utils'

/**
Expand All @@ -25,7 +24,7 @@ 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 @@ -70,13 +69,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 @@ -85,4 +77,6 @@ export async function preview(
logger
)
}

return httpServer
}
24 changes: 0 additions & 24 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ import {
ssrRewriteStacktrace
} from '../ssr/ssrStacktrace'
import { createMissingImporterRegisterFn } from '../optimizer/registerMissing'
import { printServerUrls } from '../logger'
import { resolveHostname } from '../utils'
import { searchForWorkspaceRoot } from './searchRoot'
import { CLIENT_DIR } from '../constants'
import { performance } from 'perf_hooks'

export { searchForWorkspaceRoot } from './searchRoot'

Expand Down Expand Up @@ -599,28 +597,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(
`\n ready in ${Math.round(
// @ts-ignore
performance.now() - global.__vite_start_time
)}ms.\n`
)
)
}

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

0 comments on commit 61c8aff

Please sign in to comment.