diff --git a/src/functions/bundler/all-pages-bun-bundler.ts b/src/functions/bundler/all-pages-bun-bundler.ts index 3725b67..33530b5 100644 --- a/src/functions/bundler/all-pages-bun-bundler.ts +++ b/src/functions/bundler/all-pages-bun-bundler.ts @@ -124,7 +124,10 @@ export default async function allPagesBunBundler(params?: Params) { } if (artifacts?.[0]) { - await recordArtifacts({ artifacts }); + await recordArtifacts({ + artifacts, + page_file_paths, + }); } const elapsed = (performance.now() - buildStart).toFixed(0); diff --git a/src/functions/bundler/grab-client-hydration-script.tsx b/src/functions/bundler/grab-client-hydration-script.tsx index b297e54..6317089 100644 --- a/src/functions/bundler/grab-client-hydration-script.tsx +++ b/src/functions/bundler/grab-client-hydration-script.tsx @@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names"; import AppNames from "../../utils/grab-app-names"; import grabConstants from "../../utils/grab-constants"; import pagePathTransform from "../../utils/page-path-transform"; +import grabRootFilePath from "../server/web-pages/grab-root-file-path"; const { PAGES_DIR } = grabDirNames(); @@ -20,25 +21,23 @@ export default async function grabClientHydrationScript({ ClientWindowPagePropsName, } = grabConstants(); + const { root_file_path } = grabRootFilePath(); + const target_path = pagePathTransform({ page_path: page_local_path }); - - const root_component_path = path.join( - PAGES_DIR, - `${AppNames["RootPagesComponentName"]}.tsx`, - ); - - const does_root_exist = existsSync(root_component_path); + const target_root_path = root_file_path + ? pagePathTransform({ page_path: root_file_path }) + : undefined; let txt = ``; txt += `import { hydrateRoot } from "react-dom/client";\n`; - if (does_root_exist) { - txt += `import Root from "${root_component_path}";\n`; + if (target_root_path) { + txt += `import Root from "${target_root_path}";\n`; } txt += `import Page from "${target_path}";\n\n`; txt += `const pageProps = window.${ClientWindowPagePropsName} || {};\n`; - if (does_root_exist) { + if (target_root_path) { txt += `const component = \n`; } else { txt += `const component = \n`; diff --git a/src/functions/bundler/record-artifacts.ts b/src/functions/bundler/record-artifacts.ts index 6736cf4..668e2a8 100644 --- a/src/functions/bundler/record-artifacts.ts +++ b/src/functions/bundler/record-artifacts.ts @@ -1,13 +1,18 @@ import grabDirNames from "../../utils/grab-dir-names"; import type { BundlerCTXMap } from "../../types"; +import _ from "lodash"; const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames(); type Params = { artifacts: BundlerCTXMap[]; + page_file_paths?: string[]; }; -export default async function recordArtifacts({ artifacts }: Params) { +export default async function recordArtifacts({ + artifacts, + page_file_paths, +}: Params) { const artifacts_map: { [k: string]: BundlerCTXMap } = {}; for (const artifact of artifacts) { @@ -17,7 +22,7 @@ export default async function recordArtifacts({ artifacts }: Params) { } if (global.BUNDLER_CTX_MAP) { - global.BUNDLER_CTX_MAP = artifacts_map; + global.BUNDLER_CTX_MAP = _.merge(global.BUNDLER_CTX_MAP, artifacts_map); } await Bun.write( diff --git a/src/functions/server/watcher.ts b/src/functions/server/watcher.ts index 8bcec42..63cd86b 100644 --- a/src/functions/server/watcher.ts +++ b/src/functions/server/watcher.ts @@ -86,9 +86,7 @@ async function fullRebuild(params?: { msg?: string }) { (hmr) => hmr.target_map?.local_path, ).filter((f) => typeof f == "string"); - await rewritePagesModule({ - page_file_path: target_file_paths, - }); + await rewritePagesModule(); if (msg) { log.watch(msg); diff --git a/src/functions/server/web-pages/generate-web-html.tsx b/src/functions/server/web-pages/generate-web-html.tsx index eba911a..ad1fe5e 100644 --- a/src/functions/server/web-pages/generate-web-html.tsx +++ b/src/functions/server/web-pages/generate-web-html.tsx @@ -24,78 +24,123 @@ export default async function genWebHTML({ component, pageProps, bundledMap, - head: Head, module, - meta, routeParams, debug, + root_module, }: LivePageDistGenParams) { const { ClientRootElementIDName, ClientWindowPagePropsName } = grabContants(); + const is_dev = isDevelopment(); + if (debug) { log.info("component", component); } - const componentHTML = renderToString(component); - - if (debug) { - log.info("componentHTML", componentHTML); - } - - const headHTML = Head - ? renderToString() - : ""; - - let html = `\n`; - html += `\n`; - html += ` \n`; - html += ` \n`; - html += ` \n`; - - if (meta) { - html += ` ${grabWebMetaHTML({ meta })}\n`; - } - - if (bundledMap?.css_path) { - html += ` \n`; - } - const serializedProps = (EJSON.stringify(pageProps || {}) || "{}").replace( /<\//g, "<\\/", ); - html += ` \n`; - if (bundledMap?.path) { - const dev = isDevelopment(); - const devSuffix = dev ? "?dev" : ""; - const importMap = JSON.stringify({ - imports: { - react: `https://esm.sh/react@${_reactVersion}${devSuffix}`, - "react-dom": `https://esm.sh/react-dom@${_reactVersion}${devSuffix}`, - "react-dom/client": `https://esm.sh/react-dom@${_reactVersion}/client${devSuffix}`, - "react/jsx-runtime": `https://esm.sh/react@${_reactVersion}/jsx-runtime${devSuffix}`, - "react/jsx-dev-runtime": `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime${devSuffix}`, - }, - }); - html += ` \n`; - html += ` \n`; - } + const page_hydration_script = await grabWebPageHydrationScript(); + const root_meta = root_module?.meta + ? typeof root_module.meta == "function" && routeParams + ? await root_module.meta({ ctx: routeParams, serverRes: pageProps }) + : typeof root_module.meta == "function" + ? undefined + : root_module.meta + : undefined; + const page_meta = module?.meta + ? typeof module.meta == "function" && routeParams + ? await module.meta({ ctx: routeParams, serverRes: pageProps }) + : typeof module.meta == "function" + ? undefined + : module.meta + : undefined; - if (isDevelopment()) { - html += `\n`; - } + const html_props = { + ...module?.html_props, + ...root_module?.html_props, + }; - if (headHTML) { - html += ` ${headHTML}\n`; - } + const Head = module?.Head; + const RootHead = root_module?.Head; - html += ` \n`; - html += ` \n`; - html += `
${componentHTML}
\n`; - html += ` \n`; - html += `\n`; + const dev = isDevelopment(); + const devSuffix = dev ? "?dev" : ""; + const importMap = JSON.stringify({ + imports: { + react: `https://esm.sh/react@${_reactVersion}${devSuffix}`, + "react-dom": `https://esm.sh/react-dom@${_reactVersion}${devSuffix}`, + "react-dom/client": `https://esm.sh/react-dom@${_reactVersion}/client${devSuffix}`, + "react/jsx-runtime": `https://esm.sh/react@${_reactVersion}/jsx-runtime${devSuffix}`, + "react/jsx-dev-runtime": `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime${devSuffix}`, + }, + }); + + let final_component = ( + + + + + + {root_meta ? grabWebMetaHTML({ meta: root_meta }) : null} + {page_meta ? grabWebMetaHTML({ meta: page_meta }) : null} + + {bundledMap?.css_path ? ( + + ) : null} + +