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

feat: vite preview port is used automatically +1 #4219

Merged
merged 3 commits into from Jul 19, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 25 additions & 12 deletions packages/vite/src/node/preview.ts
Expand Up @@ -5,7 +5,11 @@ import connect from 'connect'
import compression from 'compression'
import { ResolvedConfig } from '.'
import { Connect } from 'types/connect'
import { resolveHttpsConfig, resolveHttpServer } from './server/http'
import {
resolveHttpsConfig,
resolveHttpServer,
httpServerStart
} from './server/http'
import { openBrowser } from './server/openBrowser'
import corsMiddleware from 'cors'
import { proxyMiddleware } from './server/middlewares/proxy'
Expand Down Expand Up @@ -53,17 +57,26 @@ export async function preview(
const logger = config.logger
const base = config.base

httpServer.listen(port, hostname.host, () => {
logger.info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
)
const serverPort = await httpServerStart(httpServer, {
port,
strict: !!serverOptions.port,
host: hostname.host,
logger
})

printServerUrls(hostname, protocol, port, base, logger.info)
logger.info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` build preview server running at:\n`)
)

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(`${protocol}://${hostname.name}:${port}${path}`, true, logger)
}
})
printServerUrls(hostname, protocol, serverPort, base, logger.info)

if (options.open) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(
`${protocol}://${hostname.name}:${serverPort}${path}`,
true,
logger
)
}
}
37 changes: 37 additions & 0 deletions packages/vite/src/node/server/http.ts
Expand Up @@ -4,6 +4,7 @@ import { Server as HttpServer } from 'http'
import { ServerOptions as HttpsServerOptions } from 'https'
import { ResolvedConfig, ServerOptions } from '..'
import { Connect } from 'types/connect'
import { Logger } from '../logger'

export async function resolveHttpServer(
{ proxy }: ServerOptions,
Expand Down Expand Up @@ -159,3 +160,39 @@ async function getCertificate(config: ResolvedConfig) {
return content
}
}

export async function httpServerStart(
httpServer: HttpServer,
serverOptions: {
port: number
strict: boolean | undefined
host: string | undefined
logger: Logger
}
): Promise<number> {
return new Promise((resolve, reject) => {
let { port, strict, host, logger } = serverOptions

const onError = (e: Error & { code?: string }) => {
if (e.code === 'EADDRINUSE') {
if (strict) {
httpServer.removeListener('error', onError)
reject(new Error(`Port ${port} is already in use`))
} else {
logger.info(`Port ${port} is in use, trying another one...`)
httpServer.listen(++port, host)
}
} else {
httpServer.removeListener('error', onError)
reject(e)
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
}
}

httpServer.on('error', onError)

httpServer.listen(port, host, () => {
httpServer.removeListener('error', onError)
resolve(port)
})
})
}
121 changes: 55 additions & 66 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -8,7 +8,7 @@ import corsMiddleware from 'cors'
import chalk from 'chalk'
import { AddressInfo } from 'net'
import chokidar from 'chokidar'
import { resolveHttpsConfig, resolveHttpServer } from './http'
import { resolveHttpsConfig, resolveHttpServer, httpServerStart } from './http'
import { resolveConfig, InlineConfig, ResolvedConfig } from '../config'
import {
createPluginContainer,
Expand Down Expand Up @@ -583,84 +583,73 @@ async function startServer(
}

const options = server.config.server
let port = inlinePort || options.port || 3000
const port = inlinePort || options.port || 3000
const hostname = resolveHostname(options.host)

const protocol = options.https ? 'https' : 'http'
const info = server.config.logger.info
const base = server.config.base

return new Promise((resolve, reject) => {
const onError = (e: Error & { code?: string }) => {
if (e.code === 'EADDRINUSE') {
if (options.strictPort) {
httpServer.removeListener('error', onError)
reject(new Error(`Port ${port} is already in use`))
} else {
info(`Port ${port} is in use, trying another one...`)
httpServer.listen(++port, hostname.host)
}
} else {
httpServer.removeListener('error', onError)
reject(e)
}
}

httpServer.on('error', onError)

httpServer.listen(port, hostname.host, () => {
httpServer.removeListener('error', onError)

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, port, base, info)

// @ts-ignore
if (global.__vite_start_time) {
httpServerStart(httpServer, {
port,
strict: options.strictPort,
host: hostname.host,
logger: server.config.logger
})
.then((serverPort) => {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
info(
chalk.cyan(
// @ts-ignore
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
)
}

// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
// Write profile to disk, upload, etc.
if (!err) {
const outPath = path.resolve('./vite-profile.cpuprofile')
fs.writeFileSync(outPath, JSON.stringify(profile))
info(
chalk.yellow(
` CPU profile written to ${chalk.white.dim(outPath)}\n`
)
printServerUrls(hostname, protocol, serverPort, base, info)

// @ts-ignore
if (global.__vite_start_time) {
info(
chalk.cyan(
// @ts-ignore
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
} else {
throw err
}
})
}
)
}

if (options.open && !isRestart) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(
`${protocol}://${hostname.name}:${port}${path}`,
true,
server.config.logger
)
}
// @ts-ignore
const profileSession = global.__vite_profile_session
if (profileSession) {
profileSession.post('Profiler.stop', (err: any, { profile }: any) => {
// Write profile to disk, upload, etc.
if (!err) {
const outPath = path.resolve('./vite-profile.cpuprofile')
fs.writeFileSync(outPath, JSON.stringify(profile))
info(
chalk.yellow(
` CPU profile written to ${chalk.white.dim(outPath)}\n`
)
)
} else {
throw err
}
})
}

resolve(server)
})
if (options.open && !isRestart) {
const path = typeof options.open === 'string' ? options.open : base
openBrowser(
`${protocol}://${hostname.name}:${serverPort}${path}`,
true,
server.config.logger
)
}
resolve(server)
})
.catch((e) => {
reject(e)
})
})
}

Expand Down