Skip to content

Commit

Permalink
fix(hmr): fix hmr invalidation on circular deps
Browse files Browse the repository at this point in the history
fix #1477
  • Loading branch information
yyx990803 committed Jan 19, 2021
1 parent 4176bd6 commit ca8442c
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions packages/vite/src/node/server/hmr.ts
Expand Up @@ -104,12 +104,14 @@ export async function handleHMRUpdate(
}

const updates: Update[] = []
const invalidatedModules = new Set<ModuleNode>()

for (const mod of hmrContext.modules) {
const boundaries = new Set<{
boundary: ModuleNode
acceptedVia: ModuleNode
}>()
invalidate(mod, timestamp, invalidatedModules)
const hasDeadEnd = propagateUpdate(mod, timestamp, boundaries)
if (hasDeadEnd) {
config.logger.info(chalk.green(`page reload `) + chalk.dim(shortFile), {
Expand Down Expand Up @@ -159,10 +161,6 @@ function propagateUpdate(
boundary: node,
acceptedVia: node
})
// mark current propagation chain dirty.
// timestamp is used for injecting timestamp query during rewrite
// also invalidate cache
invalidateChain(currentChain, timestamp)
return false
}

Expand All @@ -177,24 +175,29 @@ function propagateUpdate(
boundary: importer,
acceptedVia: node
})
invalidateChain(subChain, timestamp)
continue
}

if (!currentChain.includes(importer)) {
if (propagateUpdate(importer, timestamp, boundaries, subChain)) {
return true
}
if (currentChain.includes(importer)) {
// circular deps is considered dead end
return true
}

if (propagateUpdate(importer, timestamp, boundaries, subChain)) {
return true
}
}
return false
}

function invalidateChain(chain: ModuleNode[], timestamp: number) {
chain.forEach((node) => {
node.lastHMRTimestamp = timestamp
node.transformResult = null
})
function invalidate(mod: ModuleNode, timestamp: number, seen: Set<ModuleNode>) {
if (seen.has(mod)) {
return
}
seen.add(mod)
mod.lastHMRTimestamp = timestamp
mod.transformResult = null
mod.importers.forEach((importer) => invalidate(importer, timestamp, seen))
}

export function handlePrunedModules(
Expand Down

0 comments on commit ca8442c

Please sign in to comment.