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