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

fix(hmr): don't bind hmr methods to the context class #15679

Closed
wants to merge 1 commit into from
Closed
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
25 changes: 14 additions & 11 deletions packages/vite/src/shared/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class HMRContext implements ViteHotContext {
return this.hmrClient.dataMap.get(this.ownerPath)
}

accept(deps?: any, callback?: any): void {
accept = (deps?: any, callback?: any): void => {
if (typeof deps === 'function' || !deps) {
// self-accept: hot.accept(() => {})
this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod))
Expand All @@ -87,26 +87,26 @@ export class HMRContext implements ViteHotContext {

// export names (first arg) are irrelevant on the client side, they're
// extracted in the server for propagation
acceptExports(
acceptExports = (
_: string | readonly string[],
callback: (data: any) => void,
): void {
): void => {
this.acceptDeps([this.ownerPath], ([mod]) => callback?.(mod))
}

dispose(cb: (data: any) => void): void {
dispose = (cb: (data: any) => void): void => {
this.hmrClient.disposeMap.set(this.ownerPath, cb)
}

prune(cb: (data: any) => void): void {
prune = (cb: (data: any) => void): void => {
this.hmrClient.pruneMap.set(this.ownerPath, cb)
}

// Kept for backward compatibility (#11036)
// eslint-disable-next-line @typescript-eslint/no-empty-function
decline(): void {}

invalidate(message: string): void {
invalidate = (message: string): void => {
this.hmrClient.notifyListeners('vite:invalidate', {
path: this.ownerPath,
message,
Expand All @@ -117,10 +117,10 @@ export class HMRContext implements ViteHotContext {
)
}

on<T extends string>(
on = <T extends string>(
event: T,
cb: (payload: InferCustomEventPayload<T>) => void,
): void {
): void => {
const addToMap = (map: Map<string, any[]>) => {
const existing = map.get(event) || []
existing.push(cb)
Expand All @@ -130,10 +130,10 @@ export class HMRContext implements ViteHotContext {
addToMap(this.newListeners)
}

off<T extends string>(
off = <T extends string>(
event: T,
cb: (payload: InferCustomEventPayload<T>) => void,
): void {
): void => {
const removeFromMap = (map: Map<string, any[]>) => {
const existing = map.get(event)
if (existing === undefined) {
Expand All @@ -150,7 +150,10 @@ export class HMRContext implements ViteHotContext {
removeFromMap(this.newListeners)
}

send<T extends string>(event: T, data?: InferCustomEventPayload<T>): void {
send = <T extends string>(
event: T,
data?: InferCustomEventPayload<T>,
): void => {
this.hmrClient.messenger.send(
JSON.stringify({ type: 'custom', event, data }),
)
Expand Down
Loading