UNPKG

everything-dev

Version:

A consolidated product package for building Module Federation apps with oRPC APIs.

1 lines • 6.54 kB
{"version":3,"file":"head.mjs","names":[],"sources":["../../src/ui/head.ts"],"sourcesContent":["import type { ClientRuntimeConfig } from \"../types\";\nimport { type EntrySlot, entryUrls } from \"./slot\";\nimport type { HeadLink, HeadScript } from \"./types\";\n\nexport interface RemoteScriptsOptions {\n runtimeConfig?: Partial<ClientRuntimeConfig>;\n containerName?: string;\n hydratePath?: string;\n integrity?: string;\n cspNonce?: string;\n}\n\nexport function getThemeInitScript(): HeadScript {\n return {\n children:\n \"(function(){var t=localStorage.getItem('theme');if(t==='dark'){document.documentElement.classList.add('dark');}})();\",\n };\n}\n\nfunction escapeJsonForScript(value: unknown): string {\n return JSON.stringify(value ?? null)\n .replace(/<\\/script/gi, \"<\\\\/script\")\n .replace(/\\u2028/g, \"\\\\u2028\")\n .replace(/\\u2029/g, \"\\\\u2029\");\n}\n\nexport function getHydrateScript(\n runtimeConfig: Partial<ClientRuntimeConfig> | undefined,\n containerName = \"ui\",\n hydratePath = \"./Hydrate\",\n cspNonce?: string,\n): HeadScript {\n return {\n children: `\n window.__CSP_NONCE__=${cspNonce != null ? escapeJsonForScript(cspNonce) : null};\n window.__RUNTIME_CONFIG__=${escapeJsonForScript(runtimeConfig)};\n function __hydrate(){\n if (window.__EVERYTHING_DEV_HYDRATE_PROMISE__) {\n console.warn('[Hydrate] Already in progress, skipping duplicate call');\n return;\n }\n var container = window['${containerName}'];\n if (!container) {\n console.warn('[Hydrate] Container not ready yet, waiting...');\n window.__hydrateRetry = window.__hydrateRetry || 0;\n if (window.__hydrateRetry < 10) {\n window.__hydrateRetry++;\n setTimeout(__hydrate, 100);\n return;\n }\n console.error('[Hydrate] Container not found after 10 retries');\n return;\n }\n console.log('[Hydrate] Container available, starting init...');\n container.init({}).then(function(){\n return container.get('${hydratePath}');\n }).then(function(mod){\n return mod().hydrate();\n }).catch(function(e){\n console.error('[Hydrate] Failed:', e);\n window.__EVERYTHING_DEV_HYDRATE_PROMISE__ = undefined;\n });\n }\n if(document.readyState==='loading'){document.addEventListener('DOMContentLoaded',__hydrate);}else{__hydrate();}\n \t\t`.trim(),\n };\n}\n\nexport function getRemoteScripts(options: RemoteScriptsOptions): HeadScript[] {\n const { runtimeConfig, containerName, hydratePath, integrity, cspNonce } = options;\n const assetsUrl = runtimeConfig?.assetsUrl?.replace(/\\/$/, \"\");\n const slot: EntrySlot = {\n name: \"ui\",\n url: assetsUrl,\n entryUrl: runtimeConfig?.ui?.entryUrl,\n integrity,\n };\n const entrySrc = entryUrls(slot, runtimeConfig?.env ?? \"production\").web;\n const entryScript: HeadScript = {\n src: entrySrc,\n crossOrigin: \"anonymous\",\n };\n if (integrity) {\n entryScript.integrity = integrity;\n }\n\n return [entryScript, getHydrateScript(runtimeConfig, containerName, hydratePath, cspNonce)];\n}\n\n/**\n * Preload hints for the composed plugin-UI remotes (`ui.compose`). Without\n * them each remote entry is discovered only after the core remoteEntry\n * executes and hydrate registers remotes — serializing\n * document → remoteEntry → manifest → remoteEntry. Attributes must match\n * how the MF runtime loads these resources (plain no-cors `<script>` for\n * entries, cors `fetch()` for manifests) or the browser refetches.\n *\n * Server-only: the client render (hydration re-render of head content, or\n * the client-shell fallback) runs after the hydrate compose has already\n * fetched the manifests and entries — re-injecting the hints then produces\n * preloads nothing will ever consume (\"preloaded but not used\" console\n * warnings on every load).\n */\nexport function getRemotePreloadLinks(\n runtimeConfig: Partial<ClientRuntimeConfig> | undefined,\n): HeadLink[] {\n if (typeof window !== \"undefined\") return [];\n const remotes = runtimeConfig?.ui?.compose?.remotes ?? [];\n const seen = new Set<string>();\n const links: HeadLink[] = [];\n for (const remote of remotes) {\n if (remote.manifestUrl && !seen.has(remote.manifestUrl)) {\n seen.add(remote.manifestUrl);\n links.push({\n rel: \"preload\",\n href: remote.manifestUrl,\n as: \"fetch\",\n crossOrigin: \"anonymous\",\n });\n }\n if (remote.entry && !seen.has(remote.entry)) {\n seen.add(remote.entry);\n links.push({\n rel: \"preload\",\n href: remote.entry,\n as: \"script\",\n fetchPriority: \"high\",\n });\n }\n }\n return links;\n}\n\nexport function getBaseStyles(): string {\n return `\n*, *::before, *::after { box-sizing: border-box; }\nhtml { height: 100%; height: 100dvh; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; color-scheme: light dark; }\nbody { min-height: 100%; min-height: 100dvh; margin: 0; background-color: var(--background); color: var(--foreground); -webkit-tap-highlight-color: transparent; touch-action: manipulation; }\n#root { min-height: 100vh; }\n@supports (min-height: 100dvh) { #root { min-height: 100dvh; } }\n `.trim();\n}\n"],"mappings":";;;AAYA,SAAgB,qBAAiC;CAC/C,OAAO,EACL,UACE,uHACJ;AACF;AAEA,SAAS,oBAAoB,OAAwB;CACnD,OAAO,KAAK,UAAU,SAAS,IAAI,CAAC,CACjC,QAAQ,eAAe,YAAY,CAAC,CACpC,QAAQ,WAAW,SAAS,CAAC,CAC7B,QAAQ,WAAW,SAAS;AACjC;AAEA,SAAgB,iBACd,eACA,gBAAgB,MAChB,cAAc,aACd,UACY;CACZ,OAAO,EACL,UAAU;wBACU,YAAY,OAAO,oBAAoB,QAAQ,IAAI,KAAK;6BACnD,oBAAoB,aAAa,EAAE;;;;;;6BAMnC,cAAc;;;;;;;;;;;;;;4BAcf,YAAY;;;;;;;;;KASnC,KAAK,EACR;AACF;AAEA,SAAgB,iBAAiB,SAA6C;CAC5E,MAAM,EAAE,eAAe,eAAe,aAAa,WAAW,aAAa;CAE3E,MAAM,OAAkB;EACtB,MAAM;EACN,KAHgB,eAAe,WAAW,QAAQ,OAAO,EAAE;EAI3D,UAAU,eAAe,IAAI;EAC7B;CACF;CAEA,MAAM,cAA0B;EAC9B,KAFe,UAAU,MAAM,eAAe,OAAO,YAAY,CAAC,CAAC;EAGnE,aAAa;CACf;CACA,IAAI,WACF,YAAY,YAAY;CAG1B,OAAO,CAAC,aAAa,iBAAiB,eAAe,eAAe,aAAa,QAAQ,CAAC;AAC5F;;;;;;;;;;;;;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