Skip to content

Commit 8872aba

Browse files
authored
refactor(hmr): simplify fetchUpdate (#9881)
1 parent 2d2f2e5 commit 8872aba

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

packages/vite/src/client/client.ts

+20-37
Original file line numberDiff line numberDiff line change
@@ -400,46 +400,29 @@ async function fetchUpdate({ path, acceptedPath, timestamp }: Update) {
400400
const moduleMap = new Map<string, ModuleNamespace>()
401401
const isSelfUpdate = path === acceptedPath
402402

403-
// make sure we only import each dep once
404-
const modulesToUpdate = new Set<string>()
405-
if (isSelfUpdate) {
406-
// self update - only update self
407-
modulesToUpdate.add(path)
408-
} else {
409-
// dep update
410-
for (const { deps } of mod.callbacks) {
411-
deps.forEach((dep) => {
412-
if (acceptedPath === dep) {
413-
modulesToUpdate.add(dep)
414-
}
415-
})
416-
}
417-
}
418-
419403
// determine the qualified callbacks before we re-import the modules
420-
const qualifiedCallbacks = mod.callbacks.filter(({ deps }) => {
421-
return deps.some((dep) => modulesToUpdate.has(dep))
422-
})
423-
424-
await Promise.all(
425-
Array.from(modulesToUpdate).map(async (dep) => {
426-
const disposer = disposeMap.get(dep)
427-
if (disposer) await disposer(dataMap.get(dep))
428-
const [path, query] = dep.split(`?`)
429-
try {
430-
const newMod: ModuleNamespace = await import(
431-
/* @vite-ignore */
432-
base +
433-
path.slice(1) +
434-
`?import&t=${timestamp}${query ? `&${query}` : ''}`
435-
)
436-
moduleMap.set(dep, newMod)
437-
} catch (e) {
438-
warnFailedFetch(e, dep)
439-
}
440-
})
404+
const qualifiedCallbacks = mod.callbacks.filter(({ deps }) =>
405+
deps.includes(acceptedPath)
441406
)
442407

408+
if (isSelfUpdate || qualifiedCallbacks.length > 0) {
409+
const dep = acceptedPath
410+
const disposer = disposeMap.get(dep)
411+
if (disposer) await disposer(dataMap.get(dep))
412+
const [path, query] = dep.split(`?`)
413+
try {
414+
const newMod: ModuleNamespace = await import(
415+
/* @vite-ignore */
416+
base +
417+
path.slice(1) +
418+
`?import&t=${timestamp}${query ? `&${query}` : ''}`
419+
)
420+
moduleMap.set(dep, newMod)
421+
} catch (e) {
422+
warnFailedFetch(e, dep)
423+
}
424+
}
425+
443426
return () => {
444427
for (const { deps, fn } of qualifiedCallbacks) {
445428
fn(deps.map((dep) => moduleMap.get(dep)))

0 commit comments

Comments
 (0)