Add more features. Customize HTML. Add server logic and other page module exports to __root
This commit is contained in:
parent
67ed8749e2
commit
6f1db7c01f
@ -124,7 +124,10 @@ export default async function allPagesBunBundler(params?: Params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (artifacts?.[0]) {
|
if (artifacts?.[0]) {
|
||||||
await recordArtifacts({ artifacts });
|
await recordArtifacts({
|
||||||
|
artifacts,
|
||||||
|
page_file_paths,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const elapsed = (performance.now() - buildStart).toFixed(0);
|
const elapsed = (performance.now() - buildStart).toFixed(0);
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import grabDirNames from "../../utils/grab-dir-names";
|
|||||||
import AppNames from "../../utils/grab-app-names";
|
import AppNames from "../../utils/grab-app-names";
|
||||||
import grabConstants from "../../utils/grab-constants";
|
import grabConstants from "../../utils/grab-constants";
|
||||||
import pagePathTransform from "../../utils/page-path-transform";
|
import pagePathTransform from "../../utils/page-path-transform";
|
||||||
|
import grabRootFilePath from "../server/web-pages/grab-root-file-path";
|
||||||
|
|
||||||
const { PAGES_DIR } = grabDirNames();
|
const { PAGES_DIR } = grabDirNames();
|
||||||
|
|
||||||
@ -20,25 +21,23 @@ export default async function grabClientHydrationScript({
|
|||||||
ClientWindowPagePropsName,
|
ClientWindowPagePropsName,
|
||||||
} = grabConstants();
|
} = grabConstants();
|
||||||
|
|
||||||
|
const { root_file_path } = grabRootFilePath();
|
||||||
|
|
||||||
const target_path = pagePathTransform({ page_path: page_local_path });
|
const target_path = pagePathTransform({ page_path: page_local_path });
|
||||||
|
const target_root_path = root_file_path
|
||||||
const root_component_path = path.join(
|
? pagePathTransform({ page_path: root_file_path })
|
||||||
PAGES_DIR,
|
: undefined;
|
||||||
`${AppNames["RootPagesComponentName"]}.tsx`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const does_root_exist = existsSync(root_component_path);
|
|
||||||
|
|
||||||
let txt = ``;
|
let txt = ``;
|
||||||
|
|
||||||
txt += `import { hydrateRoot } from "react-dom/client";\n`;
|
txt += `import { hydrateRoot } from "react-dom/client";\n`;
|
||||||
if (does_root_exist) {
|
if (target_root_path) {
|
||||||
txt += `import Root from "${root_component_path}";\n`;
|
txt += `import Root from "${target_root_path}";\n`;
|
||||||
}
|
}
|
||||||
txt += `import Page from "${target_path}";\n\n`;
|
txt += `import Page from "${target_path}";\n\n`;
|
||||||
txt += `const pageProps = window.${ClientWindowPagePropsName} || {};\n`;
|
txt += `const pageProps = window.${ClientWindowPagePropsName} || {};\n`;
|
||||||
|
|
||||||
if (does_root_exist) {
|
if (target_root_path) {
|
||||||
txt += `const component = <Root suppressHydrationWarning={true} {...pageProps}><Page {...pageProps} /></Root>\n`;
|
txt += `const component = <Root suppressHydrationWarning={true} {...pageProps}><Page {...pageProps} /></Root>\n`;
|
||||||
} else {
|
} else {
|
||||||
txt += `const component = <Page suppressHydrationWarning={true} {...pageProps} />\n`;
|
txt += `const component = <Page suppressHydrationWarning={true} {...pageProps} />\n`;
|
||||||
|
|||||||
@ -1,13 +1,18 @@
|
|||||||
import grabDirNames from "../../utils/grab-dir-names";
|
import grabDirNames from "../../utils/grab-dir-names";
|
||||||
import type { BundlerCTXMap } from "../../types";
|
import type { BundlerCTXMap } from "../../types";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
const { HYDRATION_DST_DIR_MAP_JSON_FILE } = grabDirNames();
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
artifacts: BundlerCTXMap[];
|
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 } = {};
|
const artifacts_map: { [k: string]: BundlerCTXMap } = {};
|
||||||
|
|
||||||
for (const artifact of artifacts) {
|
for (const artifact of artifacts) {
|
||||||
@ -17,7 +22,7 @@ export default async function recordArtifacts({ artifacts }: Params) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (global.BUNDLER_CTX_MAP) {
|
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(
|
await Bun.write(
|
||||||
|
|||||||
@ -86,9 +86,7 @@ async function fullRebuild(params?: { msg?: string }) {
|
|||||||
(hmr) => hmr.target_map?.local_path,
|
(hmr) => hmr.target_map?.local_path,
|
||||||
).filter((f) => typeof f == "string");
|
).filter((f) => typeof f == "string");
|
||||||
|
|
||||||
await rewritePagesModule({
|
await rewritePagesModule();
|
||||||
page_file_path: target_file_paths,
|
|
||||||
});
|
|
||||||
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
log.watch(msg);
|
log.watch(msg);
|
||||||
|
|||||||
@ -24,78 +24,123 @@ export default async function genWebHTML({
|
|||||||
component,
|
component,
|
||||||
pageProps,
|
pageProps,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
head: Head,
|
|
||||||
module,
|
module,
|
||||||
meta,
|
|
||||||
routeParams,
|
routeParams,
|
||||||
debug,
|
debug,
|
||||||
|
root_module,
|
||||||
}: LivePageDistGenParams) {
|
}: LivePageDistGenParams) {
|
||||||
const { ClientRootElementIDName, ClientWindowPagePropsName } =
|
const { ClientRootElementIDName, ClientWindowPagePropsName } =
|
||||||
grabContants();
|
grabContants();
|
||||||
|
|
||||||
|
const is_dev = isDevelopment();
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
log.info("component", component);
|
log.info("component", component);
|
||||||
}
|
}
|
||||||
|
|
||||||
const componentHTML = renderToString(component);
|
|
||||||
|
|
||||||
if (debug) {
|
|
||||||
log.info("componentHTML", componentHTML);
|
|
||||||
}
|
|
||||||
|
|
||||||
const headHTML = Head
|
|
||||||
? renderToString(<Head serverRes={pageProps} ctx={routeParams} />)
|
|
||||||
: "";
|
|
||||||
|
|
||||||
let html = `<!DOCTYPE html>\n`;
|
|
||||||
html += `<html>\n`;
|
|
||||||
html += ` <head>\n`;
|
|
||||||
html += ` <meta charset="utf-8" />\n`;
|
|
||||||
html += ` <meta name="viewport" content="width=device-width, initial-scale=1.0">\n`;
|
|
||||||
|
|
||||||
if (meta) {
|
|
||||||
html += ` ${grabWebMetaHTML({ meta })}\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bundledMap?.css_path) {
|
|
||||||
html += ` <link rel="stylesheet" href="/${bundledMap.css_path}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
const serializedProps = (EJSON.stringify(pageProps || {}) || "{}").replace(
|
const serializedProps = (EJSON.stringify(pageProps || {}) || "{}").replace(
|
||||||
/<\//g,
|
/<\//g,
|
||||||
"<\\/",
|
"<\\/",
|
||||||
);
|
);
|
||||||
html += ` <script>window.${ClientWindowPagePropsName} = ${serializedProps}</script>\n`;
|
|
||||||
|
|
||||||
if (bundledMap?.path) {
|
const page_hydration_script = await grabWebPageHydrationScript();
|
||||||
const dev = isDevelopment();
|
const root_meta = root_module?.meta
|
||||||
const devSuffix = dev ? "?dev" : "";
|
? typeof root_module.meta == "function" && routeParams
|
||||||
const importMap = JSON.stringify({
|
? await root_module.meta({ ctx: routeParams, serverRes: pageProps })
|
||||||
imports: {
|
: typeof root_module.meta == "function"
|
||||||
react: `https://esm.sh/react@${_reactVersion}${devSuffix}`,
|
? undefined
|
||||||
"react-dom": `https://esm.sh/react-dom@${_reactVersion}${devSuffix}`,
|
: root_module.meta
|
||||||
"react-dom/client": `https://esm.sh/react-dom@${_reactVersion}/client${devSuffix}`,
|
: undefined;
|
||||||
"react/jsx-runtime": `https://esm.sh/react@${_reactVersion}/jsx-runtime${devSuffix}`,
|
const page_meta = module?.meta
|
||||||
"react/jsx-dev-runtime": `https://esm.sh/react@${_reactVersion}/jsx-dev-runtime${devSuffix}`,
|
? typeof module.meta == "function" && routeParams
|
||||||
},
|
? await module.meta({ ctx: routeParams, serverRes: pageProps })
|
||||||
});
|
: typeof module.meta == "function"
|
||||||
html += ` <script type="importmap">${importMap}</script>\n`;
|
? undefined
|
||||||
html += ` <script src="/${bundledMap.path}" type="module" id="${AppData["BunextClientHydrationScriptID"]}" async></script>\n`;
|
: module.meta
|
||||||
}
|
: undefined;
|
||||||
|
|
||||||
if (isDevelopment()) {
|
const html_props = {
|
||||||
html += `<script defer>\n${await grabWebPageHydrationScript()}\n</script>\n`;
|
...module?.html_props,
|
||||||
}
|
...root_module?.html_props,
|
||||||
|
};
|
||||||
|
|
||||||
if (headHTML) {
|
const Head = module?.Head;
|
||||||
html += ` ${headHTML}\n`;
|
const RootHead = root_module?.Head;
|
||||||
}
|
|
||||||
|
|
||||||
html += ` </head>\n`;
|
const dev = isDevelopment();
|
||||||
html += ` <body>\n`;
|
const devSuffix = dev ? "?dev" : "";
|
||||||
html += ` <div id="${ClientRootElementIDName}">${componentHTML}</div>\n`;
|
const importMap = JSON.stringify({
|
||||||
html += ` </body>\n`;
|
imports: {
|
||||||
html += `</html>\n`;
|
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 = (
|
||||||
|
<html {...html_props}>
|
||||||
|
<head>
|
||||||
|
<meta charSet="utf-8" />
|
||||||
|
<meta
|
||||||
|
name="viewport"
|
||||||
|
content="width=device-width, initial-scale=1.0"
|
||||||
|
/>
|
||||||
|
|
||||||
|
{root_meta ? grabWebMetaHTML({ meta: root_meta }) : null}
|
||||||
|
{page_meta ? grabWebMetaHTML({ meta: page_meta }) : null}
|
||||||
|
|
||||||
|
{bundledMap?.css_path ? (
|
||||||
|
<link rel="stylesheet" href={`/${bundledMap.css_path}`} />
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
<script
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: `window.${ClientWindowPagePropsName} = ${serializedProps}`,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
|
||||||
|
{bundledMap?.path ? (
|
||||||
|
<>
|
||||||
|
<script
|
||||||
|
type="importmap"
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: importMap,
|
||||||
|
}}
|
||||||
|
fetchPriority="high"
|
||||||
|
/>
|
||||||
|
<script
|
||||||
|
src={`/${bundledMap.path}`}
|
||||||
|
type="module"
|
||||||
|
id={AppData["BunextClientHydrationScriptID"]}
|
||||||
|
defer
|
||||||
|
/>
|
||||||
|
</>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{is_dev ? (
|
||||||
|
<script
|
||||||
|
defer
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: page_hydration_script,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
|
||||||
|
{RootHead ? (
|
||||||
|
<RootHead serverRes={pageProps} ctx={routeParams} />
|
||||||
|
) : null}
|
||||||
|
{Head ? <Head serverRes={pageProps} ctx={routeParams} /> : null}
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div id={ClientRootElementIDName}>{component}</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
);
|
||||||
|
|
||||||
|
let html = `<!DOCTYPE html>\n`;
|
||||||
|
html += renderToString(final_component);
|
||||||
|
|
||||||
return html;
|
return html;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -8,21 +8,19 @@ export default async function generateWebPageResponseFromComponentReturn({
|
|||||||
component,
|
component,
|
||||||
module,
|
module,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
head,
|
|
||||||
meta,
|
|
||||||
routeParams,
|
routeParams,
|
||||||
serverRes,
|
serverRes,
|
||||||
debug,
|
debug,
|
||||||
|
root_module,
|
||||||
}: GrabPageComponentRes) {
|
}: GrabPageComponentRes) {
|
||||||
const html = await genWebHTML({
|
const html = await genWebHTML({
|
||||||
component,
|
component,
|
||||||
pageProps: serverRes,
|
pageProps: serverRes,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
module,
|
module,
|
||||||
meta,
|
|
||||||
head,
|
|
||||||
routeParams,
|
routeParams,
|
||||||
debug,
|
debug,
|
||||||
|
root_module,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
import type { FC } from "react";
|
|
||||||
import grabRouteParams from "../../../utils/grab-route-params";
|
import grabRouteParams from "../../../utils/grab-route-params";
|
||||||
import type {
|
import type {
|
||||||
BunextPageModule,
|
BunextPageModule,
|
||||||
BunextPageModuleServerReturn,
|
BunextPageModuleServerReturn,
|
||||||
|
BunextRootModule,
|
||||||
BunxRouteParams,
|
BunxRouteParams,
|
||||||
GrabPageComponentRes,
|
GrabPageComponentRes,
|
||||||
} from "../../../types";
|
} from "../../../types";
|
||||||
@ -11,6 +11,7 @@ import grabPageBundledReactComponent from "./grab-page-bundled-react-component";
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import { log } from "../../../utils/log";
|
import { log } from "../../../utils/log";
|
||||||
import grabRootFilePath from "./grab-root-file-path";
|
import grabRootFilePath from "./grab-root-file-path";
|
||||||
|
import grabPageServerRes from "./grab-page-server-res";
|
||||||
|
|
||||||
class NotFoundError extends Error {}
|
class NotFoundError extends Error {}
|
||||||
|
|
||||||
@ -65,6 +66,7 @@ export default async function grabPageComponent({
|
|||||||
const bundledMap = global.BUNDLER_CTX_MAP?.[file_path];
|
const bundledMap = global.BUNDLER_CTX_MAP?.[file_path];
|
||||||
|
|
||||||
if (!bundledMap?.path) {
|
if (!bundledMap?.path) {
|
||||||
|
console.log(global.BUNDLER_CTX_MAP);
|
||||||
const errMsg = `No Bundled File Path for this request path!`;
|
const errMsg = `No Bundled File Path for this request path!`;
|
||||||
log.error(errMsg);
|
log.error(errMsg);
|
||||||
throw new Error(errMsg);
|
throw new Error(errMsg);
|
||||||
@ -77,52 +79,36 @@ export default async function grabPageComponent({
|
|||||||
const { root_file_path } = grabRootFilePath();
|
const { root_file_path } = grabRootFilePath();
|
||||||
|
|
||||||
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
const module: BunextPageModule = await import(`${file_path}?t=${now}`);
|
||||||
|
const root_module: BunextRootModule | undefined = root_file_path
|
||||||
|
? await import(`${root_file_path}?t=${now}`)
|
||||||
|
: undefined;
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
log.info(`module:`, module);
|
log.info(`module:`, module);
|
||||||
}
|
}
|
||||||
|
|
||||||
const serverRes: BunextPageModuleServerReturn = await (async () => {
|
const rootServerRes: BunextPageModuleServerReturn | undefined =
|
||||||
const default_props: BunextPageModuleServerReturn = {
|
root_module?.server
|
||||||
url: {
|
? await grabPageServerRes({
|
||||||
...(_.pick<URL, keyof URL>(url!, [
|
module: root_module,
|
||||||
"host",
|
url,
|
||||||
"hostname",
|
query: match?.query,
|
||||||
"pathname",
|
routeParams,
|
||||||
"origin",
|
})
|
||||||
"port",
|
: undefined;
|
||||||
"search",
|
|
||||||
"searchParams",
|
|
||||||
"hash",
|
|
||||||
"href",
|
|
||||||
"password",
|
|
||||||
"protocol",
|
|
||||||
"username",
|
|
||||||
]) as any),
|
|
||||||
},
|
|
||||||
query: match?.query,
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
if (debug) {
|
||||||
if (routeParams) {
|
log.info(`rootServerRes:`, rootServerRes);
|
||||||
const serverData = await module["server"]?.({
|
}
|
||||||
...routeParams,
|
|
||||||
query: { ...routeParams.query, ...match?.query },
|
const serverRes: BunextPageModuleServerReturn = await grabPageServerRes(
|
||||||
});
|
{
|
||||||
return {
|
module,
|
||||||
...serverData,
|
url,
|
||||||
...default_props,
|
query: match?.query,
|
||||||
};
|
routeParams,
|
||||||
}
|
},
|
||||||
return {
|
);
|
||||||
...default_props,
|
|
||||||
};
|
|
||||||
} catch (error) {
|
|
||||||
return {
|
|
||||||
...default_props,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
})();
|
|
||||||
|
|
||||||
if (debug) {
|
if (debug) {
|
||||||
log.info(`serverRes:`, serverRes);
|
log.info(`serverRes:`, serverRes);
|
||||||
@ -143,8 +129,6 @@ export default async function grabPageComponent({
|
|||||||
log.info(`meta:`, meta);
|
log.info(`meta:`, meta);
|
||||||
}
|
}
|
||||||
|
|
||||||
const Head = module.Head as FC<any>;
|
|
||||||
|
|
||||||
const { component } =
|
const { component } =
|
||||||
(await grabPageBundledReactComponent({
|
(await grabPageBundledReactComponent({
|
||||||
file_path,
|
file_path,
|
||||||
@ -162,12 +146,11 @@ export default async function grabPageComponent({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
component,
|
component,
|
||||||
serverRes,
|
serverRes: _.merge(rootServerRes || {}, serverRes),
|
||||||
routeParams,
|
routeParams,
|
||||||
module,
|
module,
|
||||||
bundledMap,
|
bundledMap,
|
||||||
meta,
|
root_module,
|
||||||
head: Head,
|
|
||||||
};
|
};
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
log.error(`Error Grabbing Page Component: ${error.message}`);
|
log.error(`Error Grabbing Page Component: ${error.message}`);
|
||||||
|
|||||||
@ -14,21 +14,25 @@ export default function grabPageReactComponentString({
|
|||||||
}: Params): string | undefined {
|
}: Params): string | undefined {
|
||||||
try {
|
try {
|
||||||
const target_path = pagePathTransform({ page_path: file_path });
|
const target_path = pagePathTransform({ page_path: file_path });
|
||||||
|
const target_root_path = root_file_path
|
||||||
|
? pagePathTransform({ page_path: root_file_path })
|
||||||
|
: undefined;
|
||||||
|
|
||||||
let tsx = ``;
|
let tsx = ``;
|
||||||
|
|
||||||
const server_res_json = JSON.stringify(
|
const server_res_json = JSON.stringify(
|
||||||
EJSON.stringify(server_res || {}) ?? "{}",
|
EJSON.stringify(server_res || {}) ?? "{}",
|
||||||
);
|
);
|
||||||
|
|
||||||
if (root_file_path) {
|
if (target_root_path) {
|
||||||
tsx += `import Root from "${root_file_path}"\n`;
|
tsx += `import Root from "${target_root_path}"\n`;
|
||||||
}
|
}
|
||||||
|
|
||||||
tsx += `import Page from "${target_path}"\n`;
|
tsx += `import Page from "${target_path}"\n`;
|
||||||
tsx += `export default function Main() {\n\n`;
|
tsx += `export default function Main() {\n\n`;
|
||||||
tsx += `const props = JSON.parse(${server_res_json})\n\n`;
|
tsx += `const props = JSON.parse(${server_res_json})\n\n`;
|
||||||
tsx += ` return (\n`;
|
tsx += ` return (\n`;
|
||||||
if (root_file_path) {
|
if (target_root_path) {
|
||||||
tsx += ` <Root suppressHydrationWarning={true} {...props}><Page {...props} /></Root>\n`;
|
tsx += ` <Root suppressHydrationWarning={true} {...props}><Page {...props} /></Root>\n`;
|
||||||
} else {
|
} else {
|
||||||
tsx += ` <Page suppressHydrationWarning={true} {...props} />\n`;
|
tsx += ` <Page suppressHydrationWarning={true} {...props} />\n`;
|
||||||
|
|||||||
63
src/functions/server/web-pages/grab-page-server-res.tsx
Normal file
63
src/functions/server/web-pages/grab-page-server-res.tsx
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
import type {
|
||||||
|
BunextPageModule,
|
||||||
|
BunextPageModuleServerReturn,
|
||||||
|
BunxRouteParams,
|
||||||
|
GrabPageComponentRes,
|
||||||
|
} from "../../../types";
|
||||||
|
import _ from "lodash";
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
url?: URL;
|
||||||
|
module: BunextPageModule;
|
||||||
|
query?: Record<string, string>;
|
||||||
|
routeParams?: BunxRouteParams;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default async function grabPageServerRes({
|
||||||
|
url,
|
||||||
|
query,
|
||||||
|
routeParams,
|
||||||
|
module,
|
||||||
|
}: Params): Promise<BunextPageModuleServerReturn> {
|
||||||
|
const default_props: BunextPageModuleServerReturn = {
|
||||||
|
url: url
|
||||||
|
? {
|
||||||
|
...(_.pick<URL, keyof URL>(url, [
|
||||||
|
"host",
|
||||||
|
"hostname",
|
||||||
|
"pathname",
|
||||||
|
"origin",
|
||||||
|
"port",
|
||||||
|
"search",
|
||||||
|
"searchParams",
|
||||||
|
"hash",
|
||||||
|
"href",
|
||||||
|
"password",
|
||||||
|
"protocol",
|
||||||
|
"username",
|
||||||
|
]) as any),
|
||||||
|
}
|
||||||
|
: null,
|
||||||
|
query,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (routeParams) {
|
||||||
|
const serverData = await module["server"]?.({
|
||||||
|
...routeParams,
|
||||||
|
query: { ...routeParams.query, ...query },
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
...serverData,
|
||||||
|
...default_props,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
...default_props,
|
||||||
|
};
|
||||||
|
} catch (error) {
|
||||||
|
return {
|
||||||
|
...default_props,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,7 @@
|
|||||||
import isDevelopment from "../../../utils/is-development";
|
import isDevelopment from "../../../utils/is-development";
|
||||||
import * as esbuild from "esbuild";
|
import tailwindcss from "bun-plugin-tailwind";
|
||||||
import grabDirNames from "../../../utils/grab-dir-names";
|
import grabDirNames from "../../../utils/grab-dir-names";
|
||||||
import path from "path";
|
import path from "path";
|
||||||
import tailwindEsbuildPlugin from "./tailwind-esbuild-plugin";
|
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
tsx: string;
|
tsx: string;
|
||||||
@ -20,21 +19,22 @@ export default async function grabTsxStringModule<T extends any = any>({
|
|||||||
.replace(/.*\/src\/pages\//, "")
|
.replace(/.*\/src\/pages\//, "")
|
||||||
.replace(/\.tsx$/, "");
|
.replace(/\.tsx$/, "");
|
||||||
|
|
||||||
|
const src_file_path = path.join(
|
||||||
|
BUNX_CWD_MODULE_CACHE_DIR,
|
||||||
|
`${trimmed_file_path}.tsx`,
|
||||||
|
);
|
||||||
|
|
||||||
const out_file_path = path.join(
|
const out_file_path = path.join(
|
||||||
BUNX_CWD_MODULE_CACHE_DIR,
|
BUNX_CWD_MODULE_CACHE_DIR,
|
||||||
`${trimmed_file_path}.js`,
|
`${trimmed_file_path}.js`,
|
||||||
);
|
);
|
||||||
|
|
||||||
await esbuild.build({
|
await Bun.write(src_file_path, tsx);
|
||||||
stdin: {
|
|
||||||
contents: tsx,
|
await Bun.build({
|
||||||
resolveDir: process.cwd(),
|
entrypoints: [src_file_path],
|
||||||
loader: "tsx",
|
|
||||||
},
|
|
||||||
bundle: true,
|
|
||||||
format: "esm",
|
format: "esm",
|
||||||
target: "es2020",
|
target: "bun",
|
||||||
platform: "node",
|
|
||||||
external: ["react", "react-dom"],
|
external: ["react", "react-dom"],
|
||||||
minify: true,
|
minify: true,
|
||||||
define: {
|
define: {
|
||||||
@ -43,10 +43,12 @@ export default async function grabTsxStringModule<T extends any = any>({
|
|||||||
),
|
),
|
||||||
},
|
},
|
||||||
metafile: true,
|
metafile: true,
|
||||||
plugins: [tailwindEsbuildPlugin],
|
plugins: [tailwindcss],
|
||||||
jsx: "automatic",
|
jsx: {
|
||||||
write: true,
|
runtime: "automatic",
|
||||||
outfile: out_file_path,
|
development: dev,
|
||||||
|
},
|
||||||
|
outdir: BUNX_CWD_MODULE_CACHE_DIR,
|
||||||
});
|
});
|
||||||
|
|
||||||
Loader.registry.delete(out_file_path);
|
Loader.registry.delete(out_file_path);
|
||||||
@ -54,3 +56,27 @@ export default async function grabTsxStringModule<T extends any = any>({
|
|||||||
|
|
||||||
return module as T;
|
return module as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// await esbuild.build({
|
||||||
|
// stdin: {
|
||||||
|
// contents: tsx,
|
||||||
|
// resolveDir: process.cwd(),
|
||||||
|
// loader: "tsx",
|
||||||
|
// },
|
||||||
|
// bundle: true,
|
||||||
|
// format: "esm",
|
||||||
|
// target: "es2020",
|
||||||
|
// platform: "node",
|
||||||
|
// external: ["react", "react-dom"],
|
||||||
|
// minify: true,
|
||||||
|
// define: {
|
||||||
|
// "process.env.NODE_ENV": JSON.stringify(
|
||||||
|
// dev ? "development" : "production",
|
||||||
|
// ),
|
||||||
|
// },
|
||||||
|
// metafile: true,
|
||||||
|
// plugins: [tailwindEsbuildPlugin],
|
||||||
|
// jsx: "automatic",
|
||||||
|
// write: true,
|
||||||
|
// outfile: out_file_path,
|
||||||
|
// });
|
||||||
|
|||||||
@ -1,77 +0,0 @@
|
|||||||
import { escape } from "lodash";
|
|
||||||
import type { BunextPageModuleMeta } from "../../../types";
|
|
||||||
|
|
||||||
type Params = {
|
|
||||||
meta: BunextPageModuleMeta;
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function grabWebMetaHTML({ meta }: Params) {
|
|
||||||
let html = ``;
|
|
||||||
|
|
||||||
if (meta.title) {
|
|
||||||
html += ` <title>${escape(meta.title)}</title>\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.description) {
|
|
||||||
html += ` <meta name="description" content="${escape(meta.description)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.keywords) {
|
|
||||||
const keywords = Array.isArray(meta.keywords)
|
|
||||||
? meta.keywords.join(", ")
|
|
||||||
: meta.keywords;
|
|
||||||
html += ` <meta name="keywords" content="${escape(keywords)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.author) {
|
|
||||||
html += ` <meta name="author" content="${escape(meta.author)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.robots) {
|
|
||||||
html += ` <meta name="robots" content="${escape(meta.robots)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.canonical) {
|
|
||||||
html += ` <link rel="canonical" href="${escape(meta.canonical)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.themeColor) {
|
|
||||||
html += ` <meta name="theme-color" content="${escape(meta.themeColor)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.og) {
|
|
||||||
const { og } = meta;
|
|
||||||
if (og.title)
|
|
||||||
html += ` <meta property="og:title" content="${escape(og.title)}" />\n`;
|
|
||||||
if (og.description)
|
|
||||||
html += ` <meta property="og:description" content="${escape(og.description)}" />\n`;
|
|
||||||
if (og.image)
|
|
||||||
html += ` <meta property="og:image" content="${escape(og.image)}" />\n`;
|
|
||||||
if (og.url)
|
|
||||||
html += ` <meta property="og:url" content="${escape(og.url)}" />\n`;
|
|
||||||
if (og.type)
|
|
||||||
html += ` <meta property="og:type" content="${escape(og.type)}" />\n`;
|
|
||||||
if (og.siteName)
|
|
||||||
html += ` <meta property="og:site_name" content="${escape(og.siteName)}" />\n`;
|
|
||||||
if (og.locale)
|
|
||||||
html += ` <meta property="og:locale" content="${escape(og.locale)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (meta.twitter) {
|
|
||||||
const { twitter } = meta;
|
|
||||||
if (twitter.card)
|
|
||||||
html += ` <meta name="twitter:card" content="${escape(twitter.card)}" />\n`;
|
|
||||||
if (twitter.title)
|
|
||||||
html += ` <meta name="twitter:title" content="${escape(twitter.title)}" />\n`;
|
|
||||||
if (twitter.description)
|
|
||||||
html += ` <meta name="twitter:description" content="${escape(twitter.description)}" />\n`;
|
|
||||||
if (twitter.image)
|
|
||||||
html += ` <meta name="twitter:image" content="${escape(twitter.image)}" />\n`;
|
|
||||||
if (twitter.site)
|
|
||||||
html += ` <meta name="twitter:site" content="${escape(twitter.site)}" />\n`;
|
|
||||||
if (twitter.creator)
|
|
||||||
html += ` <meta name="twitter:creator" content="${escape(twitter.creator)}" />\n`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return html;
|
|
||||||
}
|
|
||||||
76
src/functions/server/web-pages/grab-web-meta-html.tsx
Normal file
76
src/functions/server/web-pages/grab-web-meta-html.tsx
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import type { BunextPageModuleMeta } from "../../../types";
|
||||||
|
|
||||||
|
type Params = {
|
||||||
|
meta: BunextPageModuleMeta;
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function grabWebMetaHTML({ meta }: Params) {
|
||||||
|
const keywords = meta.keywords
|
||||||
|
? Array.isArray(meta.keywords)
|
||||||
|
? meta.keywords.join(", ")
|
||||||
|
: meta.keywords
|
||||||
|
: undefined;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{meta.title && <title>{meta.title}</title>}
|
||||||
|
{meta.description && (
|
||||||
|
<meta name="description" content={meta.description} />
|
||||||
|
)}
|
||||||
|
{keywords && <meta name="keywords" content={keywords} />}
|
||||||
|
{meta.author && <meta name="author" content={meta.author} />}
|
||||||
|
{meta.robots && <meta name="robots" content={meta.robots} />}
|
||||||
|
{meta.canonical && (
|
||||||
|
<link rel="canonical" href={meta.canonical} />
|
||||||
|
)}
|
||||||
|
{meta.themeColor && (
|
||||||
|
<meta name="theme-color" content={meta.themeColor} />
|
||||||
|
)}
|
||||||
|
{meta.og?.title && (
|
||||||
|
<meta property="og:title" content={meta.og.title} />
|
||||||
|
)}
|
||||||
|
{meta.og?.description && (
|
||||||
|
<meta
|
||||||
|
property="og:description"
|
||||||
|
content={meta.og.description}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{meta.og?.image && (
|
||||||
|
<meta property="og:image" content={meta.og.image} />
|
||||||
|
)}
|
||||||
|
{meta.og?.url && (
|
||||||
|
<meta property="og:url" content={meta.og.url} />
|
||||||
|
)}
|
||||||
|
{meta.og?.type && (
|
||||||
|
<meta property="og:type" content={meta.og.type} />
|
||||||
|
)}
|
||||||
|
{meta.og?.siteName && (
|
||||||
|
<meta property="og:site_name" content={meta.og.siteName} />
|
||||||
|
)}
|
||||||
|
{meta.og?.locale && (
|
||||||
|
<meta property="og:locale" content={meta.og.locale} />
|
||||||
|
)}
|
||||||
|
{meta.twitter?.card && (
|
||||||
|
<meta name="twitter:card" content={meta.twitter.card} />
|
||||||
|
)}
|
||||||
|
{meta.twitter?.title && (
|
||||||
|
<meta name="twitter:title" content={meta.twitter.title} />
|
||||||
|
)}
|
||||||
|
{meta.twitter?.description && (
|
||||||
|
<meta
|
||||||
|
name="twitter:description"
|
||||||
|
content={meta.twitter.description}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{meta.twitter?.image && (
|
||||||
|
<meta name="twitter:image" content={meta.twitter.image} />
|
||||||
|
)}
|
||||||
|
{meta.twitter?.site && (
|
||||||
|
<meta name="twitter:site" content={meta.twitter.site} />
|
||||||
|
)}
|
||||||
|
{meta.twitter?.creator && (
|
||||||
|
<meta name="twitter:creator" content={meta.twitter.creator} />
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
13
src/presets/components/head.tsx
Normal file
13
src/presets/components/head.tsx
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import type {
|
||||||
|
DetailedHTMLProps,
|
||||||
|
HTMLAttributes,
|
||||||
|
PropsWithChildren,
|
||||||
|
} from "react";
|
||||||
|
|
||||||
|
type Props = PropsWithChildren<
|
||||||
|
DetailedHTMLProps<HTMLAttributes<HTMLHeadElement>, HTMLHeadElement>
|
||||||
|
>;
|
||||||
|
|
||||||
|
export default function Head({ children, ...props }: Props) {
|
||||||
|
return <head {...props}>{children}</head>;
|
||||||
|
}
|
||||||
@ -1,5 +1,12 @@
|
|||||||
import type { MatchedRoute, Server, WebSocketHandler } from "bun";
|
import type { MatchedRoute, Server, WebSocketHandler } from "bun";
|
||||||
import type { FC, JSX, PropsWithChildren, ReactNode } from "react";
|
import type {
|
||||||
|
DetailedHTMLProps,
|
||||||
|
FC,
|
||||||
|
HtmlHTMLAttributes,
|
||||||
|
JSX,
|
||||||
|
PropsWithChildren,
|
||||||
|
ReactNode,
|
||||||
|
} from "react";
|
||||||
|
|
||||||
export type ServerProps = {
|
export type ServerProps = {
|
||||||
params: Record<string, string>;
|
params: Record<string, string>;
|
||||||
@ -145,11 +152,10 @@ export type PageDistGenParams = {
|
|||||||
|
|
||||||
export type LivePageDistGenParams = {
|
export type LivePageDistGenParams = {
|
||||||
component: ReactNode;
|
component: ReactNode;
|
||||||
head?: FC<BunextPageHeadFCProps>;
|
|
||||||
pageProps?: any;
|
pageProps?: any;
|
||||||
module?: BunextPageModule;
|
module?: BunextPageModule;
|
||||||
|
root_module?: BunextRootModule;
|
||||||
bundledMap?: BundlerCTXMap;
|
bundledMap?: BundlerCTXMap;
|
||||||
meta?: BunextPageModuleMeta;
|
|
||||||
routeParams?: BunxRouteParams;
|
routeParams?: BunxRouteParams;
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
};
|
};
|
||||||
@ -165,8 +171,14 @@ export type BunextPageModule = {
|
|||||||
meta?: BunextPageModuleMeta | BunextPageModuleMetaFn;
|
meta?: BunextPageModuleMeta | BunextPageModuleMetaFn;
|
||||||
Head?: FC<BunextPageHeadFCProps>;
|
Head?: FC<BunextPageHeadFCProps>;
|
||||||
config?: BunextRouteConfig;
|
config?: BunextRouteConfig;
|
||||||
|
html_props?: BunextHTMLProps;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type BunextHTMLProps = DetailedHTMLProps<
|
||||||
|
HtmlHTMLAttributes<HTMLHtmlElement>,
|
||||||
|
HTMLHtmlElement
|
||||||
|
>;
|
||||||
|
|
||||||
export type BunextPageModuleMetaFn = (params: {
|
export type BunextPageModuleMetaFn = (params: {
|
||||||
ctx: BunxRouteParams;
|
ctx: BunxRouteParams;
|
||||||
serverRes?: BunextPageModuleServerReturn;
|
serverRes?: BunextPageModuleServerReturn;
|
||||||
@ -253,11 +265,12 @@ export type GrabPageComponentRes = {
|
|||||||
routeParams?: BunxRouteParams;
|
routeParams?: BunxRouteParams;
|
||||||
bundledMap?: BundlerCTXMap;
|
bundledMap?: BundlerCTXMap;
|
||||||
module: BunextPageModule;
|
module: BunextPageModule;
|
||||||
meta?: BunextPageModuleMeta;
|
root_module?: BunextRootModule;
|
||||||
head?: FC<BunextPageHeadFCProps>;
|
|
||||||
debug?: boolean;
|
debug?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type BunextRootModule = BunextPageModule;
|
||||||
|
|
||||||
export type GrabPageReactBundledComponentRes = {
|
export type GrabPageReactBundledComponentRes = {
|
||||||
component: JSX.Element;
|
component: JSX.Element;
|
||||||
server_res?: BunextPageModuleServerReturn;
|
server_res?: BunextPageModuleServerReturn;
|
||||||
|
|||||||
@ -1,6 +1,8 @@
|
|||||||
import grabAllPages from "./grab-all-pages";
|
import grabAllPages from "./grab-all-pages";
|
||||||
import pagePathTransform from "./page-path-transform";
|
import pagePathTransform from "./page-path-transform";
|
||||||
import stripServerSideLogic from "../functions/bundler/strip-server-side-logic";
|
import stripServerSideLogic from "../functions/bundler/strip-server-side-logic";
|
||||||
|
import grabRootFilePath from "../functions/server/web-pages/grab-root-file-path";
|
||||||
|
import { existsSync } from "fs";
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
page_file_path?: string | string[];
|
page_file_path?: string | string[];
|
||||||
@ -21,20 +23,26 @@ export default async function rewritePagesModule(params?: Params) {
|
|||||||
|
|
||||||
for (let i = 0; i < target_pages.length; i++) {
|
for (let i = 0; i < target_pages.length; i++) {
|
||||||
const page_path = target_pages[i];
|
const page_path = target_pages[i];
|
||||||
const dst_path = pagePathTransform({ page_path });
|
await transformFile(page_path);
|
||||||
|
}
|
||||||
|
|
||||||
if (page_path.match(/__root\.tsx?/)) {
|
const { root_file_path } = grabRootFilePath();
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const origin_page_content = await Bun.file(page_path).text();
|
if (root_file_path && existsSync(root_file_path)) {
|
||||||
const dst_page_content = stripServerSideLogic({
|
await transformFile(root_file_path);
|
||||||
txt_code: origin_page_content,
|
|
||||||
file_path: page_path,
|
|
||||||
});
|
|
||||||
|
|
||||||
await Bun.write(dst_path, dst_page_content, {
|
|
||||||
createPath: true,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function transformFile(page_path: string) {
|
||||||
|
const dst_path = pagePathTransform({ page_path });
|
||||||
|
|
||||||
|
const origin_page_content = await Bun.file(page_path).text();
|
||||||
|
const dst_page_content = stripServerSideLogic({
|
||||||
|
txt_code: origin_page_content,
|
||||||
|
file_path: page_path,
|
||||||
|
});
|
||||||
|
|
||||||
|
await Bun.write(dst_path, dst_page_content, {
|
||||||
|
createPath: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user