From 14b5cedbfdc0a9e0f43855b44bfd6212105ebfdb Mon Sep 17 00:00:00 2001 From: Vladimir Date: Wed, 29 May 2024 16:14:30 +0200 Subject: [PATCH] feat: add an option to not start a websocket server (#16219) --- packages/vite/src/node/server/index.ts | 5 +++++ packages/vite/src/node/server/ws.ts | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 2616cfe58aba4c..7590bafcc3a88e 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -101,6 +101,11 @@ export interface ServerOptions extends CommonServerOptions { * Configure HMR-specific options (port, host, path & protocol) */ hmr?: HmrOptions | boolean + /** + * Do not start the websocket connection. + * @experimental + */ + ws?: false /** * Warm-up files to transform and cache the results in advance. This improves the * initial page load during server starts and prevents transform waterfalls. diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index 6b70d1fbea5e77..d0bffcdce4f8a0 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -85,11 +85,31 @@ const wsServerEvents = [ 'message', ] +function noop() { + // noop +} + export function createWebSocketServer( server: HttpServer | null, config: ResolvedConfig, httpsOptions?: HttpsServerOptions, ): WebSocketServer { + if (config.server.ws === false) { + return { + name: 'ws', + get clients() { + return new Set() + }, + async close() { + // noop + }, + on: noop as any as WebSocketServer['on'], + off: noop as any as WebSocketServer['off'], + listen: noop, + send: noop, + } + } + let wss: WebSocketServerRaw_ let wsHttpServer: Server | undefined = undefined