UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

91 lines (90 loc) 3.35 kB
import "./setup.mjs"; import { AppRegistry } from "react-native-web"; import { Root } from "./Root.mjs"; import { resolveClientLoader } from "./clientLoaderResolver.mjs"; import { render } from "./render.mjs"; import { renderToString } from "./server-render.mjs"; import ReactDOMServer from "react-dom/server.browser"; import { getServerHeadInsertions } from "./useServerHeadInsertion.mjs"; import { ensureExists } from "./utils/ensureExists.mjs"; import { SERVER_CONTEXT_POST_RENDER_STRING } from "./vite/constants.mjs"; import { getServerContext, setServerContext } from "./vite/one-server-only.mjs"; import { cloneElement } from "react"; import { Fragment, jsx } from "react/jsx-runtime"; function createApp(options) { return import.meta.env.SSR ? { options, render: async props => { let { loaderData, loaderProps, css, mode, loaderServerData } = props; setServerContext({ postRenderData: loaderServerData, loaderData, loaderProps, mode, css }); let renderId; const App = () => /* @__PURE__ */jsx(Root, { flags: options.flags, onRenderId: id => { renderId = id; }, routes: options.routes, routerRoot: options.routerRoot, ...props }); AppRegistry.registerComponent("App", () => App); const Application = AppRegistry.getApplication("App", {}), rootElement = Application.element.props.children; let html = await renderToString(rootElement, { preloads: props.preloads }); try { const extraHeadElements = [], styleTag = Application.getStyleElement({ nonce: process.env.ONE_NONCE }); styleTag && extraHeadElements.push(styleTag), ensureExists(renderId); const insertions = getServerHeadInsertions(renderId); if (insertions) for (const insertion of insertions) { const out = insertion(); out && extraHeadElements.push(out); } if (extraHeadElements.length) { const extraHeadHTML = ReactDOMServer.renderToStaticMarkup(/* @__PURE__ */jsx(Fragment, { children: extraHeadElements.map((x, i) => cloneElement(x, { key: i })) })); extraHeadHTML && (html = html.replace("</head>", `${extraHeadHTML}</head>`)); } } catch (err) { if (!`${err}`.includes("sheet is not defined")) throw err; } const postRenderData = getServerContext()?.postRenderData; return postRenderData && (html = html.replace(JSON.stringify(SERVER_CONTEXT_POST_RENDER_STRING), JSON.stringify(postRenderData))), html; } } : (options.routes[`/${options.routerRoot}/_layout.tsx`]?.()).then(() => { resolveClientLoader(getServerContext() || {}).then(() => { render(/* @__PURE__ */jsx(Root, { isClient: !0, flags: options.flags, routes: options.routes, routerRoot: options.routerRoot, path: window.location.href })); }).catch(err => { console.error('Error running client loader resolver "onClientLoaderResolve":', err); }); }).catch(err => { console.error("Error importing root layout on client", err); }); } export { createApp }; //# sourceMappingURL=createApp.mjs.map