Skip to content

Commit

Permalink
fix: avoid outdated module to crash in importAnalysis after restart (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev authored May 17, 2023
1 parent 0cbd818 commit 3609e79
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions packages/vite/src/node/plugins/importAnalysis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 10 additions & 2 deletions packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ import {
ensureWatchedFile,
fsPathFromId,
injectQuery,
isJSRequest,
joinUrlSegments,
normalizePath,
processSrcSetSync,
stripBase,
unwrapId,
wrapId,
} from '../../utils'
import { isCSSRequest } from '../../plugins/css'
import { checkPublicFile } from '../../plugins/asset'
import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap'

Expand Down Expand Up @@ -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,
Expand All @@ -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 (
Expand All @@ -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
Expand Down

0 comments on commit 3609e79

Please sign in to comment.