diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 9a39c792388400..b08000fbf12930 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -696,7 +696,7 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { NULL_BYTE_PLACEHOLDER, '\0' ) - const request = transformRequest(url, server, { ssr }).catch((e) => { + transformRequest(url, server, { ssr }).catch((e) => { if (e?.code === ERR_OUTDATED_OPTIMIZED_DEP) { // This are expected errors return @@ -704,9 +704,6 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // Unexpected error, log the issue but avoid an unhandled exception config.logger.error(e.message) }) - if (depsOptimizer && !config.legacy?.devDepsScanner) { - depsOptimizer.delayDepsOptimizerUntil(id, () => request) - } }) } diff --git a/packages/vite/src/node/server/transformRequest.ts b/packages/vite/src/node/server/transformRequest.ts index c131463a6abe08..b89a79e4b23cb3 100644 --- a/packages/vite/src/node/server/transformRequest.ts +++ b/packages/vite/src/node/server/transformRequest.ts @@ -17,6 +17,7 @@ import { } from '../utils' import { checkPublicFile } from '../plugins/asset' import { ssrTransform } from '../ssr/ssrTransform' +import { getDepsOptimizer } from '../optimizer' import { injectSourcesContent } from './sourcemap' import { isFileServingAllowed } from './middlewares/static' @@ -118,9 +119,8 @@ async function doTransform( ) { url = removeTimestampQuery(url) - const { config, pluginContainer, moduleGraph, watcher } = server - const { root, logger } = config - const prettyUrl = isDebug ? prettifyUrl(url, root) : '' + const { config, pluginContainer } = server + const prettyUrl = isDebug ? prettifyUrl(url, config.root) : '' const ssr = !!options.ssr const module = await server.moduleGraph.getModuleByUrl(url, ssr) @@ -142,6 +142,29 @@ async function doTransform( // resolve const id = (await pluginContainer.resolveId(url, undefined, { ssr }))?.id || url + + const result = loadAndTransform(id, url, server, options, timestamp) + + const depsOptimizer = getDepsOptimizer(config) + if (depsOptimizer && !config.legacy?.devDepsScanner) { + depsOptimizer.delayDepsOptimizerUntil(id, () => result) + } + + return result +} + +async function loadAndTransform( + id: string, + url: string, + server: ViteDevServer, + options: TransformOptions, + timestamp: number +) { + const { config, pluginContainer, moduleGraph, watcher } = server + const { root, logger } = config + const prettyUrl = isDebug ? prettifyUrl(url, config.root) : '' + const ssr = !!options.ssr + const file = cleanUrl(id) let code: string | null = null