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: allow disabling ws server completely if hmr: false #2474

Closed
Closed
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
11 changes: 9 additions & 2 deletions packages/vite/src/node/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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), {
Expand Down
24 changes: 24 additions & 0 deletions packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
}