Skip to content

Commit

Permalink
fix: revert refactor, change location of server start log messages (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Sep 29, 2021
1 parent 7426c85 commit ce2aa04
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 59 deletions.
31 changes: 2 additions & 29 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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}`),
Expand Down Expand Up @@ -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}`),
Expand Down
29 changes: 3 additions & 26 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 10 additions & 4 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,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'

/**
Expand All @@ -24,7 +25,7 @@ import { resolveHostname } from './utils'
export async function preview(
config: ResolvedConfig,
serverOptions: Pick<ServerOptions, 'port' | 'host'>
): Promise<Server> {
): Promise<void> {
const app = connect() as Connect.Server
const httpServer = await resolveHttpServer(
config.server,
Expand Down Expand Up @@ -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(
Expand All @@ -79,6 +87,4 @@ export async function preview(
logger
)
}

return httpServer
}
24 changes: 24 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit ce2aa04

Please sign in to comment.