Skip to content

Commit

Permalink
wip: more precise hmr callback triggering
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Dec 15, 2020
1 parent 18deaa0 commit b958f2c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
10 changes: 7 additions & 3 deletions packages/vite/src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ export function removeStyle(id: string) {
}
}

async function fetchUpdate({ path, changedPath, timestamp }: Update) {
async function fetchUpdate({ path, accpetedPath, timestamp }: Update) {
const mod = hotModulesMap.get(path)
if (!mod) {
// In a code-spliting project,
Expand All @@ -269,7 +269,7 @@ async function fetchUpdate({ path, changedPath, timestamp }: Update) {
}

const moduleMap = new Map()
const isSelfUpdate = path === changedPath
const isSelfUpdate = path === accpetedPath

// make sure we only import each dep once
const modulesToUpdate = new Set<string>()
Expand All @@ -279,7 +279,11 @@ async function fetchUpdate({ path, changedPath, timestamp }: Update) {
} else {
// dep update
for (const { deps } of mod.callbacks) {
deps.forEach((dep) => modulesToUpdate.add(dep))
deps.forEach((dep) => {
if (accpetedPath.startsWith(dep)) {
modulesToUpdate.add(dep)
}
})
}
}

Expand Down
24 changes: 18 additions & 6 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ export async function handleHMRUpdate(
const updates: Update[] = []

for (const mod of filteredMods) {
const boundaries = new Set<ModuleNode>()
const boundaries = new Set<{
boundary: ModuleNode
acceptedVia: ModuleNode
}>()
const hasDeadEnd = propagateUpdate(mod, timestamp, boundaries)
if (hasDeadEnd) {
debugHmr(`[full reload] ${chalk.dim(file)}`)
Expand All @@ -79,14 +82,14 @@ export async function handleHMRUpdate(
}

updates.push(
...[...boundaries].map((boundary) => {
...[...boundaries].map(({ boundary, acceptedVia }) => {
const type = `${boundary.type}-update` as Update['type']
debugHmr(`[${type}] ${chalk.dim(boundary.url)}`)
return {
type,
timestamp,
path: boundary.url,
changedPath: mod.url
accpetedPath: acceptedVia.url
}
})
)
Expand Down Expand Up @@ -119,11 +122,17 @@ export function handlePrunedModules(
function propagateUpdate(
node: ModuleNode,
timestamp: number,
boundaries: Set<ModuleNode>,
boundaries: Set<{
boundary: ModuleNode
acceptedVia: ModuleNode
}>,
currentChain: ModuleNode[] = [node]
): boolean /* hasDeadEnd */ {
if (node.isSelfAccepting) {
boundaries.add(node)
boundaries.add({
boundary: node,
acceptedVia: node
})
// mark current propagation chain dirty.
// timestamp is used for injecting timestamp query during rewrite
// also invalidate cache
Expand All @@ -138,7 +147,10 @@ function propagateUpdate(
for (const importer of node.importers) {
const subChain = currentChain.concat(importer)
if (importer.acceptedHmrDeps.has(node)) {
boundaries.add(importer)
boundaries.add({
boundary: importer,
acceptedVia: node
})
invalidateChain(subChain, timestamp)
continue
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/types/hmrPayload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export interface UpdatePayload {
export interface Update {
type: 'js-update' | 'css-update'
path: string
changedPath: string
accpetedPath: string
timestamp: number
}

Expand Down

0 comments on commit b958f2c

Please sign in to comment.