UNPKG

everything-dev

Version:

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

1 lines • 4.39 kB
{"version":3,"file":"head.cjs","names":["entryUrls"],"sources":["../../src/ui/head.ts"],"sourcesContent":["import type { ClientRuntimeConfig } from \"../types\";\nimport { type EntrySlot, entryUrls } from \"./slot\";\nimport type { 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\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,KAFeA,uBAAU,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;AAEA,SAAgB,gBAAwB;CACtC,OAAO;;;;;;IAML,KAAK;AACT"}