Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an option to not start a websocket server #16219

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -96,6 +96,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