Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vite-node): vite hmr support (#1516)
* chore: update * feat: vite-node hmr * chore: lock.yaml * chore: update * chore: update * chore: update * feat: hmr module * chore: package.json * perf: create ctx only had `__vite_ssr_import_meta__.hot` * chore: update * chore: update * chore: update * chore: update * perf: only install hmr plugin on watch mode * chore: update lock Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
- Loading branch information
Showing
13 changed files
with
452 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { EventEmitter } from 'events' | ||
import type { HMRPayload, Plugin } from 'vite' | ||
|
||
export type EventType = string | symbol | ||
export type Handler<T = unknown> = (event: T) => void | ||
export interface Emitter<Events extends Record<EventType, unknown>> { | ||
on<Key extends keyof Events>(type: Key, handler: Handler<Events[Key]>): void | ||
off<Key extends keyof Events>(type: Key, handler?: Handler<Events[Key]>): void | ||
emit<Key extends keyof Events>(type: Key, event: Events[Key]): void | ||
emit<Key extends keyof Events>(type: undefined extends Events[Key] ? Key : never): void | ||
} | ||
|
||
export type HMREmitter = Emitter<{ | ||
'message': HMRPayload | ||
}> & EventEmitter | ||
|
||
declare module 'vite' { | ||
interface ViteDevServer { | ||
emitter: HMREmitter | ||
} | ||
} | ||
|
||
export function createHmrEmitter(): HMREmitter { | ||
const emitter = new EventEmitter() | ||
return emitter | ||
} | ||
|
||
export function viteNodeHmrPlugin(): Plugin { | ||
const emitter = createHmrEmitter() | ||
return { | ||
name: 'vite-node:hmr', | ||
|
||
configureServer(server) { | ||
const _send = server.ws.send | ||
server.emitter = emitter | ||
server.ws.send = function (payload: HMRPayload) { | ||
_send(payload) | ||
emitter.emit('message', payload) | ||
} | ||
}, | ||
} | ||
} |
Oops, something went wrong.