cloudflare/kumo
Publicmirrored fromhttps://github.com/cloudflare/kumoAvailable
packages/kumo-docs-astro/astro.config.mjs
90lines · modecode
| 1 | // @ts-check |
| 2 | import { defineConfig } from "astro/config"; |
| 3 | import react from "@astrojs/react"; |
| 4 | import tailwindcss from "@tailwindcss/vite"; |
| 5 | import { execSync } from "child_process"; |
| 6 | import { readFileSync } from "fs"; |
| 7 | import { resolve } from "path"; |
| 8 | import { fileURLToPath } from "url"; |
| 9 | import { kumoColorsPlugin } from "./src/lib/vite-plugin-kumo-colors.js"; |
| 10 | import { kumoRegistryPlugin } from "./src/lib/vite-plugin-kumo-registry.js"; |
| 11 | import { kumoHmrPlugin } from "./src/lib/vite-plugin-kumo-hmr.js"; |
| 12 | |
| 13 | const __dirname = fileURLToPath(new URL(".", import.meta.url)); |
| 14 | |
| 15 | function getBuildInfo() { |
| 16 | // Read version from the main kumo package |
| 17 | const kumoPkg = JSON.parse( |
| 18 | readFileSync(resolve(__dirname, "../kumo/package.json"), "utf-8"), |
| 19 | ); |
| 20 | |
| 21 | // Read version from the docs-astro package |
| 22 | const docsPkg = JSON.parse( |
| 23 | readFileSync(resolve(__dirname, "package.json"), "utf-8"), |
| 24 | ); |
| 25 | |
| 26 | let commitHash = "unknown"; |
| 27 | let commitDate = "unknown"; |
| 28 | let branch = "unknown"; |
| 29 | |
| 30 | try { |
| 31 | commitHash = execSync("git rev-parse --short HEAD", { |
| 32 | encoding: "utf-8", |
| 33 | }).trim(); |
| 34 | commitDate = execSync("git log -1 --format=%cI", { |
| 35 | encoding: "utf-8", |
| 36 | }).trim(); |
| 37 | branch = execSync("git rev-parse --abbrev-ref HEAD", { |
| 38 | encoding: "utf-8", |
| 39 | }).trim(); |
| 40 | } catch (error) { |
| 41 | console.warn( |
| 42 | "[kumo-docs-astro] Git info unavailable during build:", |
| 43 | error instanceof Error ? error.message : error, |
| 44 | ); |
| 45 | console.warn( |
| 46 | "[kumo-docs-astro] This may happen with shallow clones. Set GIT_DEPTH=0 or fetch-depth: 0 in CI.", |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | return { |
| 51 | kumoVersion: kumoPkg.version, |
| 52 | docsVersion: docsPkg.version, |
| 53 | commitHash, |
| 54 | commitDate, |
| 55 | branch, |
| 56 | buildDate: new Date().toISOString(), |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | const buildInfo = getBuildInfo(); |
| 61 | |
| 62 | // Detect dev mode: `astro dev` sets this in process.argv |
| 63 | const isDev = process.argv.includes("dev"); |
| 64 | |
| 65 | // https://astro.build/config |
| 66 | export default defineConfig({ |
| 67 | integrations: [react()], |
| 68 | vite: { |
| 69 | plugins: [ |
| 70 | // @ts-expect-error - Vite version mismatch between Astro and @tailwindcss/vite |
| 71 | tailwindcss(), |
| 72 | kumoColorsPlugin(), |
| 73 | kumoRegistryPlugin(), |
| 74 | // In dev mode, resolve @cloudflare/kumo imports to raw source files |
| 75 | // for instant HMR. In production builds, the normal package.json |
| 76 | // exports (dist/) are used — preserving the real consumer experience. |
| 77 | ...(isDev ? [kumoHmrPlugin()] : []), |
| 78 | ], |
| 79 | |
| 80 | define: { |
| 81 | __KUMO_VERSION__: JSON.stringify(buildInfo.kumoVersion), |
| 82 | __DOCS_VERSION__: JSON.stringify(buildInfo.docsVersion), |
| 83 | __BUILD_VERSION__: JSON.stringify(buildInfo.kumoVersion), // Alias for backwards compatibility |
| 84 | __BUILD_COMMIT__: JSON.stringify(buildInfo.commitHash), |
| 85 | __BUILD_COMMIT_DATE__: JSON.stringify(buildInfo.commitDate), |
| 86 | __BUILD_BRANCH__: JSON.stringify(buildInfo.branch), |
| 87 | __BUILD_DATE__: JSON.stringify(buildInfo.buildDate), |
| 88 | }, |
| 89 | }, |
| 90 | }); |
| 91 | |