diff --git a/packages/vite/src/node/cli.ts b/packages/vite/src/node/cli.ts index 5c2566a7eef87a..605c274cba2e58 100644 --- a/packages/vite/src/node/cli.ts +++ b/packages/vite/src/node/cli.ts @@ -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' @@ -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( @@ -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}`), diff --git a/packages/vite/src/node/logger.ts b/packages/vite/src/node/logger.ts index fb2365db14055c..a6a6ae279f71e2 100644 --- a/packages/vite/src/node/logger.ts +++ b/packages/vite/src/node/logger.ts @@ -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' @@ -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, diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index b2588c738910ec..d4061d6b9a7bbd 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,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' /** @@ -25,7 +24,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, @@ -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( @@ -85,4 +77,6 @@ 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 80d731e0b45da1..577bca9ab633f0 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -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' @@ -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) {