1 line
50 KiB
Text
1 line
50 KiB
Text
|
{"version":3,"file":"client.mjs","sources":["hmr.ts","overlay.ts","client.ts"],"sourcesContent":["import type { Update } from 'types/hmrPayload'\nimport type { ModuleNamespace, ViteHotContext } from 'types/hot'\nimport type { InferCustomEventPayload } from 'types/customEvent'\n\ntype CustomListenersMap = Map<string, ((data: any) => void)[]>\n\ninterface HotModule {\n id: string\n callbacks: HotCallback[]\n}\n\ninterface HotCallback {\n // the dependencies must be fetchable paths\n deps: string[]\n fn: (modules: Array<ModuleNamespace | undefined>) => void\n}\n\ninterface Connection {\n addBuffer(message: string): void\n send(): unknown\n}\n\nexport class HMRContext implements ViteHotContext {\n private newListeners: CustomListenersMap\n\n constructor(\n private ownerPath: string,\n private hmrClient: HMRClient,\n private connection: Connection,\n ) {\n if (!hmrClient.dataMap.has(ownerPath)) {\n hmrClient.dataMap.set(ownerPath, {})\n }\n\n // when a file is hot updated, a new context is created\n // clear its stale callbacks\n const mod = hmrClient.hotModulesMap.get(ownerPath)\n if (mod) {\n mod.callbacks = []\n }\n\n // clear stale custom event listeners\n const staleListeners = hmrClient.ctxToListenersMap.get(ownerPath)\n if (staleListeners) {\n for (const [event, staleFns] of staleListeners) {\n const listeners = hmrClient.customListenersMap.get(event)\n if (listeners) {\n hmrClient.customListenersMap.set(\n event,\n listeners.filter((l) => !staleFns.includes(l)),\n )\n }\n }\n }\n\n this.newListeners = new Map()\n hmrClient.ctxToListenersMap.set(ownerPath, this.newListeners)\n }\n\n get data(): any {\n return this.hmrClient.dataMap.get(this.ownerPath)\n }\n\n accept(deps?: any, callback?: any): void {\n if (typeof deps === 'function' || !deps) {\n // self-accept: hot.accept(() => {})\n this.acceptDeps([this.ownerPath], ([mod]) => deps?.(mod))\n } else if (typeof deps === 'string') {\n // explicit deps\n this.acceptDeps([deps], ([mod]) => callback?.(mod))\n } else if (Array.isArray(deps)) {\n this.acceptDeps(deps, callback)\n } else {\n throw new Error(`invalid hot.accept() usage.`)\n }\n }\n\n // export names (first arg) are irrelevant on the client side, they're\n // extracted in the server for propagation\n acceptExports(\n _: string | readonly string[],\n callback: (data: any) => void,\n ): void {\n this.acceptDeps([this.ownerPath], ([mod]) => callback?.(mod))\n }\n\n dispose(cb: (data: any) => void): void {\n this.hmrClient.disposeMap.set(this.ownerPath, cb)\n }\n\n prune(cb: (data: any) => void): void {\n this.hmrClient.pruneMap.set(this.ownerPath, cb)\n }\n\n // Kept for backward compatibility (#11036)\n // eslint-disable-next-line @typescript-eslint/no-empty-function\n decline(): void {}\n\n invalidate(message: string): void {\n this.hmrClient.notifyListeners('vite:invalidate', {\n path: this.ownerPath,\n message,\n })\n this.send('vite:invalidate', { path: this.ownerPath, message })\n this.hmrClient.logger.debug(\n `[vite] invalidate ${this.ownerPath}${message ? `: ${message}` : ''}`,\n )\n }\n\n on<T extends string>(\n event: T,\n cb: (payload: InferCustomEventPayload<T>) => void,\n ): void {\n const addToMap = (map: Map<string, any[]>) => {\n const existing = map.get(event) || []\n existing.push(cb)\n map.set(event, existing)\n }\n addToMap(this.hmrClient.customListenersMap)\n addToMap(this.newListeners)\n }\n\n off<T extends string>(\n event: T,\n cb: (payload: InferCustomEventPayload<T>) => void,\n ): void {\n const removeFromMap = (map: Map<string, any[]>) => {\n const existing = map.get(event)\n if (existing === undefined) {\n return\n }\n const pruned = existing.filter((l) => l !== cb)\n if (pruned.length === 0) {\n map.delete(event)\n return\n }\n map.set
|