Skip to content

Commit

Permalink
perf: use transform cache by resolved id (#15785)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
  • Loading branch information
bluwy and antfu authored Feb 6, 2024
1 parent 5fbeba3 commit 78d838a
Showing 1 changed file with 54 additions and 19 deletions.
73 changes: 54 additions & 19 deletions packages/vite/src/node/server/transformRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,31 +131,23 @@ async function doTransform(
url = removeTimestampQuery(url)

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

if (ssr && isDepsOptimizerEnabled(config, true)) {
await initDevSsrDepsOptimizer(config, server)
}

const module = await server.moduleGraph.getModuleByUrl(url, ssr)

// tries to handle soft invalidation of the module if available,
// returns a boolean true is successful, or false if no handling is needed
const softInvalidatedTransformResult =
module &&
(await handleModuleSoftInvalidation(module, ssr, timestamp, server))
if (softInvalidatedTransformResult) {
debugCache?.(`[memory-hmr] ${prettyUrl}`)
return softInvalidatedTransformResult
}

// check if we have a fresh cache
const cached =
module && (ssr ? module.ssrTransformResult : module.transformResult)
if (cached) {
debugCache?.(`[memory] ${prettyUrl}`)
return cached
let module = await server.moduleGraph.getModuleByUrl(url, ssr)
if (module) {
// try use cache from url
const cached = await getCachedTransformResult(
url,
module,
server,
ssr,
timestamp,
)
if (cached) return cached
}

const resolved = module
Expand All @@ -165,6 +157,21 @@ async function doTransform(
// resolve
const id = module?.id ?? resolved?.id ?? url

module ??= server.moduleGraph.getModuleById(id)
if (module) {
// if a different url maps to an existing loaded id, make sure we relate this url to the id
await server.moduleGraph._ensureEntryFromUrl(url, ssr, undefined, resolved)
// try use cache from id
const cached = await getCachedTransformResult(
url,
module,
server,
ssr,
timestamp,
)
if (cached) return cached
}

const result = loadAndTransform(
id,
url,
Expand All @@ -180,6 +187,34 @@ async function doTransform(
return result
}

async function getCachedTransformResult(
url: string,
module: ModuleNode,
server: ViteDevServer,
ssr: boolean,
timestamp: number,
) {
const prettyUrl = debugCache ? prettifyUrl(url, server.config.root) : ''

// tries to handle soft invalidation of the module if available,
// returns a boolean true is successful, or false if no handling is needed
const softInvalidatedTransformResult =
module &&
(await handleModuleSoftInvalidation(module, ssr, timestamp, server))
if (softInvalidatedTransformResult) {
debugCache?.(`[memory-hmr] ${prettyUrl}`)
return softInvalidatedTransformResult
}

// check if we have a fresh cache
const cached =
module && (ssr ? module.ssrTransformResult : module.transformResult)
if (cached) {
debugCache?.(`[memory] ${prettyUrl}`)
return cached
}
}

async function loadAndTransform(
id: string,
url: string,
Expand Down

0 comments on commit 78d838a

Please sign in to comment.