From 2f3b3d459d831e2b2b46cdf6b9788b56405dae99 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:38:47 +0900 Subject: [PATCH] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 58 +++++++++++------------------- 1 file changed, 20 insertions(+), 38 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 0ea5af710c0518..687f0f9b4c6c52 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -398,54 +398,36 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { } const moduleMap = new Map() - const isSelfUpdate = path === acceptedPath - - // make sure we only import each dep once - const modulesToUpdate = new Set() - if (isSelfUpdate) { - // self update - only update self - modulesToUpdate.add(path) - } else { - // dep update - for (const { deps } of mod.callbacks) { - deps.forEach((dep) => { - if (acceptedPath === dep) { - modulesToUpdate.add(dep) - } - }) - } - } // determine the qualified callbacks before we re-import the modules const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => { - return deps.some((dep) => modulesToUpdate.has(dep)) + return deps.some((dep) => dep === acceptedPath) }) - await Promise.all( - Array.from(modulesToUpdate).map(async (dep) => { - const disposer = disposeMap.get(dep) - if (disposer) await disposer(dataMap.get(dep)) - const [path, query] = dep.split(`?`) - try { - const newMod: ModuleNamespace = await import( - /* @vite-ignore */ - base + - path.slice(1) + - `?import&t=${timestamp}${query ? `&${query}` : ''}` - ) - moduleMap.set(dep, newMod) - } catch (e) { - warnFailedFetch(e, dep) - } - }) - ) + if (qualifiedCallbacks.length > 0) { + const disposer = disposeMap.get(acceptedPath) + if (disposer) await disposer(dataMap.get(acceptedPath)) + const [path, query] = acceptedPath.split(`?`) + try { + const newMod: ModuleNamespace = await import( + /* @vite-ignore */ + base + + path.slice(1) + + `?import&t=${timestamp}${query ? `&${query}` : ''}` + ) + moduleMap.set(acceptedPath, newMod) + } catch (e) { + warnFailedFetch(e, acceptedPath) + } + } return () => { for (const { deps, fn } of qualifiedCallbacks) { fn(deps.map((dep) => moduleMap.get(dep))) } - const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}` - console.debug(`[vite] hot updated: ${loggedPath}`) + const loggedPath = + path === acceptedPath ? path : `${acceptedPath} via ${path}` + console.log(`[vite] hot updated: ${loggedPath}`) } }