From 64ddff03f7ff693e8a9ed1217d401b628a940852 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Sun, 27 Mar 2022 00:23:30 +0800 Subject: [PATCH] refactor(types): share hot context type (#7475) --- packages/vite/src/client/client.ts | 16 +++++------ packages/vite/types/hot.d.ts | 43 +++++++++++++++++++++++++++++ packages/vite/types/importMeta.d.ts | 43 +---------------------------- 3 files changed, 52 insertions(+), 50 deletions(-) create mode 100644 packages/vite/types/hot.d.ts diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 40f0bb0418f365..1fe461f5b04caf 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -6,6 +6,7 @@ import type { Update, UpdatePayload } from 'types/hmrPayload' +import type { ViteHotContext } from 'types/hot' import type { CustomEventName } from 'types/customEvent' import { ErrorOverlay, overlayId } from './overlay' // eslint-disable-next-line node/no-missing-import @@ -391,9 +392,7 @@ const ctxToListenersMap = new Map< Map void)[]> >() -// Just infer the return type for now -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export const createHotContext = (ownerPath: string) => { +export function createHotContext(ownerPath: string): ViteHotContext { if (!dataMap.has(ownerPath)) { dataMap.set(ownerPath, {}) } @@ -434,12 +433,12 @@ export const createHotContext = (ownerPath: string) => { hotModulesMap.set(ownerPath, mod) } - const hot = { + const hot: ViteHotContext = { get data() { return dataMap.get(ownerPath) }, - accept(deps: any, callback?: any) { + accept(deps?: any, callback?: any) { if (typeof deps === 'function' || !deps) { // self-accept: hot.accept(() => {}) acceptDeps([ownerPath], ([mod]) => deps && deps(mod)) @@ -460,10 +459,11 @@ export const createHotContext = (ownerPath: string) => { ) }, - dispose(cb: (data: any) => void) { + dispose(cb) { disposeMap.set(ownerPath, cb) }, + // @ts-expect-error untyped prune(cb: (data: any) => void) { pruneMap.set(ownerPath, cb) }, @@ -479,7 +479,7 @@ export const createHotContext = (ownerPath: string) => { }, // custom events - on: (event: string, cb: (data: any) => void) => { + on(event, cb) { const addToMap = (map: Map) => { const existing = map.get(event) || [] existing.push(cb) @@ -489,7 +489,7 @@ export const createHotContext = (ownerPath: string) => { addToMap(newListeners) }, - send: (event: string, data?: any) => { + send(event, data) { messageBuffer.push(JSON.stringify({ type: 'custom', event, data })) sendMessageBuffer() } diff --git a/packages/vite/types/hot.d.ts b/packages/vite/types/hot.d.ts new file mode 100644 index 00000000000000..ee7c660056d086 --- /dev/null +++ b/packages/vite/types/hot.d.ts @@ -0,0 +1,43 @@ +import type { + ErrorPayload, + FullReloadPayload, + PrunePayload, + UpdatePayload +} from './hmrPayload' + +export interface ViteHotContext { + readonly data: any + + accept(): void + accept(cb: (mod: any) => void): void + accept(dep: string, cb: (mod: any) => void): void + accept(deps: readonly string[], cb: (mods: any[]) => void): void + + /** + * @deprecated + */ + acceptDeps(): never + + dispose(cb: (data: any) => void): void + decline(): void + invalidate(): void + + on: { + (event: 'vite:beforeUpdate', cb: (payload: UpdatePayload) => void): void + (event: 'vite:beforePrune', cb: (payload: PrunePayload) => void): void + ( + event: 'vite:beforeFullReload', + cb: (payload: FullReloadPayload) => void + ): void + (event: 'vite:error', cb: (payload: ErrorPayload) => void): void + (event: string, cb: (data: any) => void): void + } + + send(event: string, data?: any): void +} + +// See https://stackoverflow.com/a/63549561. +export type CustomEventName = (T extends `vite:${T}` + ? never + : T) & + (`vite:${T}` extends T ? never : T) diff --git a/packages/vite/types/importMeta.d.ts b/packages/vite/types/importMeta.d.ts index 1fd39b993d5142..9b57fd120a7ba9 100644 --- a/packages/vite/types/importMeta.d.ts +++ b/packages/vite/types/importMeta.d.ts @@ -20,48 +20,7 @@ interface GlobOptions { interface ImportMeta { url: string - readonly hot?: { - readonly data: any - - accept(): void - accept(cb: (mod: any) => void): void - accept(dep: string, cb: (mod: any) => void): void - accept(deps: readonly string[], cb: (mods: any[]) => void): void - - /** - * @deprecated - */ - acceptDeps(): never - - dispose(cb: (data: any) => void): void - decline(): void - invalidate(): void - - on: { - ( - event: 'vite:beforeUpdate', - cb: (payload: import('./hmrPayload').UpdatePayload) => void - ): void - ( - event: 'vite:beforePrune', - cb: (payload: import('./hmrPayload').PrunePayload) => void - ): void - ( - event: 'vite:beforeFullReload', - cb: (payload: import('./hmrPayload').FullReloadPayload) => void - ): void - ( - event: 'vite:error', - cb: (payload: import('./hmrPayload').ErrorPayload) => void - ): void - ( - event: import('./customEvent').CustomEventName, - cb: (data: any) => void - ): void - } - - send(event: string, data?: any): void - } + readonly hot?: import('./hot').ViteHotContext readonly env: ImportMetaEnv