From 9243cc9e8ea6271eb5d548ff459eb7cc5f260598 Mon Sep 17 00:00:00 2001 From: Eren Bets <0x142857@gmail.com> Date: Mon, 15 Mar 2021 22:12:22 +0800 Subject: [PATCH] feat: allow custom websocket server (#2338) --- packages/vite/src/node/server/hmr.ts | 2 ++ packages/vite/src/node/server/ws.ts | 11 ++++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/server/hmr.ts b/packages/vite/src/node/server/hmr.ts index 49a6c7259a6dc0..7f7752a71fc2a9 100644 --- a/packages/vite/src/node/server/hmr.ts +++ b/packages/vite/src/node/server/hmr.ts @@ -9,6 +9,7 @@ import { CLIENT_DIR } from '../constants' import { RollupError } from 'rollup' import { prepareError } from './middlewares/error' import match from 'minimatch' +import { Server } from 'http' export const debugHmr = createDebugger('vite:hmr') @@ -21,6 +22,7 @@ export interface HmrOptions { path?: string timeout?: number overlay?: boolean + server?: Server } export interface HmrContext { diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index 80cf07c0492a32..e704717f8f2b2d 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -17,9 +17,12 @@ export function createWebSocketServer( ): WebSocketServer { let wss: WebSocket.Server - if (server) { + const hmr = typeof config.server.hmr === 'object' && config.server.hmr + const wsServer = (hmr && hmr.server) || server + + if (wsServer) { wss = new WebSocket.Server({ noServer: true }) - server.on('upgrade', (req, socket, head) => { + wsServer.on('upgrade', (req, socket, head) => { if (req.headers['sec-websocket-protocol'] === HMR_HEADER) { wss.handleUpgrade(req, socket, head, (ws) => { wss.emit('connection', ws, req) @@ -29,9 +32,7 @@ export function createWebSocketServer( } else { // vite dev server in middleware mode wss = new WebSocket.Server({ - port: - (typeof config.server.hmr === 'object' && config.server.hmr.port) || - 24678 + port: (hmr && hmr.port) || 24678 }) }