The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import type { Plugin, PluginOption } from "vite"; |
| 3 | import { loadProjectModule } from "../../core/Loader.ts"; |
| 4 | import { loadVinextModule } from "../Modules.ts"; |
| 5 | |
| 6 | export type VinextCacheKind = "redis" | "s3" | "kv"; |
| 7 | |
| 8 | export interface VinextCacheOptions { |
| 9 | data?: { adapter: string; options?: Record<string, unknown> }; |
| 10 | cdn?: { adapter: string; options?: Record<string, unknown> }; |
| 11 | } |
| 12 | |
| 13 | interface CacheModule { |
| 14 | VIRTUAL_CACHE_ADAPTERS: string; |
| 15 | VIRTUAL_CDN_CACHE_ADAPTER: string; |
| 16 | findVinextCacheConfigInPlugins( |
| 17 | plugins: PluginOption[] | undefined, |
| 18 | ): Promise<VinextCacheOptions | null>; |
| 19 | generateCacheAdaptersModule(cache: VinextCacheOptions): string; |
| 20 | generateCdnCacheAdapterModule(cache: VinextCacheOptions): string; |
| 21 | } |
| 22 | |
| 23 | /** The deploy target owns data caching; native CDN configuration is preserved. */ |
| 24 | export const makeVinextCachePlugin = Effect.fn(function* (root: string, kind: VinextCacheKind) { |
| 25 | const native = yield* loadVinextModule<CacheModule>(root, "cache/cache-adapters-virtual.js"); |
| 26 | const { kvAdapter, redisAdapter, s3Adapter } = yield* loadProjectModule< |
| 27 | typeof import("./index.ts") |
| 28 | >(root, "@alchemy.run/frontend-frameworks/vinext/cache"); |
| 29 | const data = kind === "kv" ? kvAdapter() : kind === "s3" ? s3Adapter() : redisAdapter(); |
| 30 | let cache: VinextCacheOptions = { data }; |
| 31 | const adaptersId = "\0alchemy:vinext-cache-adapters"; |
| 32 | const cdnId = "\0alchemy:vinext-cdn-cache-adapter"; |
| 33 | return { |
| 34 | name: "alchemy:vinext-cache", |
| 35 | enforce: "pre", |
| 36 | async configResolved(config) { |
| 37 | const configured = await native.findVinextCacheConfigInPlugins([...config.plugins]); |
| 38 | if (configured?.data && configured.data.adapter !== data.adapter) { |
| 39 | config.logger.warn( |
| 40 | "[alchemy] Website.Vinext owns the data-cache adapter; the adapter in vite.config is overridden.", |
| 41 | ); |
| 42 | } |
| 43 | cache = { ...configured, data }; |
| 44 | }, |
| 45 | resolveId: { |
| 46 | order: "pre", |
| 47 | handler(id) { |
| 48 | if (id === native.VIRTUAL_CACHE_ADAPTERS) return adaptersId; |
| 49 | if (id === native.VIRTUAL_CDN_CACHE_ADAPTER) return cdnId; |
| 50 | }, |
| 51 | }, |
| 52 | load(id) { |
| 53 | if (id === adaptersId) return native.generateCacheAdaptersModule(cache); |
| 54 | if (id === cdnId) return native.generateCdnCacheAdapterModule(cache); |
| 55 | }, |
| 56 | } satisfies Plugin; |
| 57 | }); |