Skip to content

Commit 78d838a

Browse files
bluwyantfu
andauthored
perf: use transform cache by resolved id (#15785)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 5fbeba3 commit 78d838a

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

packages/vite/src/node/server/transformRequest.ts

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -131,31 +131,23 @@ async function doTransform(
131131
url = removeTimestampQuery(url)
132132

133133
const { config, pluginContainer } = server
134-
const prettyUrl = debugCache ? prettifyUrl(url, config.root) : ''
135134
const ssr = !!options.ssr
136135

137136
if (ssr && isDepsOptimizerEnabled(config, true)) {
138137
await initDevSsrDepsOptimizer(config, server)
139138
}
140139

141-
const module = await server.moduleGraph.getModuleByUrl(url, ssr)
142-
143-
// tries to handle soft invalidation of the module if available,
144-
// returns a boolean true is successful, or false if no handling is needed
145-
const softInvalidatedTransformResult =
146-
module &&
147-
(await handleModuleSoftInvalidation(module, ssr, timestamp, server))
148-
if (softInvalidatedTransformResult) {
149-
debugCache?.(`[memory-hmr] ${prettyUrl}`)
150-
return softInvalidatedTransformResult
151-
}
152-
153-
// check if we have a fresh cache
154-
const cached =
155-
module && (ssr ? module.ssrTransformResult : module.transformResult)
156-
if (cached) {
157-
debugCache?.(`[memory] ${prettyUrl}`)
158-
return cached
140+
let module = await server.moduleGraph.getModuleByUrl(url, ssr)
141+
if (module) {
142+
// try use cache from url
143+
const cached = await getCachedTransformResult(
144+
url,
145+
module,
146+
server,
147+
ssr,
148+
timestamp,
149+
)
150+
if (cached) return cached
159151
}
160152

161153
const resolved = module
@@ -165,6 +157,21 @@ async function doTransform(
165157
// resolve
166158
const id = module?.id ?? resolved?.id ?? url
167159

160+
module ??= server.moduleGraph.getModuleById(id)
161+
if (module) {
162+
// if a different url maps to an existing loaded id, make sure we relate this url to the id
163+
await server.moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved)
164+
// try use cache from id
165+
const cached = await getCachedTransformResult(
166+
url,
167+
module,
168+
server,
169+
ssr,
170+
timestamp,
171+
)
172+
if (cached) return cached
173+
}
174+
168175
const result = loadAndTransform(
169176
id,
170177
url,
@@ -180,6 +187,34 @@ async function doTransform(
180187
return result
181188
}
182189

190+
async function getCachedTransformResult(
191+
url: string,
192+
module: ModuleNode,
193+
server: ViteDevServer,
194+
ssr: boolean,
195+
timestamp: number,
196+
) {
197+
const prettyUrl = debugCache ? prettifyUrl(url, server.config.root) : ''
198+
199+
// tries to handle soft invalidation of the module if available,
200+
// returns a boolean true is successful, or false if no handling is needed
201+
const softInvalidatedTransformResult =
202+
module &&
203+
(await handleModuleSoftInvalidation(module, ssr, timestamp, server))
204+
if (softInvalidatedTransformResult) {
205+
debugCache?.(`[memory-hmr] ${prettyUrl}`)
206+
return softInvalidatedTransformResult
207+
}
208+
209+
// check if we have a fresh cache
210+
const cached =
211+
module && (ssr ? module.ssrTransformResult : module.transformResult)
212+
if (cached) {
213+
debugCache?.(`[memory] ${prettyUrl}`)
214+
return cached
215+
}
216+
}
217+
183218
async function loadAndTransform(
184219
id: string,
185220
url: string,

0 commit comments

Comments
 (0)