Skip to content

Commit 19f2aa6

Browse files
committed
perf(vite): cache normalized inspect module ids
1 parent eda6b67 commit 19f2aa6

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎packages/vite/src/node/__tests__/inspect-context.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,3 +1286,26 @@ describe('vite inspect hook timing', () => {
12861286
})
12871287
})
12881288
})
1289+
1290+
describe('vite inspect public module ids', () => {
1291+
it('normalizes dependency ids relative to the current root', async () => {
1292+
const { envCtx, vite } = await createFixture()
1293+
const id = '/project/node_modules/dep/index.js?v=abc123'
1294+
1295+
expect(envCtx.getPublicModuleId(id)).toBe('./node_modules/dep/index.js')
1296+
expect(envCtx.getPublicModuleId(id)).toBe('./node_modules/dep/index.js')
1297+
1298+
// A cached id must follow a root change instead of returning the stale result.
1299+
;(vite.config as { root: string }).root = '/project/node_modules'
1300+
expect(envCtx.getPublicModuleId(id)).toBe('./dep/index.js')
1301+
})
1302+
1303+
it('keeps returning correct ids after the cache is cleared at its size limit', async () => {
1304+
const { envCtx } = await createFixture()
1305+
for (let index = 0; index < 10_001; index++)
1306+
expect(envCtx.getPublicModuleId(`/project/node_modules/dep/m${index}.js`)).toBe(`./node_modules/dep/m${index}.js`)
1307+
1308+
expect(envCtx.getPublicModuleId('/project/node_modules/dep/m0.js')).toBe('./node_modules/dep/m0.js')
1309+
expect(envCtx.getPublicModuleId('/src/main.ts')).toBe('/src/main.ts')
1310+
})
1311+
})

‎packages/vite/src/node/inspect/context.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,16 @@ export class ViteInspectViteContext {
180180
}
181181
}
182182

183+
// Ids can carry HMR timestamps, so cap the cache instead of letting it grow for the whole session.
184+
const MAX_PUBLIC_MODULE_ID_CACHE_SIZE = 10_000
185+
183186
export class ViteInspectEnvironmentContext {
184187
readonly scope: string
185188
private pluginCallCount = 0
186189
private graphModuleIdResolver?: ReturnType<typeof createGraphModuleIdResolver>
190+
// Every hook call records its module id, so the same id is normalized once per plugin per request.
191+
private readonly publicModuleIds = new Map<string, string>()
192+
private publicModuleIdsRoot?: string
187193

188194
constructor(
189195
readonly inspectContext: ViteInspectContext,
@@ -336,7 +342,17 @@ export class ViteInspectEnvironmentContext {
336342
}
337343

338344
getPublicModuleId(id: string): string {
339-
return normalizeModuleId(this.normalizeId(id), this.getModuleIdBaseRoot())
345+
const root = this.getModuleIdBaseRoot()
346+
if (root !== this.publicModuleIdsRoot || this.publicModuleIds.size >= MAX_PUBLIC_MODULE_ID_CACHE_SIZE) {
347+
this.publicModuleIds.clear()
348+
this.publicModuleIdsRoot = root
349+
}
350+
let publicId = this.publicModuleIds.get(id)
351+
if (publicId === undefined) {
352+
publicId = normalizeModuleId(this.normalizeId(id), root)
353+
this.publicModuleIds.set(id, publicId)
354+
}
355+
return publicId
340356
}
341357

342358
async getModuleId(id: string): Promise<string> {

0 commit comments

Comments
 (0)