UNPKG

astro

Version:

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

45 lines (44 loc) • 1.69 kB
import { fileURLToPath } from "node:url"; import { AstroError } from "../errors/errors.js"; import { CacheProviderNotFound } from "../errors/errors-data.js"; import { normalizeCacheProviderConfig } from "./utils.js"; export const VIRTUAL_CACHE_PROVIDER_ID = "virtual:astro:cache-provider"; const RESOLVED_VIRTUAL_CACHE_PROVIDER_ID = "\0" + VIRTUAL_CACHE_PROVIDER_ID; export function vitePluginCacheProvider({ settings }) { const providerConfig = settings.config.cache?.provider; if (!providerConfig) { return; } return { name: VIRTUAL_CACHE_PROVIDER_ID, enforce: "pre", resolveId: { filter: { id: new RegExp(`^${VIRTUAL_CACHE_PROVIDER_ID}$`) }, handler() { return RESOLVED_VIRTUAL_CACHE_PROVIDER_ID; } }, load: { filter: { id: new RegExp(`^${RESOLVED_VIRTUAL_CACHE_PROVIDER_ID}$`) }, async handler() { const provider = normalizeCacheProviderConfig(providerConfig); // Use the project root as the importer so that adapter-provided // providers (e.g. astro/cache/memory) resolve from the project's // node_modules, not from astro core's location. const importerPath = fileURLToPath(new URL("package.json", settings.config.root)); let resolved; try { resolved = await this.resolve(provider.entrypoint, importerPath); } catch {} if (!resolved) { const displayName = provider.name ?? provider.entrypoint; throw new AstroError({ ...CacheProviderNotFound, message: CacheProviderNotFound.message(displayName) }); } return { code: `import { default as _default } from ${JSON.stringify(resolved.id)};\nexport * from ${JSON.stringify(resolved.id)};\nexport default _default;` }; } } }; }