Skip to content

Commit 8404fb0

Browse files
authored
minor refactoring (#2953)
1 parent 7dab0e7 commit 8404fb0

1 file changed

Lines changed: 37 additions & 39 deletions

File tree

packages/vike/node/vite/plugins/pluginVirtualFiles.ts

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -105,25 +105,29 @@ async function onFileModified(ctx: HmrContext, config: ResolvedConfig) {
105105
const { file, server } = ctx
106106
const isAppFile = await isAppDependency(ctx.file, ctx.server.moduleGraph)
107107
debugFileChange(isAppFile)
108+
if (!isAppFile) return
109+
const reloadVikeConfig = () => reloadConfig(file, config, 'modified', server)
108110

109-
if (isAppFile) {
110-
if (isAppFile.isConfigDependency) {
111-
/* Tailwind breaks this assertion, see https://github.com/vikejs/vike/discussions/1330#discussioncomment-7787238
112-
const isViteModule = ctx.modules.length > 0
113-
assert(!isViteModule)
114-
*/
111+
if (isAppFile.isRuntimeDependency) {
112+
// Ensure we invalidate `file` *before* server.ssrLoadModule() in updateUserFiles()
113+
// Vite also invalidates it, but *after* handleHotUpdate() and thus after server.ssrLoadModule()
114+
ctx.modules.forEach((mod) => server.moduleGraph.invalidateModule(mod))
115+
// Re-running ssrLoadModule() is cheap (Vite uses a cache) => eagerly calling updateUserFiles() makes sense.
116+
// - Even for SPA apps that don't have (m)any server files? Ideally, we should set `isRuntimeDependency: true` only for server modules (let's do it once Vite has a clear separate per-environment module graphs).
117+
await updateUserFiles()
118+
}
119+
120+
if (isAppFile.isConfigDependency) {
121+
/* Tailwind breaks this assertion, see https://github.com/vikejs/vike/discussions/1330#discussioncomment-7787238
122+
const isViteModule = ctx.modules.length > 0
123+
assert(!isViteModule)
124+
*/
115125

116-
reloadConfig(file, config, 'modified', server)
126+
reloadVikeConfig()
117127

118-
// Trigger a full page reload. (Because files such as +config.js can potentially modify Vike's virtual files.)
119-
const vikeVirtualFiles = getVikeVirtualFiles(server)
120-
return vikeVirtualFiles
121-
} else {
122-
// Ensure we invalidate `file` *before* server.ssrLoadModule() in updateUserFiles()
123-
// Vite also invalidates it, but *after* handleHotUpdate() and thus after server.ssrLoadModule()
124-
ctx.modules.forEach((mod) => server.moduleGraph.invalidateModule(mod))
125-
await updateUserFiles()
126-
}
128+
// Trigger a full page reload. (Because files such as +config.js can potentially modify Vike's virtual files.)
129+
const vikeVirtualFiles = getVikeVirtualFiles(server)
130+
return vikeVirtualFiles
127131
}
128132
}
129133

@@ -134,37 +138,31 @@ async function onFileCreatedOrRemoved(file: string, isRemove: boolean, server: V
134138
debugFileChange('server.watcher', file, operation)
135139
const { moduleGraph } = server
136140
const isAppFile = await isAppDependency(file, moduleGraph)
137-
const reload = () => reloadConfig(file, config, operation, server)
138-
139-
// Vike config (non-runtime) code
140-
if (isAppFile && isAppFile.isConfigDependency) {
141-
reload()
142-
return
143-
}
141+
const reloadVikeConfig = () => reloadConfig(file, config, operation, server)
144142

145-
// New or deleted + file
146-
if (isPlusFile(file)) {
147-
reload()
143+
if (
144+
// Vike config (non-runtime) code
145+
isAppFile?.isConfigDependency ||
146+
// New + file => not tracked yet by Vike (`vikeConfigObject._vikeConfigDependencies`) nor Vite (`moduleGraph`)
147+
isPlusFile(file) ||
148+
// Trick: when fixing the path of a relative import => we don't know whether `file` is the imported file => we take a leap of faith when the conditions below are met.
149+
// - Reloading Vike's config is cheap => eagerly reloading it makes sense when it's in an erroneous state.
150+
// - Not sure how reliable that trick is.
151+
// - Reproduction:
152+
// ```bash
153+
// rm someImportedFile.js && sleep 2 && git checkout someImportedFile.js
154+
// ```
155+
(isScriptFile(file) && getVikeConfigError() && !existsInViteModuleGraph(file, moduleGraph))
156+
) {
157+
reloadVikeConfig()
148158
return
149159
}
150160

151161
// Vike runtime code => let Vite handle it
152-
if (isAppFile && isAppFile.isRuntimeDependency) {
162+
if (isAppFile?.isRuntimeDependency) {
153163
assert(existsInViteModuleGraph(file, moduleGraph))
154164
return
155165
}
156-
157-
// Trick: when fixing the path of a relative import => we don't know whether `file` is the imported file => we take a leap of faith when the conditions below are met.
158-
// - Not sure how reliable that trick is.
159-
// - Reloading Vike's config is cheap and file creation/removal is rare => the trick is worth it.
160-
// - Reproduction:
161-
// ```bash
162-
// rm someImportedFile.js && sleep 2 && git checkout someImportedFile.js
163-
// ```
164-
if (isScriptFile(file) && getVikeConfigError() && !existsInViteModuleGraph(file, moduleGraph)) {
165-
reload()
166-
return
167-
}
168166
}
169167

170168
async function isAppDependency(filePathAbsoluteFilesystem: string, moduleGraph: ModuleGraph) {

0 commit comments

Comments
 (0)