Skip to content

Commit

Permalink
feat: add an option to not start a websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 20, 2024
1 parent d443428 commit 2ddc0d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export interface ServerOptions extends CommonServerOptions {
* Configure HMR-specific options (port, host, path & protocol)
*/
hmr?: HmrOptions | boolean
/**
* Do not start the websocket connection.
*/
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
21 changes: 21 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,32 @@ const wsServerEvents = [
'message',
]

function noop() {
// noop
}

export function createWebSocketServer(
server: HttpServer | null,
config: ResolvedConfig,
httpsOptions?: HttpsServerOptions,
): WebSocketServer {
if (config.server.ws === false) {
const clients = new Set<WebSocketClient>()
return {
name: 'ws',
get clients() {
return clients
},
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 2ddc0d1

Please sign in to comment.