UNPKG

astro

Version:

Astro is a modern site builder with web best practices, performance, and DX front-of-mind.

60 lines (59 loc) • 2.13 kB
import { VIRTUAL_PAGE_RESOLVED_MODULE_ID } from "../../vite-plugin-pages/const.js"; // This walks up the dependency graph and yields out each ModuleInfo object. export function getParentExtendedModuleInfos(id, ctx, until, depth = 0, order = 0, childId = "", seen = new Set(), accumulated = []) { seen.add(id); const info = ctx.getModuleInfo(id); if (info) { if (childId) { const idx = info.importedIds.indexOf(childId); if (idx === -1) { // Dynamic imports come after all normal imports. So first add the number of normal imports. order += info.importedIds.length; // Then add on the dynamic ones. order += info.dynamicallyImportedIds.indexOf(childId); } else { order += idx; } } accumulated.push({ info, depth, order }); } if (info && !until?.(id)) { const importers = info.importers.concat(info.dynamicImporters); for (const imp of importers) { if (!seen.has(imp)) { getParentExtendedModuleInfos(imp, ctx, until, depth + 1, order, id, seen, accumulated); } } } return accumulated; } export function getParentModuleInfos(id, ctx, until, seen = new Set(), accumulated = []) { seen.add(id); const info = ctx.getModuleInfo(id); if (info) { accumulated.push(info); } if (info && !until?.(id)) { const importers = info.importers.concat(info.dynamicImporters); for (const imp of importers) { if (!seen.has(imp)) { getParentModuleInfos(imp, ctx, until, seen, accumulated); } } } return accumulated; } // Returns true if a module is a top-level page. We determine this based on whether // it is imported by the top-level virtual module. export function moduleIsTopLevelPage(info) { return info.importers[0]?.includes(VIRTUAL_PAGE_RESOLVED_MODULE_ID) || info.dynamicImporters[0]?.includes(VIRTUAL_PAGE_RESOLVED_MODULE_ID); } // This function walks the dependency graph, going up until it finds a page component. // This could be a .astro page, a .markdown or a .md (or really any file extension for markdown files) page. export function getTopLevelPageModuleInfos(id, ctx) { return getParentModuleInfos(id, ctx).filter(moduleIsTopLevelPage); }