Skip to content

Commit 14b5ced

Browse files
authored
feat: add an option to not start a websocket server (#16219)
1 parent e861168 commit 14b5ced

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

packages/vite/src/node/server/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ export interface ServerOptions extends CommonServerOptions {
101101
* Configure HMR-specific options (port, host, path & protocol)
102102
*/
103103
hmr?: HmrOptions | boolean
104+
/**
105+
* Do not start the websocket connection.
106+
* @experimental
107+
*/
108+
ws?: false
104109
/**
105110
* Warm-up files to transform and cache the results in advance. This improves the
106111
* initial page load during server starts and prevents transform waterfalls.

packages/vite/src/node/server/ws.ts

+20
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,31 @@ const wsServerEvents = [
8585
'message',
8686
]
8787

88+
function noop() {
89+
// noop
90+
}
91+
8892
export function createWebSocketServer(
8993
server: HttpServer | null,
9094
config: ResolvedConfig,
9195
httpsOptions?: HttpsServerOptions,
9296
): WebSocketServer {
97+
if (config.server.ws === false) {
98+
return {
99+
name: 'ws',
100+
get clients() {
101+
return new Set<WebSocketClient>()
102+
},
103+
async close() {
104+
// noop
105+
},
106+
on: noop as any as WebSocketServer['on'],
107+
off: noop as any as WebSocketServer['off'],
108+
listen: noop,
109+
send: noop,
110+
}
111+
}
112+
93113
let wss: WebSocketServerRaw_
94114
let wsHttpServer: Server | undefined = undefined
95115

0 commit comments

Comments
 (0)