Skip to content

Commit

Permalink
refactor: rename ssrInvalidates to invalidates
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va committed Mar 28, 2024
1 parent 31e1d3a commit 72fe84e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ export class ModuleRunner {
? silentConsole
: options.hmr.logger || console,
options.hmr.connection,
({ acceptedPath }) => {
this.moduleCache.invalidateDepTree([acceptedPath])
({ acceptedPath, invalidates }) => {
this.moduleCache.invalidate(acceptedPath)
if (invalidates) {
this.invalidateFiles(invalidates)
}
return this.import(acceptedPath)
},
)
Expand Down Expand Up @@ -125,6 +128,16 @@ export class ModuleRunner {
return this._destroyed
}

// map files to modules and invalidate them
private invalidateFiles(files: string[]) {
files.forEach((file) => {
const ids = this.fileToIdMap.get(file)
if (ids) {
ids.forEach((id) => this.moduleCache.invalidate(id))
}
})
}

// we don't use moduleCache.normalize because this URL doesn't have to follow the same rules
// this URL is something that user passes down manually, and is later resolved by fetchModule
// moduleCache.normalize is used on resolved "file" property
Expand Down
27 changes: 27 additions & 0 deletions packages/vite/src/node/server/hmr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ export function updateModules(
? isExplicitImportRequired(acceptedVia.url)
: false,
isWithinCircularImport,
invalidates: getInvalidatedImporters(acceptedVia),
}),
),
)
Expand Down Expand Up @@ -435,6 +436,32 @@ export function updateModules(
})
}

function populateImporters(
module: EnvironmentModuleNode,
timestamp: number,
seen: Set<EnvironmentModuleNode> = new Set(),
) {
module.importedModules.forEach((importer) => {
if (seen.has(importer)) {
return
}
if (
importer.lastHMRTimestamp === timestamp ||
importer.lastInvalidationTimestamp === timestamp
) {
seen.add(importer)
populateImporters(importer, timestamp, seen)
}
})
return seen
}

function getInvalidatedImporters(module: EnvironmentModuleNode) {
return [...populateImporters(module, module.lastHMRTimestamp)].map(
(m) => m.file!,
)
}

function areAllImportsAccepted(
importedBindings: Set<string>,
acceptedExports: Set<string>,
Expand Down
2 changes: 2 additions & 0 deletions packages/vite/types/hmrPayload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export interface Update {
explicitImportRequired?: boolean
/** @internal */
isWithinCircularImport?: boolean
/** @internal */
invalidates?: string[]
}

export interface PrunePayload {
Expand Down

0 comments on commit 72fe84e

Please sign in to comment.