Skip to content

Commit

Permalink
feat(hmr): add full reload reason (#14914)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre authored Nov 10, 2023
1 parent 2f39547 commit 60a020e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
46 changes: 23 additions & 23 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export async function handleHMRUpdate(
updateModules(shortFile, hmrContext.modules, timestamp, server)
}

type HasDeadEnd = boolean | string
export function updateModules(
file: string,
modules: ModuleNode[],
Expand All @@ -152,7 +153,7 @@ export function updateModules(
const updates: Update[] = []
const invalidatedModules = new Set<ModuleNode>()
const traversedModules = new Set<ModuleNode>()
let needFullReload = false
let needFullReload: HasDeadEnd = false

for (const mod of modules) {
const boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[] = []
Expand All @@ -165,7 +166,7 @@ export function updateModules(
}

if (hasDeadEnd) {
needFullReload = true
needFullReload = hasDeadEnd
continue
}

Expand All @@ -184,10 +185,14 @@ export function updateModules(
}

if (needFullReload) {
config.logger.info(colors.green(`page reload `) + colors.dim(file), {
clear: !afterInvalidation,
timestamp: true,
})
const reason =
typeof needFullReload === 'string'
? colors.dim(` (${needFullReload})`)
: ''
config.logger.info(
colors.green(`page reload `) + colors.dim(file) + reason,
{ clear: !afterInvalidation, timestamp: true },
)
ws.send({
type: 'full-reload',
})
Expand Down Expand Up @@ -254,7 +259,7 @@ function propagateUpdate(
traversedModules: Set<ModuleNode>,
boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[],
currentChain: ModuleNode[] = [node],
): boolean /* hasDeadEnd */ {
): HasDeadEnd {
if (traversedModules.has(node)) {
return false
}
Expand All @@ -274,9 +279,8 @@ function propagateUpdate(

if (node.isSelfAccepting) {
boundaries.push({ boundary: node, acceptedVia: node })
if (isNodeWithinCircularImports(node, currentChain)) {
return true
}
const result = isNodeWithinCircularImports(node, currentChain)
if (result) return result

// additionally check for CSS importers, since a PostCSS plugin like
// Tailwind JIT may register any file as a dependency to a CSS file.
Expand All @@ -301,9 +305,8 @@ function propagateUpdate(
// so that they do get the fresh imported module when/if they are reloaded.
if (node.acceptedHmrExports) {
boundaries.push({ boundary: node, acceptedVia: node })
if (isNodeWithinCircularImports(node, currentChain)) {
return true
}
const result = isNodeWithinCircularImports(node, currentChain)
if (result) return result
} else {
if (!node.importers.size) {
return true
Expand All @@ -325,9 +328,8 @@ function propagateUpdate(

if (importer.acceptedHmrDeps.has(node)) {
boundaries.push({ boundary: importer, acceptedVia: node })
if (isNodeWithinCircularImports(importer, subChain)) {
return true
}
const result = isNodeWithinCircularImports(importer, subChain)
if (result) return result
continue
}

Expand Down Expand Up @@ -364,7 +366,7 @@ function isNodeWithinCircularImports(
node: ModuleNode,
nodeChain: ModuleNode[],
currentChain: ModuleNode[] = [node],
) {
): HasDeadEnd {
// To help visualize how each parameters work, imagine this import graph:
//
// A -> B -> C -> ACCEPTED -> D -> E -> NODE
Expand Down Expand Up @@ -405,19 +407,17 @@ function isNodeWithinCircularImports(
importChain.map((m) => colors.dim(m.url)).join(' -> '),
)
}
return true
return 'circular imports'
}

// Continue recursively
if (
!currentChain.includes(importer) &&
isNodeWithinCircularImports(
if (!currentChain.includes(importer)) {
const result = isNodeWithinCircularImports(
importer,
nodeChain,
currentChain.concat(importer),
)
) {
return true
if (result) return result
}
}
return false
Expand Down
5 changes: 5 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
isBuild,
page,
removeFile,
serverLogs,
untilBrowserLogAfter,
untilUpdated,
viteTestUrl,
Expand Down Expand Up @@ -881,6 +882,10 @@ if (import.meta.hot) {
() => page.textContent('.self-accept-within-circular'),
'cc',
)
expect(serverLogs.length).greaterThanOrEqual(1)
// Match on full log not possible because of color markers
expect(serverLogs.at(-1)!).toContain('page reload')
expect(serverLogs.at(-1)!).toContain('(circular imports)')
})

test('hmr should not reload if no accepted within circular imported files', async () => {
Expand Down

0 comments on commit 60a020e

Please sign in to comment.