diff --git a/packages/vite/src/node/server/index.ts b/packages/vite/src/node/server/index.ts index 045e1d8dcda389..639a81343330c0 100644 --- a/packages/vite/src/node/server/index.ts +++ b/packages/vite/src/node/server/index.ts @@ -16,7 +16,11 @@ import { PluginContainer } from '../server/pluginContainer' import { FSWatcher, WatchOptions } from 'types/chokidar' -import { createWebSocketServer, WebSocketServer } from '../server/ws' +import { + createMockWebSocketServer, + createWebSocketServer, + WebSocketServer +} from '../server/ws' import { baseMiddleware } from './middlewares/base' import { proxyMiddleware, ProxyOptions } from './middlewares/proxy' import { transformMiddleware } from './middlewares/transform' @@ -269,7 +273,10 @@ export async function createServer( const httpServer = middlewareMode ? null : await resolveHttpServer(serverConfig, middlewares) - const ws = createWebSocketServer(httpServer, config) + const ws = + config.server.hmr === false + ? createMockWebSocketServer(httpServer, config) + : createWebSocketServer(httpServer, config) const { ignored = [], ...watchOptions } = serverConfig.watch || {} const watcher = chokidar.watch(path.resolve(root), { diff --git a/packages/vite/src/node/server/ws.ts b/packages/vite/src/node/server/ws.ts index e704717f8f2b2d..68bf71d77170c9 100644 --- a/packages/vite/src/node/server/ws.ts +++ b/packages/vite/src/node/server/ws.ts @@ -86,3 +86,27 @@ export function createWebSocketServer( } } } + +export function createMockWebSocketServer( + _server: Server | null, + config: ResolvedConfig +) { + return { + send(payload: HMRPayload) { + if (payload.type === 'error') { + config.logger.info( + chalk.red(`WebSocket server [mock] error:\n${payload.err.message}`) + ) + } else { + const formattedPayload = JSON.stringify(payload, undefined, 2) + config.logger.info( + chalk.dim(`WebSocket server [mock] payload:\n${formattedPayload}`) + ) + } + }, + + close() { + return Promise.resolve() + } + } +}