Skip to content

Commit

Permalink
fix: full reload edgecase
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 12, 2024
1 parent 89f7a9e commit a95b8d1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
42 changes: 26 additions & 16 deletions packages/vite/src/module-runner/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function handleHMRPayload(
case 'full-reload': {
const { triggeredBy } = payload
const clearEntrypoints = triggeredBy
? findFileEntrypoints(runner, triggeredBy)
? getModulesEntrypoints(runner, getModulesByFile(runner, triggeredBy))
: findAllEntrypoints(runner)

if (!clearEntrypoints.size) break
Expand Down Expand Up @@ -119,23 +119,33 @@ class Queue {
}
}

function findFileEntrypoints(
function getModulesByFile(runner: ModuleRunner, file: string) {
const modules: string[] = []
for (const [id, mod] of runner.moduleCache.entries()) {
if (mod.meta && 'file' in mod.meta && mod.meta.file === file) {
modules.push(id)
}
}
return modules
}

function getModulesEntrypoints(
runner: ModuleRunner,
id: string,
entrypoints = new Set<string>(),
modules: string[],
visited = new Set<string>(),
): Set<string> {
if (visited.has(id)) return entrypoints
visited.add(id)
const mod = runner.moduleCache.get(id)
if (!mod || !mod.importers) return entrypoints
// entry point is a module that doesn't have any importers
if (!mod.importers.size) {
entrypoints.add(id)
return entrypoints
}
for (const importer of mod.importers) {
findFileEntrypoints(runner, importer, entrypoints)
entrypoints = new Set<string>(),
) {
for (const moduleId of modules) {
if (visited.has(moduleId)) continue
visited.add(moduleId)
const module = runner.moduleCache.getByModuleId(moduleId)
if (module.importers && !module.importers.size) {
entrypoints.add(moduleId)
continue
}
for (const importer of module.importers || []) {
getModulesEntrypoints(runner, [importer], visited, entrypoints)
}
}
return entrypoints
}
Expand Down
2 changes: 1 addition & 1 deletion playground/hmr-ssr/__tests__/hmr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,7 @@ describe('acceptExports', () => {
await server.transformRequest('non-tested/dep.js', { ssr: true })
})

test.only('does not full reload', async () => {
test('does not full reload', async () => {
editFile(
testFile,
(code) => code + '\n\nexport const query5 = "query5"',
Expand Down

0 comments on commit a95b8d1

Please sign in to comment.