Skip to content

Commit 018e52f

Browse files
ArnaudBarreClay Smith
authored and
Clay Smith
committed
refactor(client): simplify fetchUpdate code (vitejs#11004)
1 parent 4968e29 commit 018e52f

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

docs/guide/api-hmr.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ if (import.meta.hot) {
9494
import.meta.hot.accept(
9595
['./foo.js', './bar.js'],
9696
([newFooModule, newBarModule]) => {
97-
// the callback receives the updated modules in an Array
97+
// The callback receives an array where only the updated module is non null
98+
// If the update was not succeful (syntax error for ex.), the array is empty
9899
}
99100
)
100101
}

packages/vite/src/client/client.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ async function fetchUpdate({
414414
return
415415
}
416416

417-
const moduleMap = new Map<string, ModuleNamespace>()
417+
let fetchedModule: ModuleNamespace | undefined
418418
const isSelfUpdate = path === acceptedPath
419419

420420
// determine the qualified callbacks before we re-import the modules
@@ -423,28 +423,26 @@ async function fetchUpdate({
423423
)
424424

425425
if (isSelfUpdate || qualifiedCallbacks.length > 0) {
426-
const dep = acceptedPath
427-
const disposer = disposeMap.get(dep)
428-
if (disposer) await disposer(dataMap.get(dep))
429-
const [path, query] = dep.split(`?`)
426+
const disposer = disposeMap.get(acceptedPath)
427+
if (disposer) await disposer(dataMap.get(acceptedPath))
428+
const [acceptedPathWithoutQuery, query] = acceptedPath.split(`?`)
430429
try {
431-
const newMod: ModuleNamespace = await import(
430+
fetchedModule = await import(
432431
/* @vite-ignore */
433432
base +
434-
path.slice(1) +
433+
acceptedPathWithoutQuery.slice(1) +
435434
`?${explicitImportRequired ? 'import&' : ''}t=${timestamp}${
436435
query ? `&${query}` : ''
437436
}`
438437
)
439-
moduleMap.set(dep, newMod)
440438
} catch (e) {
441-
warnFailedFetch(e, dep)
439+
warnFailedFetch(e, acceptedPath)
442440
}
443441
}
444442

445443
return () => {
446444
for (const { deps, fn } of qualifiedCallbacks) {
447-
fn(deps.map((dep) => moduleMap.get(dep)))
445+
fn(deps.map((dep) => (dep === acceptedPath ? fetchedModule : undefined)))
448446
}
449447
const loggedPath = isSelfUpdate ? path : `${acceptedPath} via ${path}`
450448
console.debug(`[vite] hot updated: ${loggedPath}`)
@@ -527,10 +525,10 @@ export function createHotContext(ownerPath: string): ViteHotContext {
527525
accept(deps?: any, callback?: any) {
528526
if (typeof deps === 'function' || !deps) {
529527
// self-accept: hot.accept(() => {})
530-
acceptDeps([ownerPath], ([mod]) => deps && deps(mod))
528+
acceptDeps([ownerPath], ([mod]) => deps?.(mod))
531529
} else if (typeof deps === 'string') {
532530
// explicit deps
533-
acceptDeps([deps], ([mod]) => callback && callback(mod))
531+
acceptDeps([deps], ([mod]) => callback?.(mod))
534532
} else if (Array.isArray(deps)) {
535533
acceptDeps(deps, callback)
536534
} else {
@@ -540,8 +538,8 @@ export function createHotContext(ownerPath: string): ViteHotContext {
540538

541539
// export names (first arg) are irrelevant on the client side, they're
542540
// extracted in the server for propagation
543-
acceptExports(_: string | readonly string[], callback?: any) {
544-
acceptDeps([ownerPath], callback && (([mod]) => callback(mod)))
541+
acceptExports(_, callback) {
542+
acceptDeps([ownerPath], ([mod]) => callback?.(mod))
545543
},
546544

547545
dispose(cb) {

packages/vite/types/hot.d.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@ export interface ViteHotContext {
1515
cb: (mods: Array<ModuleNamespace | undefined>) => void
1616
): void
1717

18-
acceptExports(exportNames: string | readonly string[]): void
1918
acceptExports(
2019
exportNames: string | readonly string[],
21-
cb: (mod: ModuleNamespace | undefined) => void
20+
cb?: (mod: ModuleNamespace | undefined) => void
2221
): void
2322

2423
dispose(cb: (data: any) => void): void

0 commit comments

Comments
 (0)