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,54 +5,56 @@ 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
|
const entry = viteServer.moduleGraph.getModuleById(id);
|
||||||
// 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).
|
if (id === entry?.id) {
|
||||||
for (const entry of moduleEntriesForId) {
|
scanned.add(id);
|
||||||
// Handle this in case an module entries weren't found for ID
|
const entryIsStyle = STYLE_EXTENSIONS.has(npath.extname(id));
|
||||||
// This seems possible with some virtual IDs (ex: `astro:markdown/*.md`)
|
for (const importedModule of entry.importedModules) {
|
||||||
if (!entry) {
|
// some dynamically imported modules are *not* server rendered in time
|
||||||
continue;
|
// to only SSR modules that we can safely transform, we check against
|
||||||
}
|
// a list of file extensions based on our built-in vite plugins
|
||||||
if (id === entry.id) {
|
if (importedModule.id) {
|
||||||
scanned.add(id);
|
// Strip special query params like "?content".
|
||||||
const entryIsStyle = STYLE_EXTENSIONS.has(npath.extname(id));
|
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
|
||||||
for (const importedModule of entry.importedModules) {
|
// For example, `virtual:image-loader` if you don't have the plugin installed.
|
||||||
// some dynamically imported modules are *not* server rendered in time
|
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
|
||||||
// to only SSR modules that we can safely transform, we check against
|
// If the entry is a style, skip any modules that are not also styles.
|
||||||
// a list of file extensions based on our built-in vite plugins
|
// Tools like Tailwind might add HMR dependencies as `importedModules`
|
||||||
if (importedModule.id) {
|
// but we should skip them--they aren't really imported. Without this,
|
||||||
// Strip special query params like "?content".
|
// every hoisted script in the project is added to every page!
|
||||||
// NOTE: Cannot use `new URL()` here because not all IDs will be valid paths.
|
if (entryIsStyle && !STYLE_EXTENSIONS.has(npath.extname(importedModulePathname))) {
|
||||||
// For example, `virtual:image-loader` if you don't have the plugin installed.
|
continue;
|
||||||
const importedModulePathname = importedModule.id.replace(STRIP_QUERY_PARAMS_REGEX, '');
|
}
|
||||||
// If the entry is a style, skip any modules that are not also styles.
|
if (fileExtensionsToSSR.has(npath.extname(importedModulePathname))) {
|
||||||
// Tools like Tailwind might add HMR dependencies as `importedModules`
|
const mod = viteServer.moduleGraph.getModuleById(importedModule.id);
|
||||||
// but we should skip them--they aren't really imported. Without this,
|
if (!mod?.ssrModule) {
|
||||||
// every hoisted script in the project is added to every page!
|
await viteServer.ssrLoadModule(importedModule.id);
|
||||||
if (entryIsStyle && !STYLE_EXTENSIONS.has(npath.extname(importedModulePathname))) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
importedModules.add(importedModule);
|
|
||||||
}
|
}
|
||||||
|
importedModules.add(importedModule);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue