Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(client): simplify fetchUpdate code #11004

Merged
merged 5 commits into from Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/guide/api-hmr.md
Expand Up @@ -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
}
)
}
Expand Down
26 changes: 12 additions & 14 deletions packages/vite/src/client/client.ts
Expand Up @@ -414,7 +414,7 @@ async function fetchUpdate({
return
}

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

// determine the qualified callbacks before we re-import the modules
Expand All @@ -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}`)
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions packages/vite/types/hot.d.ts
Expand Up @@ -15,10 +15,9 @@ export interface ViteHotContext {
cb: (mods: Array<ModuleNamespace | undefined>) => 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
Expand Down