Replies: 3 comments
|
configureServer(server) {
// Vite 6+: server.environments.client.moduleGraph is the real graph.
// server.moduleGraph is the old mixed client+ssr one kept for back-compat.
for (const [id, mod] of server.environments.client.moduleGraph.idToModuleMap) {
console.log(id, mod.file, mod.type)
}
}
But the likely cause: externalized ids ( To re-run const importersById = new Map() // externalizedId -> Set<importerId>
resolveId(id, importer) {
if (shouldExternalize(id) && importer) {
let set = importersById.get(id)
if (!set) importersById.set(id, (set = new Set()))
set.add(importer)
return { id, external: true }
}
}Then invalidate just those importers instead of the whole graph: const graph = server.environments.client.moduleGraph
for (const importerId of importersById.get(changedId) ?? []) {
const mod = graph.getModuleById(importerId)
if (mod) graph.invalidateModule(mod)
}
server.ws.send({ type: 'full-reload' })
on Vite 6+ |
|
This is a great question, and your instinct to step back and look out for the X->Y problem is spot on. You have hit a very specific, nuanced behavior in how Vite caches module resolutions. Here is exactly why you cannot find those modules, and how to fix your underlying problem without resorting to the nuclear Why
|
|
Yes — in the dev server, you can access the module graph through import type { Plugin } from "vite"
export function myPlugin(): Plugin {
return {
name: "module-graph-enumerator",
configureServer(server) {
// Iterate all known modules
server.moduleGraph.forEachModule((mod) => {
console.log(mod.url, mod.id, mod.file)
})
// Get specific module by URL
const mod = server.moduleGraph.getModuleByUrl("/src/main.tsx")
// Get all modules imported by a given module
const importers = mod?.importers // modules that import this
const imports = mod?.importedModules // modules this imports
}
}
}
For watching, the |
Uh oh!
There was an error while loading. Please reload this page.
Hello, everyone.
I have this need to invalidate previous resolutions done by my plug-in's
resolveIdhook. However, I cannot locate them insideserver.moduleGraphusingserver.moduleGraph.getModuleById. Doingserver.moduleGraph.invalidateAll()does the job, so I'm thinking that there must be a way to locate the modules I resolved previously, but without being able to examine the contents ofserver.moduleGraph, I have no idea how to troubleshoot this one.Just in case I'm falling into the X->Y problem, here's my original problem: My plug-in externalizes certain module ID's, and on-demand it can be requested to "undo" those resolutions because the resolution data changed.
Without
server.moduleGraph.invalidateAll(), my plug-in is not asked to resolve the ID's again. This is why I'm fixated on the module graph at the moment.All reactions