| 1 | // Alchemy modifications are licensed under Apache-2.0. |
| 2 | // This file includes third-party code; see /THIRD_PARTY_LICENSES.md. |
| 3 | /** |
| 4 | * Vendored from `@astrojs/cloudflare` v14.1.3 (`src/vite-plugin-config.ts`, |
| 5 | * which the published package does not export). |
| 6 | * |
| 7 | * Serializes adapter settings into the `virtual:astro-cloudflare:config` |
| 8 | * virtual module consumed by the vendored runtime handler. `isPrerender` is |
| 9 | * environment-dependent: it is only `true` when the module is loaded by the |
| 10 | * (build-only) `prerender` environment. |
| 11 | */ |
| 12 | import type * as vite from "vite"; |
| 13 | |
| 14 | const VIRTUAL_CONFIG_ID = "virtual:astro-cloudflare:config"; |
| 15 | const RESOLVED_VIRTUAL_CONFIG_ID = "\0" + VIRTUAL_CONFIG_ID; |
| 16 | |
| 17 | export interface CompileImageConfig { |
| 18 | base: string; |
| 19 | assetsPrefix: string | undefined; |
| 20 | imageServiceEntrypoint: string; |
| 21 | buildAssets: string; |
| 22 | } |
| 23 | |
| 24 | export interface AstroCloudflareConfig { |
| 25 | sessionKVBindingName: string; |
| 26 | compileImageConfig: CompileImageConfig | null; |
| 27 | /** |
| 28 | * True when the user has configured the Cloudflare cache provider. Not |
| 29 | * currently supported by this package; always `false`. |
| 30 | */ |
| 31 | cacheProviderEnabled: boolean; |
| 32 | } |
| 33 | |
| 34 | export function createConfigPlugin(config: AstroCloudflareConfig): vite.Plugin { |
| 35 | return { |
| 36 | name: VIRTUAL_CONFIG_ID, |
| 37 | resolveId: { |
| 38 | filter: { id: new RegExp(`^${VIRTUAL_CONFIG_ID}$`) }, |
| 39 | handler() { |
| 40 | return RESOLVED_VIRTUAL_CONFIG_ID; |
| 41 | }, |
| 42 | }, |
| 43 | load: { |
| 44 | filter: { |
| 45 | id: new RegExp(`^${RESOLVED_VIRTUAL_CONFIG_ID.replace("\0", "\\0")}$`), |
| 46 | }, |
| 47 | handler() { |
| 48 | return [ |
| 49 | ...Object.entries(config).map( |
| 50 | ([k, v]) => `export const ${k} = ${JSON.stringify(v)};`, |
| 51 | ), |
| 52 | `export const isPrerender = ${this.environment?.name === "prerender"};`, |
| 53 | ].join("\n"); |
| 54 | }, |
| 55 | }, |
| 56 | }; |
| 57 | } |