Skip to content

Commit 365fabb

Browse files
committed
wip: fix hmr code
1 parent 63b249a commit 365fabb

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

packages/vite/src/client/client.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import type {
2+
DevRuntime as DevRuntimeType,
3+
Messenger,
4+
} from 'rolldown/experimental/runtime-types'
15
import type { ErrorPayload, HotPayload } from '#types/hmrPayload'
26
import type { ViteHotContext } from '#types/hot'
37
import { HMRClient, HMRContext } from '../shared/hmr'
@@ -617,33 +621,36 @@ export function injectQuery(url: string, queryToInject: string): string {
617621

618622
export { ErrorOverlay }
619623

620-
// TODO: proper types
621-
declare let DevRuntime: { new (socket: any): any } | undefined
624+
declare const DevRuntime: typeof DevRuntimeType
622625

623626
if (isFullBundleMode && typeof DevRuntime !== 'undefined') {
624627
class ViteDevRuntime extends DevRuntime {
625-
createModuleHotContext(moduleId: string) {
628+
override createModuleHotContext(moduleId: string) {
626629
const ctx = createHotContext(moduleId)
627630
// @ts-expect-error TODO: support CSS properly
628631
ctx._internal = { updateStyle, removeStyle }
629632
return ctx
630633
}
631634

632-
applyUpdates(_boundaries: string[]): void {
635+
override applyUpdates(_boundaries: [string, string][]): void {
633636
// noop, handled in the HMR client
634637
}
635638
}
636639

637-
// TODO: make this more performant
638-
const wrappedSocket = {
639-
readyState: WebSocket.OPEN,
640-
send(data: string) {
641-
const d = JSON.parse(data)
642-
transport.send({
643-
type: 'custom',
644-
event: 'vite:module-loaded',
645-
data: { modules: d.modules },
646-
})
640+
const wrappedSocket: Messenger = {
641+
send(message) {
642+
switch (message.type) {
643+
case 'hmr:module-registered': {
644+
transport.send({
645+
type: 'custom',
646+
event: 'vite:module-loaded',
647+
data: { modules: message.modules },
648+
})
649+
break
650+
}
651+
default:
652+
throw new Error(`Unknown message type: ${JSON.stringify(message)}`)
653+
}
647654
},
648655
}
649656
;(globalThis as any).__rolldown_runtime__ ??= new ViteDevRuntime(

0 commit comments

Comments
 (0)