| 1 | /** |
| 2 | * Cloudflare Vite plugin options for a vinext Worker. The injected |
| 3 | * `@alchemy.run/cloudflare-runtime/vite` stack registers |
| 4 | * `vite-plugin-cloudflare:alchemy` (vinext matches the prefix) and |
| 5 | * no-ops any official `@cloudflare/vite-plugin` already in the config. |
| 6 | */ |
| 7 | import type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite"; |
| 8 | import * as NodePath from "node:path"; |
| 9 | |
| 10 | export type { CloudflareVitePluginOptions } from "@alchemy.run/cloudflare-runtime/vite"; |
| 11 | |
| 12 | /** Set on the process that injects Alchemy's Cloudflare Vite plugin. */ |
| 13 | export const ALCHEMY_CLOUDFLARE_VITE_INJECTED = |
| 14 | "ALCHEMY_CLOUDFLARE_VITE_INJECTED"; |
| 15 | |
| 16 | /** vinext App Router: RSC worker + SSR child environment. */ |
| 17 | export const VINEXT_VITE_ENVIRONMENTS = { |
| 18 | entry: "rsc", |
| 19 | children: ["ssr"], |
| 20 | } as const; |
| 21 | |
| 22 | /** Official vinext Cloudflare worker entry. */ |
| 23 | export const DEFAULT_WORKER_ENTRY = "worker/index.ts"; |
| 24 | |
| 25 | export interface VinextPluginOptionsInputs { |
| 26 | readonly root: string; |
| 27 | readonly main?: string | undefined; |
| 28 | readonly compatibilityDate: string; |
| 29 | readonly compatibilityFlags: ReadonlyArray<string>; |
| 30 | readonly viteEnvironments?: { |
| 31 | readonly entry?: string; |
| 32 | readonly children?: ReadonlyArray<string>; |
| 33 | }; |
| 34 | readonly worker?: CloudflareVitePluginOptions["worker"]; |
| 35 | readonly context?: CloudflareVitePluginOptions["context"]; |
| 36 | } |
| 37 | |
| 38 | export const makeVinextPluginOptions = ( |
| 39 | inputs: VinextPluginOptionsInputs, |
| 40 | ): CloudflareVitePluginOptions => { |
| 41 | const main = inputs.main ?? DEFAULT_WORKER_ENTRY; |
| 42 | return { |
| 43 | main: NodePath.isAbsolute(main) |
| 44 | ? main |
| 45 | : NodePath.resolve(inputs.root, main), |
| 46 | viteEnvironments: { |
| 47 | entry: inputs.viteEnvironments?.entry ?? VINEXT_VITE_ENVIRONMENTS.entry, |
| 48 | children: [ |
| 49 | ...(inputs.viteEnvironments?.children ?? |
| 50 | VINEXT_VITE_ENVIRONMENTS.children), |
| 51 | ], |
| 52 | }, |
| 53 | compatibilityDate: inputs.compatibilityDate, |
| 54 | compatibilityFlags: [...inputs.compatibilityFlags], |
| 55 | ...(inputs.worker !== undefined ? { worker: inputs.worker } : {}), |
| 56 | ...(inputs.context !== undefined ? { context: inputs.context } : {}), |
| 57 | }; |
| 58 | }; |