Skip to content

Commit

Permalink
refactor: rewrite after vitejs#2977 (vitejs#3269)
Browse files Browse the repository at this point in the history
Co-authored-by: antfu <hi@antfu.me>
  • Loading branch information
Shinigami92 and antfu committed May 8, 2021
1 parent 792a6e1 commit d2b3ba1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 48 deletions.
28 changes: 28 additions & 0 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import chalk from 'chalk'
import readline from 'readline'
import os from 'os'

export type LogType = 'error' | 'warn' | 'info'
export type LogLevel = LogType | 'silent'
Expand Down Expand Up @@ -108,3 +109,30 @@ export function createLogger(

return logger
}

export function printServerUrls(
hostname: string | undefined,
protocol: string,
port: number,
base: string,
info: Logger['info']
): void {
if (hostname === '127.0.0.1') {
const url = `${protocol}://localhost:${chalk.bold(port)}${base}`
info(` > Local: ${chalk.cyan(url)}`)
info(` > Network: ${chalk.dim('use `--host` to expose')}`)
} else {
Object.values(os.networkInterfaces())
.flatMap((nInterface) => nInterface ?? [])
.filter((detail) => detail.family === 'IPv4')
.map((detail) => {
const type = detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: '
const host = detail.address
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
return ` > ${type} ${chalk.cyan(url)}`
})
.forEach((msg) => info(msg))
}
}
27 changes: 3 additions & 24 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os from 'os'
import path from 'path'
import sirv from 'sirv'
import chalk from 'chalk'
Expand All @@ -10,6 +9,7 @@ import { resolveHttpServer } from './server/http'
import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'
import { printServerUrls } from './logger'

export async function preview(
config: ResolvedConfig,
Expand Down Expand Up @@ -60,29 +60,8 @@ export async function preview(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
)
if (hostname === '127.0.0.1') {
const url = `${protocol}://localhost:${chalk.bold(port)}${base}`
logger.info(` > Local: ${chalk.cyan(url)}`)
logger.info(` > Network: ${chalk.dim('use `--host` to expose')}`)
} else {
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address
}
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
logger.info(` > ${type} ${chalk.cyan(url)}`)
})
)
}

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

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
Expand Down
26 changes: 2 additions & 24 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os from 'os'
import fs from 'fs'
import path from 'path'
import * as net from 'net'
Expand Down Expand Up @@ -51,6 +50,7 @@ import { ssrLoadModule } from '../ssr/ssrModuleLoader'
import { resolveSSRExternal } from '../ssr/ssrExternal'
import { ssrRewriteStacktrace } from '../ssr/ssrStacktrace'
import { createMissingImporterRegisterFn } from '../optimizer/registerMissing'
import { printServerUrls } from '../logger'

export interface ServerOptions {
host?: string | boolean
Expand Down Expand Up @@ -596,29 +596,7 @@ async function startServer(
}
)

if (hostname === '127.0.0.1') {
const url = `${protocol}://localhost:${chalk.bold(port)}${base}`
info(` > Local: ${chalk.cyan(url)}`)
info(` > Network: ${chalk.dim('use `--host` to expose')}`)
} else {
const interfaces = os.networkInterfaces()
Object.keys(interfaces).forEach((key) =>
(interfaces[key] || [])
.filter((details) => details.family === 'IPv4')
.map((detail) => {
return {
type: detail.address.includes('127.0.0.1')
? 'Local: '
: 'Network: ',
host: detail.address
}
})
.forEach(({ type, host }) => {
const url = `${protocol}://${host}:${chalk.bold(port)}${base}`
info(` > ${type} ${chalk.cyan(url)}`)
})
)
}
printServerUrls(hostname, protocol, port, base, info)

// @ts-ignore
if (global.__vite_start_time) {
Expand Down

0 comments on commit d2b3ba1

Please sign in to comment.