{"version":3,"file":"server.cjs","names":["React","React"],"sources":["../src/script.tsx","../src/context.ts","../src/server-provider.tsx","../src/server.ts"],"sourcesContent":["import React from 'react'\nimport { serializePublicConfig } from '@yanuaraditia/config'\nimport type { RuntimeConfigInput } from '@yanuaraditia/config'\n\nexport interface RuntimeConfigScriptProps {\n  /**\n   * The runtime config whose **public** portion will be serialised and\n   * embedded in the page as `window.__RUNTIME_CONFIG__`.\n   *\n   * Pass the value from your root loader / `getRuntimeConfig()`.\n   */\n  config: RuntimeConfigInput\n  /**\n   * HTML `nonce` attribute for strict Content-Security-Policy environments.\n   */\n  nonce?: string\n}\n\n/**\n * Renders an inline `<script>` tag that seeds `window.__RUNTIME_CONFIG__`\n * with the **public** portion of your runtime config.\n *\n * Place this inside `<head>` in your root layout.  On the server it outputs\n * a real `<script>` tag; on the client it re-uses the already-rendered tag\n * (safe for hydration).\n *\n * @example\n * ```tsx\n * // app/root.tsx — React Router v7 SSR\n * import { getRuntimeConfig } from '@yanuaraditia/config-react/server'\n * import { RuntimeConfigScript } from '@yanuaraditia/config-react'\n *\n * export async function loader() {\n *   return { runtimeConfig: getRuntimeConfig() }\n * }\n *\n * export default function Root() {\n *   const { runtimeConfig } = useLoaderData<typeof loader>()\n *   return (\n *     <html>\n *       <head>\n *         <RuntimeConfigScript config={runtimeConfig} />\n *       </head>\n *       <body>…</body>\n *     </html>\n *   )\n * }\n * ```\n */\nexport function RuntimeConfigScript({ config, nonce }: RuntimeConfigScriptProps) {\n  const json = serializePublicConfig(config)\n  return React.createElement('script', {\n    nonce,\n    // dangerouslySetInnerHTML is required for inline scripts in React.\n    // The value is JSON-encoded and HTML-escaped in serializePublicConfig.\n    dangerouslySetInnerHTML: {\n      __html: `window.__RUNTIME_CONFIG__=${json}`,\n    },\n  })\n}\n","import { createContext } from 'react'\nimport type { RuntimeConfig, ClientRuntimeConfig } from '@yanuaraditia/config'\n\n// On the server: RuntimeConfig (public + private).\n// On the client: ClientRuntimeConfig (public only), sourced from\n//   window.__RUNTIME_CONFIG__ or an SSR-injected provider.\nexport type AnyRuntimeConfig = RuntimeConfig | ClientRuntimeConfig\n\nexport const RuntimeConfigContext = createContext<AnyRuntimeConfig | null>(null)\nRuntimeConfigContext.displayName = 'RuntimeConfigContext'\n","import React from 'react'\nimport { RuntimeConfigScript } from './script'\nimport { RuntimeConfigContext } from './context'\nimport { useRuntimeConfig } from './server'\nimport { readClientConfig } from '@yanuaraditia/config'\nimport type { AnyRuntimeConfig } from './context'\n\nexport interface SSRRuntimeConfigProviderProps {\n  children: React.ReactNode\n  /** Optional nonce for Content-Security-Policy. */\n  nonce?: string\n}\n\n/**\n * SSR-aware RuntimeConfigProvider for React Router v7 / Remix apps.\n *\n * Drop it into your root layout — no props, no `useRuntimeConfig`, no\n * `RuntimeConfigScript`. It handles everything automatically:\n *\n * - **Server render**: reads from `useRuntimeConfig()` (populated when you\n *   `import '#runtime-config'` in `entry.server.tsx`) and injects\n *   `window.__RUNTIME_CONFIG__` via an inline `<script>` tag.\n * - **Client hydration / SPA navigation**: reads the already-injected\n *   `window.__RUNTIME_CONFIG__` and re-renders the same script so React\n *   hydration stays in sync.\n *\n * @example\n * ```tsx\n * // app/root.tsx\n * import { RuntimeConfigProvider } from '@yanuaraditia/config-react/server'\n *\n * export default function App() {\n *   return (\n *     <html lang=\"en\">\n *       <head>…</head>\n *       <body>\n *         <RuntimeConfigProvider>\n *           <Outlet />\n *         </RuntimeConfigProvider>\n *       </body>\n *     </html>\n *   )\n * }\n * ```\n */\nexport function RuntimeConfigProvider({ children, nonce }: SSRRuntimeConfigProviderProps) {\n  // Server: module-level state set by `import '#runtime-config'` in entry.server.tsx\n  // Client: window.__RUNTIME_CONFIG__ injected by the SSR <script> on first load\n  const raw: AnyRuntimeConfig =\n    typeof window === 'undefined'\n      ? (useRuntimeConfig() as unknown as AnyRuntimeConfig)\n      : (readClientConfig() ?? { public: {} })\n\n  const publicOnly: AnyRuntimeConfig = { public: (raw as Record<string, unknown>).public ?? {} }\n\n  return React.createElement(\n    RuntimeConfigContext.Provider,\n    { value: publicOnly },\n    // Inline script seeds window.__RUNTIME_CONFIG__ so client components and\n    // post-hydration reads always find the value. Same content on server+client\n    // → no hydration mismatch.\n    React.createElement(RuntimeConfigScript, { config: raw, nonce }),\n    children,\n  )\n}\n","/**\n * Server-side runtime config utilities for React Router v7 (and any SSR framework).\n *\n * Import these **only in server code** (loaders, actions, server middleware).\n *\n * `useRuntimeConfig()` returns the **full** config (public + private keys) on the\n * server. Pass it to `<RuntimeConfigProvider>` — the provider automatically strips\n * private keys so they are never accessible in components.\n *\n * @example\n * ```ts\n * // app/root.tsx\n * import { useRuntimeConfig } from '@yanuaraditia/config-react/server'\n *\n * export async function loader() {\n *   const config = useRuntimeConfig()  // full config — server only\n *   return { runtimeConfig: config }   // provider strips private keys for client\n * }\n * ```\n */\n\nimport { applyEnvOverrides } from '@yanuaraditia/config'\nimport type { RuntimeConfigInput, RuntimeConfig } from '@yanuaraditia/config'\n\n// Use globalThis so state is shared across module-instance duplicates\n// (e.g. the bundled #runtime-config SSR module + user's direct import\n// can resolve to different module instances under Vite's SSR bundler).\nconst STATE_KEY = '__YANUARADITIA_CONFIG_RUNTIME_STATE__'\ninterface RuntimeState {\n  baseConfig: RuntimeConfigInput | null\n  envPrefix: string\n}\nconst globalRef = globalThis as unknown as Record<string, RuntimeState | undefined>\nfunction getState(): RuntimeState {\n  let state = globalRef[STATE_KEY]\n  if (!state) {\n    state = { baseConfig: null, envPrefix: 'RUNTIME_' }\n    globalRef[STATE_KEY] = state\n  }\n  return state\n}\n\n/**\n * Register the base (default) config.  Call this once in your app's entry\n * point (server entry or root loader) before using `useRuntimeConfig()`.\n *\n * The base config is usually the output of `defineRuntimeConfig()`.\n *\n * @example\n * ```ts\n * // app/entry.server.tsx  OR  app/root.tsx (top of file)\n * import baseConfig from '~/runtime.config'\n * import { setBaseRuntimeConfig } from '@yanuaraditia/config-react/server'\n *\n * setBaseRuntimeConfig(baseConfig, { envPrefix: 'RUNTIME_' })\n * ```\n */\nexport function setBaseRuntimeConfig(\n  config: RuntimeConfigInput,\n  options?: { envPrefix?: string },\n): void {\n  const state = getState()\n  state.baseConfig = config\n  if (options?.envPrefix) state.envPrefix = options.envPrefix\n}\n\n/**\n * Get the full runtime config with environment-variable overrides applied.\n *\n * Use this in loaders, actions, or any server-side code — same API as the\n * client-side `useRuntimeConfig()` hook.\n *\n * Returns `PrivateRuntimeConfig & { public: PublicRuntimeConfig }`.\n */\nexport function useRuntimeConfig(): RuntimeConfig {\n  const state = getState()\n  if (!state.baseConfig) {\n    return { public: {} } as unknown as RuntimeConfig\n  }\n  return applyEnvOverrides(\n    state.baseConfig,\n    process.env as Record<string, string | undefined>,\n    state.envPrefix,\n  ) as unknown as RuntimeConfig\n}\n\n/** @deprecated Use `useRuntimeConfig()` instead. */\nexport const getRuntimeConfig = useRuntimeConfig\n\n/**\n * Create a self-contained getter that embeds the base config.\n * Useful when you don't want a shared module-level state.\n *\n * @example\n * ```ts\n * import baseConfig from '~/runtime.config'\n * import { createGetRuntimeConfig } from '@yanuaraditia/config-react/server'\n *\n * export const useRuntimeConfig = createGetRuntimeConfig(baseConfig)\n * ```\n */\nexport function createGetRuntimeConfig<T extends RuntimeConfigInput>(\n  baseConfig: T,\n  options?: { envPrefix?: string },\n): () => T {\n  const prefix = options?.envPrefix ?? 'RUNTIME_'\n  return () =>\n    applyEnvOverrides(\n      baseConfig,\n      process.env as Record<string, string | undefined>,\n      prefix,\n    ) as T\n}\n\nexport { RuntimeConfigProvider } from './server-provider'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiDA,SAAgB,oBAAoB,EAAE,QAAQ,SAAmC;CAC/E,MAAM,QAAA,GAAA,qBAAA,uBAA6B,OAAO;AAC1C,QAAOA,MAAAA,QAAM,cAAc,UAAU;EACnC;EAGA,yBAAyB,EACvB,QAAQ,6BAA6B,QACtC;EACF,CAAC;;;;AClD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