Skip to content

Commit

Permalink
fix: run only affected entry points for a full-reload
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Feb 13, 2024
1 parent 8a370d6 commit b660e27
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ export async function handleHMRUpdate(
hot.send({
type: 'full-reload',
path: '*',
via: file,
})
return
}
Expand Down Expand Up @@ -255,7 +256,7 @@ export function updateModules(
isWithinCircularImport,
// browser modules are invalidated by changing ?t= query,
// but in ssr we control the module system, so we can directly remove them form cache
ssrInvalidates: getSSRInvalidatedImporters(acceptedVia),
ssrInvalidates: getSSRInvalidatedImporters(config.root, acceptedVia),
}),
),
)
Expand All @@ -272,6 +273,7 @@ export function updateModules(
)
hot.send({
type: 'full-reload',
via: file,
})
return
}
Expand All @@ -295,7 +297,7 @@ export function updateModules(
function populateSSRImporters(
module: ModuleNode,
timestamp: number,
seen: Set<ModuleNode>,
seen: Set<ModuleNode> = new Set(),
) {
module.ssrImportedModules.forEach((importer) => {
if (seen.has(importer)) {
Expand All @@ -312,10 +314,10 @@ function populateSSRImporters(
return seen
}

function getSSRInvalidatedImporters(module: ModuleNode) {
return [
...populateSSRImporters(module, module.lastHMRTimestamp, new Set()),
].map((m) => m.file!)
function getSSRInvalidatedImporters(root: string, module: ModuleNode) {
return [...populateSSRImporters(module, module.lastHMRTimestamp)].map((m) =>
path.posix.relative(root, m.file!),
)
}

export async function handleFileAddUnlink(
Expand Down
14 changes: 12 additions & 2 deletions packages/vite/src/node/ssr/runtime/hmrHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,28 @@ export async function handleHMRPayload(
await hmrClient.notifyListeners(payload.event, payload.data)
break
}
case 'full-reload':
case 'full-reload': {
hmrClient.logger.debug(`[vite] program reload`)
await hmrClient.notifyListeners('vite:beforeFullReload', payload)
Array.from(runtime.moduleCache.keys()).forEach((id) => {
if (!id.includes('node_modules')) {
runtime.moduleCache.deleteByModuleId(id)
}
})
for (const id of runtime.entrypoints) {
const { via } = payload
const clearEntrypoints = via
? [...runtime.entrypoints].filter((entrypoint) =>
runtime.moduleCache.isImported({
importedId: via,
importedBy: entrypoint,
}),
)
: runtime.entrypoints
for (const id of clearEntrypoints) {
await runtime.executeUrl(id)
}
break
}
case 'prune':
await hmrClient.notifyListeners('vite:beforePrune', payload)
hmrClient.prunePaths(payload.paths)
Expand Down
34 changes: 34 additions & 0 deletions packages/vite/src/node/ssr/runtime/moduleCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,40 @@ export class ModuleCacheMap extends Map<string, ModuleCache> {
return this.deleteByModuleId(this.normalize(fsPath))
}

isImported(
{
importedId,
importedBy,
}: {
importedId: string
importedBy: string
},
seen = new Set<string>(),
): boolean {
importedId = this.normalize(importedId)
if (seen.has(importedId)) return false
seen.add(importedId)

const fileModule = this.getByModuleId(importedId)
const importers = fileModule?.importers
if (!importers) return false

importedBy = this.normalize(importedBy)

if (importers.has(importedBy)) return true

for (const importer of importers) {
if (
this.isImported({
importedBy: importedBy,
importedId: importer,
})
)
return true
}
return false
}

/**
* Invalidate modules that dependent on the given modules, up to the main entry
*/
Expand Down
1 change: 1 addition & 0 deletions packages/vite/types/hmrPayload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface PrunePayload {
export interface FullReloadPayload {
type: 'full-reload'
path?: string
via?: string
}

export interface CustomPayload {
Expand Down

0 comments on commit b660e27

Please sign in to comment.