Skip to content

Commit

Permalink
fix(hmr): only invalidate lastHMRTimestamp of importers if the inva…
Browse files Browse the repository at this point in the history
…lidated module is not a HMR boundary (#13024)

Co-authored-by: ArnaudBarre <arnaud.barre72@gmail.com>
  • Loading branch information
hyf0 and ArnaudBarre authored Jun 21, 2023
1 parent 097c583 commit 1143e0b
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,21 @@ export function updateModules(
let needFullReload = false

for (const mod of modules) {
moduleGraph.invalidateModule(mod, invalidatedModules, timestamp, true)
const boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[] = []
const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries)

moduleGraph.invalidateModule(
mod,
invalidatedModules,
timestamp,
true,
boundaries.map((b) => b.boundary),
)

if (needFullReload) {
continue
}

const boundaries: { boundary: ModuleNode; acceptedVia: ModuleNode }[] = []
const hasDeadEnd = propagateUpdate(mod, traversedModules, boundaries)
if (hasDeadEnd) {
needFullReload = true
continue
Expand Down
6 changes: 6 additions & 0 deletions packages/vite/src/node/server/moduleGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export class ModuleGraph {
seen: Set<ModuleNode> = new Set(),
timestamp: number = Date.now(),
isHmr: boolean = false,
hmrBoundaries: ModuleNode[] = [],
): void {
if (seen.has(mod)) {
return
Expand All @@ -148,6 +149,11 @@ export class ModuleGraph {
mod.ssrTransformResult = null
mod.ssrModule = null
mod.ssrError = null

// Fix #3033
if (hmrBoundaries.includes(mod)) {
return
}
mod.importers.forEach((importer) => {
if (!importer.acceptedHmrDeps.has(mod)) {
this.invalidateModule(importer, seen, timestamp, isHmr)
Expand Down
10 changes: 10 additions & 0 deletions playground/hmr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -800,4 +800,14 @@ if (import.meta.hot) {
)
await untilUpdated(() => el.textContent(), '2')
})

test('issue-3033', async () => {
await page.goto(viteTestUrl + '/issue-3033/index.html')
const el = await page.$('.issue-3033')
expect(await el.textContent()).toBe('c')
editFile('issue-3033/c.js', (code) =>
code.replace(`export const c = 'c'`, `export const c = 'cc'`),
)
await untilUpdated(() => el.textContent(), 'cc')
})
}
5 changes: 5 additions & 0 deletions playground/hmr/issue-3033/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { b } from './b'

export const a = {
b,
}
7 changes: 7 additions & 0 deletions playground/hmr/issue-3033/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { c } from './c'

const b = {
c,
}

export { b }
12 changes: 12 additions & 0 deletions playground/hmr/issue-3033/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import './b'

export const c = 'c'

function render(content) {
document.querySelector('.issue-3033').textContent = content
}
render(c)

import.meta.hot?.accept((nextExports) => {
render(nextExports.c)
})
2 changes: 2 additions & 0 deletions playground/hmr/issue-3033/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<script type="module" src="index.js"></script>
<div class="issue-3033"></div>
3 changes: 3 additions & 0 deletions playground/hmr/issue-3033/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { a } from './a'

console.log(a)

0 comments on commit 1143e0b

Please sign in to comment.