From 018e52fe3febf2cd5bd72256bed5cea31622f770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arnaud=20Barr=C3=A9?= Date: Tue, 22 Nov 2022 19:40:28 +0100 Subject: [PATCH] refactor(client): simplify fetchUpdate code (#11004) --- docs/guide/api-hmr.md | 3 ++- packages/vite/src/client/client.ts | 26 ++++++++++++-------------- packages/vite/types/hot.d.ts | 3 +-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/docs/guide/api-hmr.md b/docs/guide/api-hmr.md index 0704b0cc1cfa71..cfca9780e61d61 100644 --- a/docs/guide/api-hmr.md +++ b/docs/guide/api-hmr.md @@ -94,7 +94,8 @@ if (import.meta.hot) { import.meta.hot.accept( ['./foo.js', './bar.js'], ([newFooModule, newBarModule]) => { - // the callback receives the updated modules in an Array + // The callback receives an array where only the updated module is non null + // If the update was not succeful (syntax error for ex.), the array is empty } ) } diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index bfa0c44231cada..18ee738f36d4e1 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -414,7 +414,7 @@ async function fetchUpdate({ return } - const moduleMap = new Map() + let fetchedModule: ModuleNamespace | undefined const isSelfUpdate = path === acceptedPath // determine the qualified callbacks before we re-import the modules @@ -423,28 +423,26 @@ async function fetchUpdate({ ) if (isSelfUpdate || qualifiedCallbacks.length > 0) { - const dep = acceptedPath - const disposer = disposeMap.get(dep) - if (disposer) await disposer(dataMap.get(dep)) - const [path, query] = dep.split(`?`) + const disposer = disposeMap.get(acceptedPath) + if (disposer) await disposer(dataMap.get(acceptedPath)) + const [acceptedPathWithoutQuery, query] = acceptedPath.split(`?`) try { - const newMod: ModuleNamespace = await import( + fetchedModule = await import( /* @vite-ignore */ base + - path.slice(1) + + acceptedPathWithoutQuery.slice(1) + `?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${ query ? `&${query}` : '' }` ) - moduleMap.set(dep, newMod) } catch (e) { - warnFailedFetch(e, dep) + warnFailedFetch(e, acceptedPath) } } return () => { for (const { deps, fn } of qualifiedCallbacks) { - fn(deps.map((dep) => moduleMap.get(dep))) + fn(deps.map((dep) => (dep === acceptedPath ? fetchedModule : undefined))) } const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}` console.debug(`[vite] hot updated: ${loggedPath}`) @@ -527,10 +525,10 @@ export function createHotContext(ownerPath: string): ViteHotContext { accept(deps?: any, callback?: any) { if (typeof deps === 'function' || !deps) { // self-accept: hot.accept(() => {}) - acceptDeps([ownerPath], ([mod]) => deps && deps(mod)) + acceptDeps([ownerPath], ([mod]) => deps?.(mod)) } else if (typeof deps === 'string') { // explicit deps - acceptDeps([deps], ([mod]) => callback && callback(mod)) + acceptDeps([deps], ([mod]) => callback?.(mod)) } else if (Array.isArray(deps)) { acceptDeps(deps, callback) } else { @@ -540,8 +538,8 @@ export function createHotContext(ownerPath: string): ViteHotContext { // export names (first arg) are irrelevant on the client side, they're // extracted in the server for propagation - acceptExports(_: string | readonly string[], callback?: any) { - acceptDeps([ownerPath], callback && (([mod]) => callback(mod))) + acceptExports(_, callback) { + acceptDeps([ownerPath], ([mod]) => callback?.(mod)) }, dispose(cb) { diff --git a/packages/vite/types/hot.d.ts b/packages/vite/types/hot.d.ts index 1a67a9087a6e68..76d054c3125651 100644 --- a/packages/vite/types/hot.d.ts +++ b/packages/vite/types/hot.d.ts @@ -15,10 +15,9 @@ export interface ViteHotContext { cb: (mods: Array) => void ): void - acceptExports(exportNames: string | readonly string[]): void acceptExports( exportNames: string | readonly string[], - cb: (mod: ModuleNamespace | undefined) => void + cb?: (mod: ModuleNamespace | undefined) => void ): void dispose(cb: (data: any) => void): void