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: preview server add close method #15630

Merged
merged 3 commits into from
Jan 18, 2024
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
9 changes: 9 additions & 0 deletions packages/vite/src/node/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ResolvedServerOptions,
ResolvedServerUrls,
} from './server'
import { createServerCloseFn } from './server'
import type { CommonServerOptions } from './http'
import {
httpServerStart,
Expand Down Expand Up @@ -59,6 +60,10 @@ export interface PreviewServer {
* The resolved vite config object
*/
config: ResolvedConfig
/**
* Stop the server.
*/
close(): Promise<void>
/**
* A connect app instance.
* - Can be used to attach custom middlewares to the preview server.
Expand Down Expand Up @@ -127,6 +132,7 @@ export async function preview(
await resolveHttpsConfig(config.preview?.https),
)
setClientErrorHandler(httpServer, config.logger)
const closeHttpServer = createServerCloseFn(httpServer)

const options = config.preview
const logger = config.logger
Expand All @@ -135,6 +141,9 @@ export async function preview(
config,
middlewares: app,
httpServer,
async close() {
await closeHttpServer()
},
btea marked this conversation as resolved.
Show resolved Hide resolved
resolvedUrls: null,
printUrls() {
if (server.resolvedUrls) {
Expand Down
6 changes: 4 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -896,9 +896,11 @@ async function startServer(
server._currentServerPort = serverPort
}

function createServerCloseFn(server: HttpServer | null) {
export function createServerCloseFn(
server: HttpServer | null,
): () => Promise<void> {
if (!server) {
return () => {}
return () => Promise.resolve()
}

let hasListened = false
Expand Down