import { normalizePath } from "vite"; import { cachedCompilation, getCachedCompileResult } from "../core/compile/index.js"; import { isRelativePath } from "../core/path.js"; import { normalizeFilename } from "../vite-plugin-utils/index.js"; import { cachedFullCompilation } from "./compile.js"; import { handleHotUpdate } from "./hmr.js"; import { parseAstroRequest } from "./query.js"; import { getAstroMetadata } from "./metadata.js"; function astro({ settings, logging }) { const { config } = settings; let resolvedConfig; const srcRootWeb = config.srcDir.pathname.slice(config.root.pathname.length - 1); const isBrowserPath = (path) => path.startsWith(srcRootWeb) && srcRootWeb !== "/"; const prePlugin = { name: "astro:build", enforce: "pre", // run transforms before other plugins can configResolved(_resolvedConfig) { resolvedConfig = _resolvedConfig; }, async load(id, opts) { const parsedId = parseAstroRequest(id); const query = parsedId.query; if (!query.astro) { return null; } const filename = normalizePath(normalizeFilename(parsedId.filename, config.root)); const compileResult = getCachedCompileResult(config, filename); if (!compileResult) { return null; } switch (query.type) { case "style": { if (typeof query.index === "undefined") { throw new Error(`Requests for Astro CSS must include an index.`); } const code = compileResult.css[query.index]; if (!code) { throw new Error(`No Astro CSS at index ${query.index}`); } return { code, meta: { vite: { isSelfAccepting: true } } }; } case "script": { if (typeof query.index === "undefined") { throw new Error(`Requests for hoisted scripts must include an index`); } if (opts == null ? void 0 : opts.ssr) { return { code: `/* client hoisted script, empty in SSR: ${id} */` }; } const hoistedScript = compileResult.scripts[query.index]; if (!hoistedScript) { throw new Error(`No hoisted script at index ${query.index}`); } if (hoistedScript.type === "external") { const src = hoistedScript.src; if (src.startsWith("/") && !isBrowserPath(src)) { const publicDir = config.publicDir.pathname.replace(/\/$/, "").split("/").pop() + "/"; throw new Error( `