fix: invalidate mod before crawling graph
This commit is contained in:
parent
109aeff6a1
commit
883710d21c
1 changed files with 38 additions and 36 deletions
|
@ -5,33 +5,30 @@ import { STYLE_EXTENSIONS } from '../util.js';
|
||||||
|
|
||||||
const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
|
const STRIP_QUERY_PARAMS_REGEX = /\?.*$/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of file extensions signalling we can (and should) SSR ahead-of-time
|
||||||
|
* See usage below
|
||||||
|
*/
|
||||||
|
const fileExtensionsToSSR = new Set(['.astro', '.md']);
|
||||||
|
|
||||||
/** recursively crawl the module graph to get all style files imported by parent id */
|
/** recursively crawl the module graph to get all style files imported by parent id */
|
||||||
export async function* crawlGraph(
|
export async function* crawlGraph(
|
||||||
viteServer: vite.ViteDevServer,
|
viteServer: vite.ViteDevServer,
|
||||||
_id: string,
|
_id: string,
|
||||||
isFile: boolean,
|
isRootFile: boolean,
|
||||||
scanned = new Set<string>()
|
scanned = new Set<string>()
|
||||||
): AsyncGenerator<vite.ModuleNode, void, unknown> {
|
): AsyncGenerator<vite.ModuleNode, void, unknown> {
|
||||||
const id = unwrapId(_id);
|
const id = unwrapId(_id);
|
||||||
const importedModules = new Set<vite.ModuleNode>();
|
const importedModules = new Set<vite.ModuleNode>();
|
||||||
const moduleEntriesForId = isFile
|
if (isRootFile) {
|
||||||
? // If isFile = true, then you are at the root of your module import tree.
|
// The module graph may be out-of-date when crawling
|
||||||
// The `id` arg is a filepath, so use `getModulesByFile()` to collect all
|
// ex. Tailwind PostCSS process takes time
|
||||||
// nodes for that file. This is needed for advanced imports like Tailwind.
|
// Use "onFileChange" to invalidate cache
|
||||||
viteServer.moduleGraph.getModulesByFile(id) ?? new Set()
|
viteServer.moduleGraph.onFileChange(id);
|
||||||
: // Otherwise, you are following an import in the module import tree.
|
|
||||||
// You are safe to use getModuleById() here because Vite has already
|
|
||||||
// resolved the correct `id` for you, by creating the import you followed here.
|
|
||||||
new Set([viteServer.moduleGraph.getModuleById(id)]);
|
|
||||||
|
|
||||||
// Collect all imported modules for the module(s).
|
|
||||||
for (const entry of moduleEntriesForId) {
|
|
||||||
// Handle this in case an module entries weren't found for ID
|
|
||||||
// This seems possible with some virtual IDs (ex: `astro:markdown/*.md`)
|
|
||||||
if (!entry) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if (id === entry.id) {
|
const entry = viteServer.moduleGraph.getModuleById(id);
|
||||||
|
|
||||||
|
if (id === entry?.id) {
|
||||||
scanned.add(id);
|
scanned.add(id);
|
||||||
const entryIsStyle = STYLE_EXTENSIONS.has(npath.extname(id));
|
const entryIsStyle = STYLE_EXTENSIONS.has(npath.extname(id));
|
||||||
for (const importedModule of entry.importedModules) {
|
for (const importedModule of entry.importedModules) {
|
||||||
|
@ -50,11 +47,16 @@ export async function* crawlGraph(
|
||||||
if (entryIsStyle && !STYLE_EXTENSIONS.has(npath.extname(importedModulePathname))) {
|
if (entryIsStyle && !STYLE_EXTENSIONS.has(npath.extname(importedModulePathname))) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
|
||||||
|
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
|
||||||
|
if (!mod?.ssrModule) {
|
||||||
|
await viteServer.ssrLoadModule(importedModule.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
importedModules.add(importedModule);
|
importedModules.add(importedModule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// scan imported modules for CSS imports & add them to our collection.
|
// scan imported modules for CSS imports & add them to our collection.
|
||||||
// Then, crawl that file to follow and scan all deep imports as well.
|
// Then, crawl that file to follow and scan all deep imports as well.
|
||||||
|
|
Loading…
Reference in a new issue