Skip to content

Commit

Permalink
feat: add an option to not start a websocket server (#16219)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored May 29, 2024
1 parent e861168 commit 14b5ced
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
20 changes: 20 additions & 0 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<WebSocketClient>()
},
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

Expand Down

0 comments on commit 14b5ced

Please sign in to comment.