UNPKG

astro

Version:

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

50 lines (49 loc) • 1.99 kB
import { prependForwardSlash } from "../core/path.js"; import { DEFAULT_COMPONENTS } from "../core/routing/default.js"; import { routeIsRedirect } from "../core/routing/helpers.js"; import { VIRTUAL_PAGE_MODULE_ID, VIRTUAL_PAGE_RESOLVED_MODULE_ID } from "./const.js"; import { ASTRO_VITE_ENVIRONMENT_NAMES } from "../core/constants.js"; export function pluginPage({ routesList }) { return { name: "@astro/plugin-page", applyToEnvironment(environment) { return environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.ssr || environment.name === ASTRO_VITE_ENVIRONMENT_NAMES.prerender; }, resolveId: { filter: { id: new RegExp(`^${VIRTUAL_PAGE_MODULE_ID}`) }, handler(id) { return VIRTUAL_PAGE_RESOLVED_MODULE_ID + id.slice(VIRTUAL_PAGE_MODULE_ID.length); } }, load: { filter: { id: new RegExp(`^${VIRTUAL_PAGE_RESOLVED_MODULE_ID}`) }, handler(id) { const componentPath = getComponentFromVirtualModulePageName(VIRTUAL_PAGE_RESOLVED_MODULE_ID, id); // Skip default components (404, server islands, etc.) if (DEFAULT_COMPONENTS.some((component) => componentPath === component)) { return { code: "" }; } // Find the route(s) that use this component const routes = routesList.routes.filter((route) => route.component === componentPath); for (const route of routes) { if (routeIsRedirect(route)) { continue; } const astroModuleId = prependForwardSlash(componentPath); const imports = []; const exports = []; imports.push(`import * as _page from ${JSON.stringify(astroModuleId)};`); exports.push(`export const page = () => _page`); return { code: `${imports.join("\n")}\n${exports.join("\n")}` }; } } } }; } /** * From the VirtualModulePageName, get the component path. * Remember that the component can be use by multiple routes. */ function getComponentFromVirtualModulePageName(virtualModulePrefix, id) { return id.slice(virtualModulePrefix.length).replace(/@_@/g, "."); }