Skip to content

Commit

Permalink
feat: add --strictPort cli option in vite preview
Browse files Browse the repository at this point in the history
  • Loading branch information
ygj6 committed Jul 17, 2021
1 parent 61dbb3a commit 5966436
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 62 deletions.
5 changes: 4 additions & 1 deletion packages/vite/src/node/cli.ts
Expand Up @@ -190,6 +190,7 @@ cli
.option('--port <port>', `[number] specify port`)
.option('--https', `[boolean] use TLS + HTTP/2`)
.option('--open [path]', `[boolean | string] open browser on startup`)
.option('--strictPort', `[boolean] exit if specified port is already in use`)
.action(
async (
root: string,
Expand All @@ -198,6 +199,7 @@ cli
port?: number
https?: boolean
open?: boolean | string
strictPort?: boolean
} & GlobalCLIOptions
) => {
try {
Expand All @@ -208,7 +210,8 @@ cli
configFile: options.config,
logLevel: options.logLevel,
server: {
open: options.open
open: options.open,
strictPort: options.strictPort
}
},
'serve',
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/preview.ts
Expand Up @@ -59,7 +59,7 @@ export async function preview(

const serverPort = await httpServerStart(httpServer, {
port,
strict: !!serverOptions.port,
strictPort: options.strictPort,
host: hostname.host,
logger
})
Expand Down
6 changes: 3 additions & 3 deletions packages/vite/src/node/server/http.ts
Expand Up @@ -165,17 +165,17 @@ export async function httpServerStart(
httpServer: HttpServer,
serverOptions: {
port: number
strict: boolean | undefined
strictPort: boolean | undefined
host: string | undefined
logger: Logger
}
): Promise<number> {
return new Promise((resolve, reject) => {
let { port, strict, host, logger } = serverOptions
let { port, strictPort, host, logger } = serverOptions

const onError = (e: Error & { code?: string }) => {
if (e.code === 'EADDRINUSE') {
if (strict) {
if (strictPort) {
httpServer.removeListener('error', onError)
reject(new Error(`Port ${port} is already in use`))
} else {
Expand Down
107 changes: 50 additions & 57 deletions packages/vite/src/node/server/index.ts
Expand Up @@ -590,67 +590,60 @@ async function startServer(
const info = server.config.logger.info
const base = server.config.base

return new Promise((resolve, reject) => {
httpServerStart(httpServer, {
port,
strict: options.strictPort,
host: hostname.host,
logger: server.config.logger
})
.then((serverPort) => {
info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
)
const serverPort = await httpServerStart(httpServer, {
port,
strictPort: options.strictPort,
host: hostname.host,
logger: server.config.logger
})

printServerUrls(hostname, protocol, serverPort, base, info)
info(
chalk.cyan(`\n vite v${require('vite/package.json').version}`) +
chalk.green(` dev server running at:\n`),
{
clear: !server.config.logger.hasWarned
}
)

// @ts-ignore
if (global.__vite_start_time) {
info(
chalk.cyan(
// @ts-ignore
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
)
}
printServerUrls(hostname, protocol, serverPort, base, info)

// @ts-ignore
if (global.__vite_start_time) {
info(
chalk.cyan(
// @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
}
})
}

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)
})
})
`\n ready in ${Date.now() - global.__vite_start_time}ms.\n`
)
)
}

// @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
}
})
}

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

return server
}

function createServerCloseFn(server: http.Server | null) {
Expand Down

0 comments on commit 5966436

Please sign in to comment.