diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index 5e4ed170208a71..d81b0fb2b6eef4 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -1,9 +1,8 @@ import { cac } from 'cac' import chalk from 'chalk' -import { performance } from 'perf_hooks' import { BuildOptions } from './build' import { ServerOptions } from './server' -import { createLogger, LogLevel, printHttpServerUrls } from './logger' +import { createLogger, LogLevel } from './logger' import { resolveConfig } from '.' import { preview } from './preview' @@ -91,31 +90,7 @@ 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') - } - await server.listen() - - printHttpServerUrls(server.httpServer, server.config, options) - - // @ts-ignore - if (global.__vite_start_time) { - // @ts-ignore - const startupDuration = performance.now() - global.__vite_start_time - info(`\n ${chalk.cyan(`ready in ${Math.ceil(startupDuration)}ms.`)}\n`) - } } catch (e) { createLogger(options.logLevel).error( chalk.red(`error when starting dev server:\n${e.stack}`), @@ -247,9 +222,7 @@ cli 'serve', 'production' ) - const server = await preview(config, cleanOptions(options)) - - printHttpServerUrls(server, config, options) + await preview(config, cleanOptions(options)) } catch (e) { createLogger(options.logLevel).error( chalk.red(`error when starting preview server:\n${e.stack}`), diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index 5ba5adf7c63054..5642356231fa4e 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -1,13 +1,10 @@ /* eslint no-console: 0 */ import chalk from 'chalk' -import { AddressInfo, Server } from 'net' -import os from 'os' import readline from 'readline' +import os from 'os' import { RollupError } from 'rollup' -import { ResolvedConfig } from '.' -import { ServerOptions } from './server' -import { Hostname, resolveHostname } from './utils' +import { Hostname } from './utils' export type LogType = 'error' | 'warn' | 'info' export type LogLevel = LogType | 'silent' @@ -140,27 +137,7 @@ 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 - ) - } -} - -function printServerUrls( +export function printServerUrls( hostname: Hostname, protocol: string, port: number, diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 707c492858ec1c..25b9d768da9cb3 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -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 { @@ -13,6 +13,7 @@ import { import { openBrowser } from './server/openBrowser' import corsMiddleware from 'cors' import { proxyMiddleware } from './server/middlewares/proxy' +import { printServerUrls } from './logger' import { resolveHostname } from './utils' /** @@ -24,7 +25,7 @@ import { resolveHostname } from './utils' export async function preview( config: ResolvedConfig, serverOptions: Pick -): Promise { +): Promise { const app = connect() as Connect.Server const httpServer = await resolveHttpServer( config.server, @@ -69,6 +70,13 @@ 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( @@ -79,6 +87,4 @@ export async function preview( logger ) } - - return httpServer } diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 812013d4a47462..5cfbb424259bd9 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -52,9 +52,11 @@ 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' @@ -601,6 +603,28 @@ 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) {