import MagicString from "magic-string"; import { fileURLToPath } from "node:url"; import { loadEnv } from "vite"; function getPrivateEnv(viteConfig, astroConfig) { let envPrefixes = ["PUBLIC_"]; if (viteConfig.envPrefix) { envPrefixes = Array.isArray(viteConfig.envPrefix) ? viteConfig.envPrefix : [viteConfig.envPrefix]; } const fullEnv = loadEnv( viteConfig.mode, viteConfig.envDir ?? fileURLToPath(astroConfig.root), "" ); const privateEnv = {}; for (const key in fullEnv) { if (envPrefixes.every((prefix) => !key.startsWith(prefix))) { if (typeof process.env[key] !== "undefined") { let value = process.env[key]; if (typeof value !== "string") { value = `${value}`; } if (value === "0" || value === "1" || value === "true" || value === "false") { privateEnv[key] = value; } else { privateEnv[key] = `process.env.${key}`; } } else { privateEnv[key] = JSON.stringify(fullEnv[key]); } } } privateEnv.SITE = astroConfig.site ? JSON.stringify(astroConfig.site) : "undefined"; privateEnv.SSR = JSON.stringify(true); privateEnv.BASE_URL = astroConfig.base ? JSON.stringify(astroConfig.base) : "undefined"; privateEnv.ASSETS_PREFIX = astroConfig.build.assetsPrefix ? JSON.stringify(astroConfig.build.assetsPrefix) : "undefined"; return privateEnv; } function getReferencedPrivateKeys(source, privateEnv) { const references = /* @__PURE__ */ new Set(); for (const key in privateEnv) { if (source.includes(key)) { references.add(key); } } return references; } function envVitePlugin({ settings }) { let privateEnv; let viteConfig; const { config: astroConfig } = settings; return { name: "astro:vite-plugin-env", enforce: "pre", config() { return { define: { "import.meta.env.BASE_URL": astroConfig.base ? JSON.stringify(astroConfig.base) : "undefined", "import.meta.env.ASSETS_PREFIX": astroConfig.build.assetsPrefix ? JSON.stringify(astroConfig.build.assetsPrefix) : "undefined" } }; }, configResolved(resolvedConfig) { viteConfig = resolvedConfig; }, async transform(source, id, options) { if (!(options == null ? void 0 : options.ssr) || !source.includes("import.meta.env")) { return; } let s; const pattern = new RegExp( // Do not allow preceding '.', but do allow preceding '...' for spread operations `(?