Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rewrite after #2977 #3269

Merged
merged 3 commits into from
May 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 { logHostInfo } from './utils'

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

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

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
Expand Down
27 changes: 2 additions & 25 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 @@ -34,7 +33,7 @@ import {
import { timeMiddleware } from './middlewares/time'
import { ModuleGraph, ModuleNode } from './moduleGraph'
import { Connect } from 'types/connect'
import { createDebugger, normalizePath } from '../utils'
import { createDebugger, logHostInfo, normalizePath } from '../utils'
import { errorMiddleware, prepareError } from './middlewares/error'
import { handleHMRUpdate, HmrOptions, handleFileAddUnlink } from './hmr'
import { openBrowser } from './openBrowser'
Expand Down Expand Up @@ -576,29 +575,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)}`)
})
)
}
logHostInfo(hostname, protocol, port, base, info)

// @ts-ignore
if (global.__vite_start_time) {
Expand Down
28 changes: 28 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DecodedSourceMap,
RawSourceMap
} from '@ampproject/remapping/dist/types/types'
import { Logger } from './logger'

export function slash(p: string): string {
return p.replace(/\\/g, '/')
Expand Down Expand Up @@ -464,3 +465,30 @@ export function combineSourcemaps(
export function unique<T>(arr: T[]): T[] {
return Array.from(new Set(arr))
}

export function logHostInfo(
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
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))
}
}