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

fix(hmr): only invalidate lastHMRTimestamp of importers if the invalidated module is not a HMR boundary #13024

Merged
merged 6 commits into from
Jun 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -121,6 +121,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 @@ -139,6 +140,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 @@ -786,4 +786,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)
Loading