Skip to content

Commit 3609e79

Browse files
authoredMay 17, 2023
fix: avoid outdated module to crash in importAnalysis after restart (#13231)
1 parent 0cbd818 commit 3609e79

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

‎packages/vite/src/node/plugins/importAnalysis.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -254,9 +254,9 @@ export function importAnalysisPlugin(config: ResolvedConfig): Plugin {
254254
// since we are already in the transform phase of the importer, it must
255255
// have been loaded so its entry is guaranteed in the module graph.
256256
const importerModule = moduleGraph.getModuleById(importer)!
257-
if (!importerModule && depsOptimizer?.isOptimizedDepFile(importer)) {
258-
// Ids of optimized deps could be invalidated and removed from the graph
259-
// Return without transforming, this request is no longer valid, a full reload
257+
if (!importerModule) {
258+
// When the server is restarted, the module graph is cleared, so we
259+
// return without transforming. This request is no longer valid, a full reload
260260
// is going to request this id again. Throwing an outdated error so we
261261
// properly finish the request with a 504 sent to the browser.
262262
throwOutdatedRequest(importer)

‎packages/vite/src/node/server/middlewares/indexHtml.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ import {
2828
ensureWatchedFile,
2929
fsPathFromId,
3030
injectQuery,
31+
isJSRequest,
3132
joinUrlSegments,
3233
normalizePath,
3334
processSrcSetSync,
3435
stripBase,
3536
unwrapId,
3637
wrapId,
3738
} from '../../utils'
39+
import { isCSSRequest } from '../../plugins/css'
3840
import { checkPublicFile } from '../../plugins/asset'
3941
import { getCodeWithSourcemap, injectSourcesContent } from '../sourcemap'
4042

@@ -82,6 +84,12 @@ function getHtmlFilename(url: string, server: ViteDevServer) {
8284
}
8385
}
8486

87+
function shouldPreTransform(url: string, config: ResolvedConfig) {
88+
return (
89+
!checkPublicFile(url, config) && (isJSRequest(url) || isCSSRequest(url))
90+
)
91+
}
92+
8593
const processNodeUrl = (
8694
attr: Token.Attribute,
8795
sourceCodeLocation: Token.Location,
@@ -104,7 +112,7 @@ const processNodeUrl = (
104112
// prefix with base (dev only, base is never relative)
105113
const fullUrl = path.posix.join(devBase, url)
106114
overwriteAttrValue(s, sourceCodeLocation, fullUrl)
107-
if (server && !checkPublicFile(url, config)) {
115+
if (server && shouldPreTransform(url, config)) {
108116
preTransformRequest(server, fullUrl, devBase)
109117
}
110118
} else if (
@@ -116,7 +124,7 @@ const processNodeUrl = (
116124
// prefix with base (dev only, base is never relative)
117125
const replacer = (url: string) => {
118126
const fullUrl = path.posix.join(devBase, url)
119-
if (server && !checkPublicFile(url, config)) {
127+
if (server && shouldPreTransform(url, config)) {
120128
preTransformRequest(server, fullUrl, devBase)
121129
}
122130
return fullUrl

0 commit comments

Comments
 (0)
Please sign in to comment.