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 all 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
6 changes: 6 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 @@ -135,6 +140,7 @@ export async function preview(
config,
middlewares: app,
httpServer,
close: createServerCloseFn(httpServer),
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