Skip to content

Commit

Permalink
refactor: cache by url, not by import-url (#17974)
Browse files Browse the repository at this point in the history
  • Loading branch information
sheremet-va authored Aug 29, 2024
1 parent 0c1cfab commit 219581c
Showing 1 changed file with 20 additions and 15 deletions.
35 changes: 20 additions & 15 deletions packages/vite/src/module-runner/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,35 +245,40 @@ export class ModuleRunner {
}

private async cachedModule(url: string, importer?: string) {
const cacheKey = `${url}~~${importer}`
let cached = this.moduleInfoCache.get(cacheKey)
url = normalizeAbsoluteUrl(url, this.root)

const normalized = this.urlToIdMap.get(url)
let cachedModule = normalized && this.moduleCache.getByModuleId(normalized)
if (!cachedModule) {
cachedModule = this.moduleCache.getByModuleId(url)
}

let cached = this.moduleInfoCache.get(url)
if (!cached) {
cached = this.getModuleInformation(url, importer).finally(() => {
this.moduleInfoCache.delete(cacheKey)
})
this.moduleInfoCache.set(cacheKey, cached)
cached = this.getModuleInformation(url, importer, cachedModule).finally(
() => {
this.moduleInfoCache.delete(url)
},
)
this.moduleInfoCache.set(url, cached)
} else {
this.debug?.('[module runner] using cached module info for', url)
}

return cached
}

private async getModuleInformation(
url: string,
importer?: string,
importer: string | undefined,
cachedModule: ModuleCache | undefined,
): Promise<ModuleCache> {
if (this.destroyed) {
throw new Error(`Vite module runner has been destroyed.`)
}

this.debug?.('[module runner] fetching', url)

url = normalizeAbsoluteUrl(url, this.root)

const normalized = this.urlToIdMap.get(url)
let cachedModule = normalized && this.moduleCache.getByModuleId(normalized)
if (!cachedModule) {
cachedModule = this.moduleCache.getByModuleId(url)
}

const isCached = !!(typeof cachedModule === 'object' && cachedModule.meta)

const fetchedModule = // fast return for established externalized pattern
Expand Down

0 comments on commit 219581c

Please sign in to comment.