From 3609e79dc1416073dc4775bb2fcf6a7398f169b3 Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 17 May 2023 15:15:58 +0200 Subject: [PATCH] fix: avoid outdated module to crash in importAnalysis after restart (#13231) --- packages/vite/src/node/plugins/importAnalysis.ts | 6 +++--- .../vite/src/node/server/middlewares/indexHtml.ts | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/vite/src/node/plugins/importAnalysis.ts b/packages/vite/src/node/plugins/importAnalysis.ts index 119f0192cabb43..21f8b807fa13c7 100644 --- a/packages/vite/src/node/plugins/importAnalysis.ts +++ b/packages/vite/src/node/plugins/importAnalysis.ts @@ -254,9 +254,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin { // since we are already in the transform phase of the importer, it must // have been loaded so its entry is guaranteed in the module graph. const importerModule = moduleGraph.getModuleById(importer)! - if (!importerModule && depsOptimizer?.isOptimizedDepFile(importer)) { - // Ids of optimized deps could be invalidated and removed from the graph - // Return without transforming, this request is no longer valid, a full reload + if (!importerModule) { + // When the server is restarted, the module graph is cleared, so we + // return without transforming. This request is no longer valid, a full reload // is going to request this id again. Throwing an outdated error so we // properly finish the request with a 504 sent to the browser. throwOutdatedRequest(importer) diff --git a/packages/vite/src/node/server/middlewares/indexHtml.ts b/packages/vite/src/node/server/middlewares/indexHtml.ts index 9c7d6727f6fd9d..a063bc91bdbaf9 100644 --- a/packages/vite/src/node/server/middlewares/indexHtml.ts +++ b/packages/vite/src/node/server/middlewares/indexHtml.ts @@ -28,6 +28,7 @@ import { ensureWatchedFile, fsPathFromId, injectQuery, + isJSRequest, joinUrlSegments, normalizePath, processSrcSetSync, @@ -35,6 +36,7 @@ import { unwrapId, wrapId, } from '../../utils' +import { isCSSRequest } from '../../plugins/css' import { checkPublicFile } from '../../plugins/asset' import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap' @@ -82,6 +84,12 @@ function getHtmlFilename(url: string, server: ViteDevServer) { } } +function shouldPreTransform(url: string, config: ResolvedConfig) { + return ( + !checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url)) + ) +} + const processNodeUrl = ( attr: Token.Attribute, sourceCodeLocation: Token.Location, @@ -104,7 +112,7 @@ const processNodeUrl = ( // prefix with base (dev only, base is never relative) const fullUrl = path.posix.join(devBase, url) overwriteAttrValue(s, sourceCodeLocation, fullUrl) - if (server && !checkPublicFile(url, config)) { + if (server && shouldPreTransform(url, config)) { preTransformRequest(server, fullUrl, devBase) } } else if ( @@ -116,7 +124,7 @@ const processNodeUrl = ( // prefix with base (dev only, base is never relative) const replacer = (url: string) => { const fullUrl = path.posix.join(devBase, url) - if (server && !checkPublicFile(url, config)) { + if (server && shouldPreTransform(url, config)) { preTransformRequest(server, fullUrl, devBase) } return fullUrl