sailfork / t3code-github / .repos/ alchemy-effect/ packages/ frontend-frameworks/ src/ astro/ config-plugin.ts

// Alchemy modifications are licensed under Apache-2.0.
// This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
/**
 * Vendored from `@astrojs/cloudflare` v14.4.2 (`src/vite-plugin-config.ts`,
 * which the published package does not export).
 *
 * Serializes adapter settings into the `virtual:astro-cloudflare:config`
 * virtual module consumed by the vendored runtime handler. `isPrerender` is
 * environment-dependent: it is only `true` when the module is loaded by the
 * (build-only) `prerender` environment.
 */
import type * as vite from "vite";

const VIRTUAL_CONFIG_ID = "virtual:astro-cloudflare:config";
const RESOLVED_VIRTUAL_CONFIG_ID = "\0" + VIRTUAL_CONFIG_ID;

/**
 * The prerender-only render-scope installer (upstream's
 * `src/utils/prerender-scope.ts`), inlined into the virtual module: installs
 * an AsyncLocalStorage-backed render scope so each concurrent prerender
 * request collects its metadata (content entries, images) in its own store.
 * `installRenderScope` is first-wins, so calling it per request is
 * idempotent. Inlined rather than a separate runtime module because the
 * runtime is bundled (tsdown), so there is no stable file to import.
 */
const LOAD_PRERENDER_SCOPE = `() => Promise.all([import("node:async_hooks"), import("astro/app")]).then(([{ AsyncLocalStorage }, { installRenderScope }]) => ({ ensurePrerenderScope: () => { installRenderScope(new AsyncLocalStorage()); } }))`;

export interface CompileImageConfig {
  transformWithBinding: boolean;
}

export interface AstroCloudflareConfig {
  sessionKVBindingName: string;
  compileImageConfig: CompileImageConfig | null;
  /**
   * True when the Astro config uses the Cloudflare cache provider
   * (`cacheCloudflare()`). The handler then defaults responses without
   * cache intent to `Cloudflare-CDN-Cache-Control: no-store`.
   */
  cacheProviderEnabled: boolean;
}

export function createConfigPlugin(config: AstroCloudflareConfig): vite.Plugin {
  return {
    name: VIRTUAL_CONFIG_ID,
    resolveId: {
      filter: { id: new RegExp(`^${VIRTUAL_CONFIG_ID}

	
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		
	.repos/alchemy-effect/packages/frontend-frameworks/src/astro/config-plugin.ts at main · sailfork/t3code-github · Sailfork
	
		
) }, handler() { return RESOLVED_VIRTUAL_CONFIG_ID; }, }, load: { filter: { id: new RegExp(`^${RESOLVED_VIRTUAL_CONFIG_ID.replace("\0", "\\0")} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/config-plugin.ts at main · sailfork/t3code-github · Sailfork
), }, handler() { const isPrerender = this.environment?.name === "prerender"; return [ ...Object.entries(config).map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`), `export const isPrerender = ${isPrerender};`, // The import edge to the render-scope installer — the one module with // a `node:async_hooks` reference — must only exist in the prerender // worker's module graph, so the thunk is generated per environment. `export const loadPrerenderScope = ${isPrerender ? LOAD_PRERENDER_SCOPE : "undefined"};`, ].join("\n"); }, }, }; }
.repos/alchemy-effect/packages/frontend-frameworks/src/astro/config-plugin.ts at main · sailfork/t3code-github · Sailfork
) },\n handler() {\n return RESOLVED_VIRTUAL_CONFIG_ID;\n },\n },\n load: {\n filter: {\n id: new RegExp(`^${RESOLVED_VIRTUAL_CONFIG_ID.replace(\"\\0\", \"\\\\0\")} .repos/alchemy-effect/packages/frontend-frameworks/src/astro/config-plugin.ts at main · sailfork/t3code-github · Sailfork
),\n },\n handler() {\n const isPrerender = this.environment?.name === \"prerender\";\n return [\n ...Object.entries(config).map(([k, v]) => `export const ${k} = ${JSON.stringify(v)};`),\n `export const isPrerender = ${isPrerender};`,\n // The import edge to the render-scope installer — the one module with\n // a `node:async_hooks` reference — must only exist in the prerender\n // worker's module graph, so the thunk is generated per environment.\n `export const loadPrerenderScope = ${isPrerender ? LOAD_PRERENDER_SCOPE : \"undefined\"};`,\n ].join(\"\\n\");\n },\n },\n };\n}\n","preview":null,"history":null,"version":"fd1025b78686cb8828dccd225e2bbb4ccd975407"},"/_session?shell=1":null}