From 067b117f83b69372b2851684cbb9025969c9d656 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:38:47 +0900 Subject: [PATCH 1/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 0ea5af710c0518..a72523b3459b41 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -400,19 +400,14 @@ 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() + let modulesToUpdate: Set if (isSelfUpdate) { - // self update - only update self - modulesToUpdate.add(path) + modulesToUpdate = new Set([acceptedPath]) } else { - // dep update - for (const { deps } of mod.callbacks) { - deps.forEach((dep) => { - if (acceptedPath === dep) { - modulesToUpdate.add(dep) - } - }) + if (mod.callbacks.some(({ deps }) => deps.includes(acceptedPath))) { + modulesToUpdate = new Set([acceptedPath]) + } else { + modulesToUpdate = new Set() } } From c47364ef611a7a4cdcad83fed35ffc9210a7548d Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:52:11 +0900 Subject: [PATCH 2/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 45 ++++++++++++++---------------- 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index a72523b3459b41..439256884ef376 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -400,40 +400,37 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { const moduleMap = new Map() const isSelfUpdate = path === acceptedPath - let modulesToUpdate: Set + let moduleToUpdate: string | undefined if (isSelfUpdate) { - modulesToUpdate = new Set([acceptedPath]) + moduleToUpdate = acceptedPath } else { if (mod.callbacks.some(({ deps }) => deps.includes(acceptedPath))) { - modulesToUpdate = new Set([acceptedPath]) - } else { - modulesToUpdate = new Set() + moduleToUpdate = acceptedPath } } // determine the qualified callbacks before we re-import the modules const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => { - return deps.some((dep) => modulesToUpdate.has(dep)) + return moduleToUpdate !== undefined && deps.includes(moduleToUpdate) }) - 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 (moduleToUpdate !== undefined) { + const dep = moduleToUpdate + 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) + } + } return () => { for (const { deps, fn } of qualifiedCallbacks) { From e5c95e8aa4119413db459336374df8dea9e9a027 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:55:01 +0900 Subject: [PATCH 3/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 439256884ef376..477958ecd7b452 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -410,9 +410,12 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { } // determine the qualified callbacks before we re-import the modules - const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => { - return moduleToUpdate !== undefined && deps.includes(moduleToUpdate) - }) + let qualifiedCallbacks: HotCallback[] = [] + if (moduleToUpdate !== undefined) { + qualifiedCallbacks = mod.callbacks.filter(({ deps }) => + deps.includes(moduleToUpdate!) + ) + } if (moduleToUpdate !== undefined) { const dep = moduleToUpdate From dceb9a55eb9943a14937443d23f5e35633dd1674 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:57:08 +0900 Subject: [PATCH 4/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 477958ecd7b452..97a7cc065bb379 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -400,23 +400,23 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { const moduleMap = new Map() const isSelfUpdate = path === acceptedPath + // determine the qualified callbacks before we re-import the modules + let qualifiedCallbacks: HotCallback[] = [] let moduleToUpdate: string | undefined + if (isSelfUpdate) { moduleToUpdate = acceptedPath + qualifiedCallbacks = mod.callbacks.filter(({ deps }) => + deps.includes(acceptedPath) + ) } else { - if (mod.callbacks.some(({ deps }) => deps.includes(acceptedPath))) { + const tmp = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath)) + if (tmp.length > 0) { moduleToUpdate = acceptedPath + qualifiedCallbacks = tmp } } - // determine the qualified callbacks before we re-import the modules - let qualifiedCallbacks: HotCallback[] = [] - if (moduleToUpdate !== undefined) { - qualifiedCallbacks = mod.callbacks.filter(({ deps }) => - deps.includes(moduleToUpdate!) - ) - } - if (moduleToUpdate !== undefined) { const dep = moduleToUpdate const disposer = disposeMap.get(dep) From 29ae0e58663c59951d2a763a13ff19fcce7960e9 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:58:49 +0900 Subject: [PATCH 5/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 97a7cc065bb379..90cfcc6a5b76bf 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -404,16 +404,16 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { let qualifiedCallbacks: HotCallback[] = [] let moduleToUpdate: string | undefined + const tmp = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath)) if (isSelfUpdate) { moduleToUpdate = acceptedPath - qualifiedCallbacks = mod.callbacks.filter(({ deps }) => - deps.includes(acceptedPath) - ) + qualifiedCallbacks = tmp } else { - const tmp = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath)) if (tmp.length > 0) { moduleToUpdate = acceptedPath qualifiedCallbacks = tmp + } else { + qualifiedCallbacks = tmp } } From 1fc0a6647459424fd238e497c39ebf901526ef04 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 21:59:56 +0900 Subject: [PATCH 6/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 90cfcc6a5b76bf..50ee0d703972cb 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -405,17 +405,8 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { let moduleToUpdate: string | undefined const tmp = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath)) - if (isSelfUpdate) { - moduleToUpdate = acceptedPath - qualifiedCallbacks = tmp - } else { - if (tmp.length > 0) { - moduleToUpdate = acceptedPath - qualifiedCallbacks = tmp - } else { - qualifiedCallbacks = tmp - } - } + moduleToUpdate = isSelfUpdate || tmp.length > 0 ? acceptedPath : undefined + qualifiedCallbacks = tmp if (moduleToUpdate !== undefined) { const dep = moduleToUpdate From 18f0ea9eb822f23a7b4d2d915005e21a6b3f3df4 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 22:00:41 +0900 Subject: [PATCH 7/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index 50ee0d703972cb..b94fa5bc68d177 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -401,12 +401,12 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { const isSelfUpdate = path === acceptedPath // determine the qualified callbacks before we re-import the modules - let qualifiedCallbacks: HotCallback[] = [] - let moduleToUpdate: string | undefined + const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => + deps.includes(acceptedPath) + ) - const tmp = mod.callbacks.filter(({ deps }) => deps.includes(acceptedPath)) - moduleToUpdate = isSelfUpdate || tmp.length > 0 ? acceptedPath : undefined - qualifiedCallbacks = tmp + const moduleToUpdate = + isSelfUpdate || qualifiedCallbacks.length > 0 ? acceptedPath : undefined if (moduleToUpdate !== undefined) { const dep = moduleToUpdate From 32f3168831c57a03192d339b2d5819a98f589464 Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 28 Aug 2022 22:01:10 +0900 Subject: [PATCH 8/8] refactor(hmr): simplify fetchUpdate --- packages/vite/src/client/client.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/client/client.ts b/packages/vite/src/client/client.ts index b94fa5bc68d177..fdd28e6be42278 100644 --- a/packages/vite/src/client/client.ts +++ b/packages/vite/src/client/client.ts @@ -405,11 +405,8 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) { deps.includes(acceptedPath) ) - const moduleToUpdate = - isSelfUpdate || qualifiedCallbacks.length > 0 ? acceptedPath : undefined - - if (moduleToUpdate !== undefined) { - const dep = moduleToUpdate + 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(`?`)