From 05586fbe399b40929a01ef7da146e810481ec923 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 17 Jan 2024 20:24:09 +0800 Subject: [PATCH 1/3] feat: preview server add close method --- packages/vite/src/node/preview.ts | 9 +++++++++ packages/vite/src/node/server/index.ts | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 68a650438f32fa..8fccd20e1ee404 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -9,6 +9,7 @@ import type { ResolvedServerOptions, ResolvedServerUrls, } from './server' +import { createServerCloseFn } from './server' import type { CommonServerOptions } from './http' import { httpServerStart, @@ -59,6 +60,10 @@ export interface PreviewServer { * The resolved vite config object */ config: ResolvedConfig + /** + * Stop the server. + */ + close(): Promise /** * A connect app instance. * - Can be used to attach custom middlewares to the preview server. @@ -127,6 +132,7 @@ export async function preview( await resolveHttpsConfig(config.preview?.https), ) setClientErrorHandler(httpServer, config.logger) + const closeHttpServer = createServerCloseFn(httpServer) as () => Promise const options = config.preview const logger = config.logger @@ -135,6 +141,9 @@ export async function preview( config, middlewares: app, httpServer, + async close() { + await closeHttpServer() + }, resolvedUrls: null, printUrls() { if (server.resolvedUrls) { diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index c7c5270067adbf..f099d07ba9198d 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -896,7 +896,9 @@ async function startServer( server._currentServerPort = serverPort } -function createServerCloseFn(server: HttpServer | null) { +export function createServerCloseFn( + server: HttpServer | null, +): () => Promise | void { if (!server) { return () => {} } From 2f2e2818d4724f42e1995f2b8eab64175a5cb765 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 17 Jan 2024 22:56:42 +0800 Subject: [PATCH 2/3] feat: update createServerCloseFn --- packages/vite/src/node/preview.ts | 2 +- packages/vite/src/node/server/index.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index 8fccd20e1ee404..a1064114a442e9 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -132,7 +132,7 @@ export async function preview( await resolveHttpsConfig(config.preview?.https), ) setClientErrorHandler(httpServer, config.logger) - const closeHttpServer = createServerCloseFn(httpServer) as () => Promise + const closeHttpServer = createServerCloseFn(httpServer) const options = config.preview const logger = config.logger diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index f099d07ba9198d..93de3f19feca20 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -898,9 +898,9 @@ async function startServer( export function createServerCloseFn( server: HttpServer | null, -): () => Promise | void { +): () => Promise { if (!server) { - return () => {} + return () => Promise.resolve() } let hasListened = false From e7db7aa207fe3a05ffd2dc6e0ec6b4eda51e026a Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Thu, 18 Jan 2024 09:18:14 +0800 Subject: [PATCH 3/3] feat: remove variable --- packages/vite/src/node/preview.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/packages/vite/src/node/preview.ts b/packages/vite/src/node/preview.ts index a1064114a442e9..837e508c5af9b4 100644 --- a/packages/vite/src/node/preview.ts +++ b/packages/vite/src/node/preview.ts @@ -132,7 +132,6 @@ 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 @@ -141,9 +140,7 @@ export async function preview( config, middlewares: app, httpServer, - async close() { - await closeHttpServer() - }, + close: createServerCloseFn(httpServer), resolvedUrls: null, printUrls() { if (server.resolvedUrls) {