Updates
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
import { readdirSync, statSync, unlinkSync } from "fs";
|
||||
import grabAllPages from "../../utils/grab-all-pages";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabPageName from "../../utils/grab-page-name";
|
||||
import writeWebPageHydrationScript from "../server/web-pages/write-web-page-hydration-script";
|
||||
import path from "path";
|
||||
import bundle from "../../utils/bundle";
|
||||
import AppNames from "../../utils/grab-app-names";
|
||||
import type { PageFiles } from "../../types";
|
||||
|
||||
const { BUNX_HYDRATION_SRC_DIR, HYDRATION_DST_DIR } = grabDirNames();
|
||||
|
||||
export default async function allPagesBundler() {
|
||||
console.time("build");
|
||||
|
||||
const pages = grabAllPages({ exclude_api: true });
|
||||
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const page = pages[i];
|
||||
|
||||
if (!isPageValid(page)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const pageName = grabPageName({ path: page.local_path });
|
||||
|
||||
writeWebPageHydrationScript({
|
||||
pageName,
|
||||
page_file: page.local_path,
|
||||
});
|
||||
}
|
||||
|
||||
const hydration_files = readdirSync(BUNX_HYDRATION_SRC_DIR);
|
||||
|
||||
for (let i = 0; i < hydration_files.length; i++) {
|
||||
const hydration_file = hydration_files[i];
|
||||
|
||||
const valid_file = pages.find((p) => {
|
||||
if (!isPageValid(p)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const pageName = grabPageName({ path: p.local_path });
|
||||
|
||||
const file_tsx_name = `${pageName}.tsx`;
|
||||
if (file_tsx_name == hydration_file) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
if (!valid_file) {
|
||||
unlinkSync(path.join(BUNX_HYDRATION_SRC_DIR, hydration_file));
|
||||
}
|
||||
}
|
||||
|
||||
const entrypoints = readdirSync(BUNX_HYDRATION_SRC_DIR)
|
||||
.filter((f) => f.endsWith(".tsx"))
|
||||
.map((f) => path.join(BUNX_HYDRATION_SRC_DIR, f))
|
||||
.filter((f) => statSync(f).isFile());
|
||||
|
||||
bundle({
|
||||
src: entrypoints.join(" "),
|
||||
out_dir: HYDRATION_DST_DIR,
|
||||
exec_options: { stdio: "ignore" },
|
||||
});
|
||||
|
||||
console.timeEnd("build");
|
||||
}
|
||||
|
||||
function isPageValid(page: PageFiles): boolean {
|
||||
if (page.file_name == AppNames["RootPagesComponentName"]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (page.url_path.match(/\(|\)|--/)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -12,8 +12,6 @@ export default async function () {
|
||||
const key = keys[i];
|
||||
const dir = dirNames[key];
|
||||
|
||||
// const stat = statSync(dir);
|
||||
|
||||
if (!existsSync(dir) && !dir.match(/\.\w+$/)) {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
continue;
|
||||
|
||||
@@ -11,7 +11,7 @@ type Params = {
|
||||
export default async function getRoute({
|
||||
route,
|
||||
}: Params): Promise<GetRouteReturn | null> {
|
||||
const { ROUTES_DIR } = grabDirNames();
|
||||
const {} = grabDirNames();
|
||||
|
||||
if (route.match(/\(/)) {
|
||||
return null;
|
||||
|
||||
@@ -38,7 +38,7 @@ export default async function ({
|
||||
};
|
||||
}
|
||||
|
||||
const routeParams: BunxRouteParams = await grabRouteParams({ req, server });
|
||||
const routeParams: BunxRouteParams = await grabRouteParams({ req });
|
||||
|
||||
const module = await import(match.filePath);
|
||||
const config = module.config as BunextServerRouteConfig | undefined;
|
||||
@@ -62,7 +62,7 @@ export default async function ({
|
||||
}
|
||||
|
||||
const res: APIResponseObject = await module["default"](
|
||||
routeParams as BunxRouteParams
|
||||
routeParams as BunxRouteParams,
|
||||
);
|
||||
|
||||
return res;
|
||||
|
||||
@@ -6,14 +6,14 @@ import handleWebPages from "./web-pages/handle-web-pages";
|
||||
import handleRoutes from "./handle-routes";
|
||||
import isDevelopment from "../../utils/is-development";
|
||||
|
||||
const port = grabAppPort();
|
||||
const { PUBLIC_DIR } = grabDirNames();
|
||||
|
||||
type Params = {
|
||||
dev?: boolean;
|
||||
};
|
||||
|
||||
export default async function (params?: Params): Promise<ServeOptions> {
|
||||
const port = grabAppPort();
|
||||
const { PUBLIC_DIR } = grabDirNames();
|
||||
|
||||
return {
|
||||
async fetch(req, server) {
|
||||
try {
|
||||
@@ -61,7 +61,7 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
|
||||
return new Response(file);
|
||||
} else {
|
||||
return await handleWebPages({ req, server });
|
||||
return await handleWebPages({ req });
|
||||
}
|
||||
} catch (error: any) {
|
||||
return new Response(`Server Error: ${error.message}`, {
|
||||
@@ -70,9 +70,6 @@ export default async function (params?: Params): Promise<ServeOptions> {
|
||||
}
|
||||
},
|
||||
port,
|
||||
development: isDevelopment() && {
|
||||
hmr: true,
|
||||
console: true,
|
||||
},
|
||||
idleTimeout: 0,
|
||||
} as ServeOptions;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import AppNames from "../../utils/grab-app-names";
|
||||
import allPagesBundler from "../bundler/all-pages-bundler";
|
||||
import serverParamsGen from "./server-params-gen";
|
||||
import watcher from "./watcher";
|
||||
|
||||
@@ -15,6 +16,8 @@ export default async function startServer(params?: Params) {
|
||||
|
||||
global.SERVER = server;
|
||||
|
||||
await allPagesBundler();
|
||||
|
||||
console.log(
|
||||
`${name} Server Running on http://localhost:${server.port} ...`,
|
||||
);
|
||||
|
||||
@@ -1,101 +1,57 @@
|
||||
import { watch } from "fs";
|
||||
import grabDirNames from "../../utils/grab-dir-names";
|
||||
import grabPageName from "../../utils/grab-page-name";
|
||||
import path from "path";
|
||||
import { execSync } from "child_process";
|
||||
import serverParamsGen from "./server-params-gen";
|
||||
import allPagesBundler from "../bundler/all-pages-bundler";
|
||||
|
||||
const { ROOT_DIR, BUNX_HYDRATION_SRC_DIR, HYDRATION_DST_DIR, ROUTES_DIR } =
|
||||
const { ROOT_DIR, BUNX_HYDRATION_SRC_DIR, HYDRATION_DST_DIR, PAGES_DIR } =
|
||||
grabDirNames();
|
||||
|
||||
export default function watcher() {
|
||||
watch(
|
||||
ROOT_DIR,
|
||||
{ recursive: true, persistent: true },
|
||||
async (event, filename) => {
|
||||
{
|
||||
recursive: true,
|
||||
persistent: true,
|
||||
},
|
||||
(event, filename) => {
|
||||
// if (!filename) return;
|
||||
// if (filename.match(/ /)) return;
|
||||
// if (filename.match(/^node_modules\//)) return;
|
||||
// if (filename.match(/\.bunext|\/?public\//)) return;
|
||||
// if (!filename.match(/\.(tsx|ts|css|js|jsx)$/)) return;
|
||||
|
||||
if (global.RECOMPILING) return;
|
||||
if (!filename) return;
|
||||
if (filename.match(/ /)) return;
|
||||
if (filename.match(/^node_modules\//)) return;
|
||||
if (filename.match(/\.bunext|\/public\//)) return;
|
||||
|
||||
if (filename.match(/\/routes\//)) {
|
||||
if (event == "change") {
|
||||
clearTimeout(global.WATCHER_TIMEOUT);
|
||||
|
||||
clearTimeout(global.WATCHER_TIMEOUT);
|
||||
global.WATCHER_TIMEOUT = setTimeout(async () => {
|
||||
try {
|
||||
global.RECOMPILING = true;
|
||||
|
||||
const fullPath = path.join(ROOT_DIR, filename);
|
||||
await allPagesBundler();
|
||||
|
||||
const pageName = grabPageName({ path: fullPath });
|
||||
|
||||
// const router = grabRouter();
|
||||
// const match = router.match(fullPath);
|
||||
|
||||
// if (match?.filePath) {
|
||||
// const module = await import(match.filePath);
|
||||
|
||||
// const serverRes = await (async () => {
|
||||
// try {
|
||||
// return await module["server"]();
|
||||
// } catch (error) {
|
||||
// return {};
|
||||
// }
|
||||
// })();
|
||||
|
||||
// const Component = module.default as FC<any>;
|
||||
// const component = <Component pageProps={serverRes} />;
|
||||
|
||||
// await writeWebPageHydrationScript({
|
||||
// pageName,
|
||||
// component,
|
||||
// });
|
||||
// }
|
||||
|
||||
// await Bun.build({
|
||||
// entrypoints: [
|
||||
// `${BUNX_HYDRATION_SRC_DIR}/${pageName}.tsx`,
|
||||
// ],
|
||||
// outdir: HYDRATION_DST_DIR,
|
||||
// minify: true,
|
||||
// });
|
||||
|
||||
let cmd = `bun build`;
|
||||
cmd += ` ${BUNX_HYDRATION_SRC_DIR}/${pageName}.tsx --outdir ${HYDRATION_DST_DIR}`;
|
||||
cmd += ` --minify`;
|
||||
// cmd += ` && bun pm cache rm`;
|
||||
|
||||
execSync(cmd, { stdio: "inherit" });
|
||||
|
||||
global.ROUTER = new Bun.FileSystemRouter({
|
||||
style: "nextjs",
|
||||
dir: ROUTES_DIR,
|
||||
});
|
||||
|
||||
const encoder = new TextEncoder();
|
||||
const msg = encoder.encode(
|
||||
`event: update\ndata: reload\n\n`,
|
||||
);
|
||||
global.LAST_BUILD_TIME = Date.now();
|
||||
|
||||
for (const controller of global.HMR_CONTROLLERS) {
|
||||
controller.enqueue(msg.toString());
|
||||
try {
|
||||
controller.enqueue(
|
||||
`event: update\ndata: ${global.LAST_BUILD_TIME}\n\n`,
|
||||
);
|
||||
} catch {
|
||||
global.HMR_CONTROLLERS.delete(controller);
|
||||
}
|
||||
}
|
||||
|
||||
// Let the SSE event flush before restarting the server.
|
||||
// The server restart is required to clear Bun's module cache
|
||||
// so the next request renders the updated route, not the
|
||||
// stale cached module (which causes a hydration mismatch).
|
||||
// await Bun.sleep(500);
|
||||
|
||||
// await reloadServer();
|
||||
} catch (error: any) {
|
||||
console.log(error);
|
||||
} finally {
|
||||
global.RECOMPILING = false;
|
||||
} else if (event == "rename") {
|
||||
await reloadServer();
|
||||
}
|
||||
} else if (filename.match(/\.(js|ts|tsx|jsx)$/)) {
|
||||
clearTimeout(global.WATCHER_TIMEOUT);
|
||||
await reloadServer();
|
||||
}
|
||||
}, 150);
|
||||
|
||||
// if (filename.match(/\/pages\//)) {
|
||||
// } else if (filename.match(/\.(js|ts|tsx|jsx)$/)) {
|
||||
// clearTimeout(global.WATCHER_TIMEOUT);
|
||||
// global.WATCHER_TIMEOUT = setTimeout(() => reloadServer(), 150);
|
||||
// }
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import path from "path";
|
||||
import { renderToString } from "react-dom/server";
|
||||
import grabContants from "../../../utils/grab-constants";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import EJSON from "../../../utils/ejson";
|
||||
import type { PageDistGenParams } from "../../../types";
|
||||
import type { LivePageDistGenParams } from "../../../types";
|
||||
import isDevelopment from "../../../utils/is-development";
|
||||
|
||||
export default async function genWebHTML({
|
||||
@@ -10,16 +10,28 @@ export default async function genWebHTML({
|
||||
pageProps,
|
||||
pageName,
|
||||
module,
|
||||
}: PageDistGenParams) {
|
||||
}: LivePageDistGenParams) {
|
||||
const { ClientRootElementIDName, ClientWindowPagePropsName } =
|
||||
await grabContants();
|
||||
|
||||
const { renderToString } = await import(
|
||||
path.join(process.cwd(), "node_modules", "react-dom", "server")
|
||||
);
|
||||
|
||||
const componentHTML = renderToString(component);
|
||||
|
||||
const SCRIPT_SRC = path.join("/public/routes", pageName + ".js");
|
||||
const SCRIPT_SRC = path.join("/public/pages", pageName + ".js");
|
||||
const CSS_SRC = path.join("/public/pages", pageName + ".css");
|
||||
const { HYDRATION_DST_DIR } = grabDirNames();
|
||||
const cssExists = await Bun.file(path.join(HYDRATION_DST_DIR, pageName + ".css")).exists();
|
||||
|
||||
let html = `<!DOCTYPE html>\n`;
|
||||
|
||||
html += `<html>\n`;
|
||||
html += ` <head>\n`;
|
||||
html += ` <meta charset="utf-8" />\n`;
|
||||
if (cssExists) {
|
||||
html += ` <link rel="stylesheet" href="${CSS_SRC}" />\n`;
|
||||
}
|
||||
if (isDevelopment()) {
|
||||
html += `<script>
|
||||
const hmr = new EventSource("/__hmr");
|
||||
@@ -30,11 +42,6 @@ export default async function genWebHTML({
|
||||
});
|
||||
</script>\n`;
|
||||
}
|
||||
|
||||
html += `<html>\n`;
|
||||
html += ` <head>\n`;
|
||||
html += ` <meta charset="utf-8" />\n`;
|
||||
html += ` <title>React SSR with Bun</title>\n`;
|
||||
html += ` </head>\n`;
|
||||
html += ` <body>\n`;
|
||||
html += ` <div id="${ClientRootElementIDName}">${componentHTML}</div>\n`;
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
import type { FC } from "react";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import grabPageName from "../../../utils/grab-page-name";
|
||||
import grabRouteParams from "../../../utils/grab-route-params";
|
||||
import grabRouter from "../../../utils/grab-router";
|
||||
import type { BunextPageModule, GrabPageComponentRes } from "../../../types";
|
||||
import bundle from "../../../utils/bundle";
|
||||
import path from "path";
|
||||
import AppNames from "../../../utils/grab-app-names";
|
||||
import { existsSync } from "fs";
|
||||
|
||||
type Params = {
|
||||
req?: Request;
|
||||
file_path?: string;
|
||||
};
|
||||
|
||||
export default async function grabPageComponent({
|
||||
req,
|
||||
file_path: passed_file_path,
|
||||
}: Params): Promise<GrabPageComponentRes> {
|
||||
const url = req?.url ? new URL(req.url) : undefined;
|
||||
const router = grabRouter();
|
||||
|
||||
const {
|
||||
BUNX_ROOT_500_PRESET_COMPONENT,
|
||||
HYDRATION_DST_DIR,
|
||||
BUNX_ROOT_500_FILE_NAME,
|
||||
PAGES_DIR,
|
||||
} = grabDirNames();
|
||||
|
||||
const routeParams = req ? await grabRouteParams({ req }) : undefined;
|
||||
|
||||
try {
|
||||
const match = url ? router.match(url.pathname) : undefined;
|
||||
|
||||
if (!match?.filePath && url?.pathname) {
|
||||
const errMsg = `Page ${url.pathname} not found`;
|
||||
console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const file_path = match?.filePath || passed_file_path;
|
||||
|
||||
if (!file_path) {
|
||||
const errMsg = `No File Path (\`file_path\`) or Request Object (\`req\`) provided not found`;
|
||||
console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const pageName = grabPageName({ path: file_path });
|
||||
|
||||
const root_pages_component_ts_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.ts`;
|
||||
const root_pages_component_tsx_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.tsx`;
|
||||
const root_pages_component_js_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.js`;
|
||||
const root_pages_component_jsx_file = `${path.join(PAGES_DIR, AppNames["RootPagesComponentName"])}.jsx`;
|
||||
|
||||
const root_file = existsSync(root_pages_component_tsx_file)
|
||||
? root_pages_component_tsx_file
|
||||
: existsSync(root_pages_component_ts_file)
|
||||
? root_pages_component_ts_file
|
||||
: existsSync(root_pages_component_jsx_file)
|
||||
? root_pages_component_jsx_file
|
||||
: existsSync(root_pages_component_js_file)
|
||||
? root_pages_component_js_file
|
||||
: undefined;
|
||||
|
||||
const root_module = root_file
|
||||
? await import(`${root_file}?t=${global.LAST_BUILD_TIME ?? 0}`)
|
||||
: undefined;
|
||||
|
||||
const RootComponent = root_module?.default as FC<any> | undefined;
|
||||
|
||||
const component_file_path = root_module
|
||||
? `${file_path}`
|
||||
: `${file_path}?t=${global.LAST_BUILD_TIME ?? 0}`;
|
||||
|
||||
const module: BunextPageModule = await import(component_file_path);
|
||||
|
||||
const serverRes = await (async () => {
|
||||
try {
|
||||
if (routeParams) {
|
||||
return await module["server"]?.(routeParams);
|
||||
}
|
||||
return {};
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
const component = RootComponent ? (
|
||||
<RootComponent {...serverRes}>
|
||||
<Component {...serverRes} />
|
||||
</RootComponent>
|
||||
) : (
|
||||
<Component {...serverRes} />
|
||||
);
|
||||
|
||||
return { component, serverRes, routeParams, pageName, module };
|
||||
} catch (error: any) {
|
||||
const match = router.match("/500");
|
||||
|
||||
const filePath = match?.filePath || BUNX_ROOT_500_PRESET_COMPONENT;
|
||||
|
||||
// if (!match?.filePath) {
|
||||
// bundle({
|
||||
// out_dir: HYDRATION_DST_DIR,
|
||||
// src: `${BUNX_ROOT_500_PRESET_COMPONENT}`,
|
||||
// debug: true,
|
||||
// });
|
||||
// }
|
||||
|
||||
const module: BunextPageModule = await import(
|
||||
`${filePath}?t=${global.LAST_BUILD_TIME ?? 0}`
|
||||
);
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
const component = <Component />;
|
||||
|
||||
return {
|
||||
component,
|
||||
pageName: BUNX_ROOT_500_FILE_NAME,
|
||||
routeParams,
|
||||
module,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,47 +1,15 @@
|
||||
import type { FC } from "react";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import type { Server } from "bun";
|
||||
import grabPageName from "../../../utils/grab-page-name";
|
||||
import grabRouteParams from "../../../utils/grab-route-params";
|
||||
import genWebHTML from "./generate-web-html";
|
||||
import grabRouter from "../../../utils/grab-router";
|
||||
import type { BunextPageModule } from "../../../types";
|
||||
import grabPageComponent from "./grab-page-component";
|
||||
import writeWebPageHydrationScript from "./write-web-page-hydration-script";
|
||||
|
||||
type Params = {
|
||||
req: Request;
|
||||
server: Server;
|
||||
};
|
||||
|
||||
export default async function ({ req, server }: Params): Promise<Response> {
|
||||
const url = new URL(req.url);
|
||||
|
||||
export default async function ({ req }: Params): Promise<Response> {
|
||||
try {
|
||||
const router = grabRouter();
|
||||
const match = router.match(url.pathname);
|
||||
|
||||
if (!match?.filePath) {
|
||||
const errMsg = `Page ${url.pathname} not found`;
|
||||
console.error(errMsg);
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
|
||||
const pageName = grabPageName({ path: match.filePath });
|
||||
|
||||
const module: BunextPageModule = await import(match.filePath);
|
||||
// const config = module.config as ServerRouteConfig | undefined;
|
||||
|
||||
const routeParams = await grabRouteParams({ req, server });
|
||||
|
||||
const serverRes = await (async () => {
|
||||
try {
|
||||
return await module["server"]?.(routeParams);
|
||||
} catch (error) {
|
||||
return {};
|
||||
}
|
||||
})();
|
||||
|
||||
const Component = module.default as FC<any>;
|
||||
const component = <Component pageProps={serverRes} />;
|
||||
const { component, pageName, module, serverRes } =
|
||||
await grabPageComponent({ req });
|
||||
|
||||
const html = await genWebHTML({
|
||||
component,
|
||||
@@ -50,13 +18,20 @@ export default async function ({ req, server }: Params): Promise<Response> {
|
||||
module,
|
||||
});
|
||||
|
||||
// writeWebPageHydrationScript({
|
||||
// component,
|
||||
// pageName,
|
||||
// module,
|
||||
// pageProps: serverRes,
|
||||
// });
|
||||
|
||||
return new Response(html, {
|
||||
headers: {
|
||||
"Content-Type": "text/html",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
return new Response(`Page Not Found`, {
|
||||
} catch (error: any) {
|
||||
return new Response(error.message || `Page Not Found`, {
|
||||
status: 404,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,24 +2,22 @@ import { writeFileSync } from "fs";
|
||||
import path from "path";
|
||||
import grabDirNames from "../../../utils/grab-dir-names";
|
||||
import grabContants from "../../../utils/grab-constants";
|
||||
import genWebHTML from "./generate-web-html";
|
||||
import type { PageDistGenParams } from "../../../types";
|
||||
|
||||
const { BUNX_HYDRATION_SRC_DIR, HYDRATION_DST_DIR } = grabDirNames();
|
||||
const { BUNX_HYDRATION_SRC_DIR } = grabDirNames();
|
||||
|
||||
export default async function (params: PageDistGenParams) {
|
||||
const { pageName, page_file } = params;
|
||||
const { ClientRootElementIDName, ClientWindowPagePropsName } =
|
||||
await grabContants();
|
||||
|
||||
const PAGE_DIST_DIR = path.join(HYDRATION_DST_DIR, params.pageName);
|
||||
|
||||
const pageSrcTs = `index.tsx`;
|
||||
const pageSrcTsFileName = `${pageName}.tsx`;
|
||||
|
||||
let script = "";
|
||||
|
||||
script += `import React from "react";\n`;
|
||||
script += `import { hydrateRoot } from "react-dom/client";\n`;
|
||||
script += `import App from "./";\n`;
|
||||
script += `import App from "${page_file}";\n`;
|
||||
|
||||
script += `declare global {\n`;
|
||||
script += ` interface Window {\n`;
|
||||
@@ -30,12 +28,6 @@ export default async function (params: PageDistGenParams) {
|
||||
script += `const container = document.getElementById("${ClientRootElementIDName}");\n`;
|
||||
script += `hydrateRoot(container, <App {...window.${ClientWindowPagePropsName}} />);\n`;
|
||||
|
||||
const SRC_WRITE_FILE = path.join(PAGE_DIST_DIR, pageSrcTs);
|
||||
const SRC_WRITE_FILE = path.join(BUNX_HYDRATION_SRC_DIR, pageSrcTsFileName);
|
||||
writeFileSync(SRC_WRITE_FILE, script, "utf-8");
|
||||
|
||||
let html = await genWebHTML(params);
|
||||
const pageHtml = `index.html`;
|
||||
|
||||
const HTML_WRITE_FILE = path.join(PAGE_DIST_DIR, pageHtml);
|
||||
writeFileSync(HTML_WRITE_FILE, html, "utf-8");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
export default function DefaultServerErrorPage() {
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
width: "100vw",
|
||||
height: "100vh",
|
||||
overflow: "hidden",
|
||||
display: "flex",
|
||||
alignItems: "center",
|
||||
justifyContent: "center",
|
||||
}}
|
||||
>
|
||||
<span>500 Internal Server Error</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
+29
-6
@@ -63,7 +63,6 @@ export type GetRouteReturn = {
|
||||
export type BunxRouteParams = {
|
||||
req: Request;
|
||||
url: URL;
|
||||
server: Server;
|
||||
body?: any;
|
||||
query?: any;
|
||||
};
|
||||
@@ -120,7 +119,13 @@ export type BunextServerRouteConfig = {
|
||||
};
|
||||
|
||||
export type PageDistGenParams = {
|
||||
pageName: string;
|
||||
page_file: string;
|
||||
};
|
||||
|
||||
export type LivePageDistGenParams = {
|
||||
component: ReactNode;
|
||||
head?: ReactNode;
|
||||
pageProps?: any;
|
||||
module?: BunextPageModule;
|
||||
pageName: string;
|
||||
@@ -128,16 +133,34 @@ export type PageDistGenParams = {
|
||||
|
||||
export type BunextPageModule = {
|
||||
default: FC<any>;
|
||||
server?: (
|
||||
routeParams: BunxRouteParams,
|
||||
) => Promise<BunextPageModuleServerReturn>;
|
||||
server?: BunextPageServerFn;
|
||||
};
|
||||
|
||||
export type BunextPageModuleServerReturn = {
|
||||
props?: any;
|
||||
export type BunextPageServerFn<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
> = (routeParams: BunxRouteParams) => Promise<BunextPageModuleServerReturn<T>>;
|
||||
|
||||
export type BunextPageModuleServerReturn<
|
||||
T extends { [k: string]: any } = { [k: string]: any },
|
||||
> = {
|
||||
props?: T;
|
||||
};
|
||||
|
||||
export type BunextPageModuleMetadata = {
|
||||
title?: string;
|
||||
description?: string;
|
||||
};
|
||||
|
||||
export type GrabPageComponentRes = {
|
||||
component: JSX.Element;
|
||||
serverRes?: BunextPageModuleServerReturn;
|
||||
routeParams?: BunxRouteParams;
|
||||
pageName: string;
|
||||
module: BunextPageModule;
|
||||
};
|
||||
|
||||
export type PageFiles = {
|
||||
local_path: string;
|
||||
url_path: string;
|
||||
file_name: string;
|
||||
};
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import { execSync, type ExecSyncOptions } from "child_process";
|
||||
|
||||
type Params = {
|
||||
src: string;
|
||||
out_dir: string;
|
||||
minify?: boolean;
|
||||
exec_options?: ExecSyncOptions;
|
||||
debug?: boolean;
|
||||
};
|
||||
|
||||
export default function bundle({
|
||||
out_dir,
|
||||
src,
|
||||
minify = true,
|
||||
exec_options,
|
||||
debug,
|
||||
}: Params) {
|
||||
let cmd = `bun build`;
|
||||
|
||||
cmd += ` ${src} --outdir ${out_dir}`;
|
||||
|
||||
if (minify) {
|
||||
cmd += ` --minify`;
|
||||
}
|
||||
|
||||
if (debug) {
|
||||
console.log("cmd =>", cmd);
|
||||
}
|
||||
|
||||
execSync(cmd, {
|
||||
stdio: "inherit",
|
||||
...exec_options,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { existsSync, readdirSync, statSync } from "fs";
|
||||
import grabDirNames from "./grab-dir-names";
|
||||
import path from "path";
|
||||
import type { PageFiles } from "../types";
|
||||
|
||||
type Params = {
|
||||
exclude_api?: boolean;
|
||||
};
|
||||
|
||||
export default function grabAllPages(params?: Params) {
|
||||
const { PAGES_DIR } = grabDirNames();
|
||||
|
||||
const pages = grabPageDirRecursively({ page_dir: PAGES_DIR });
|
||||
|
||||
if (params?.exclude_api) {
|
||||
return pages.filter((p) => !Boolean(p.url_path.startsWith("/api/")));
|
||||
}
|
||||
|
||||
return pages;
|
||||
}
|
||||
|
||||
function grabPageDirRecursively({ page_dir }: { page_dir: string }) {
|
||||
const pages = readdirSync(page_dir);
|
||||
const pages_files: PageFiles[] = [];
|
||||
|
||||
const root_pages_file = grabPageFileObject({ file_path: `` });
|
||||
|
||||
if (root_pages_file) {
|
||||
pages_files.push(root_pages_file);
|
||||
}
|
||||
|
||||
for (let i = 0; i < pages.length; i++) {
|
||||
const page = pages[i];
|
||||
const full_page_path = path.join(page_dir, page);
|
||||
|
||||
if (!existsSync(full_page_path)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (page.match(/__root\.(tx|js)x?/)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (page.match(/\(|\)/)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const page_stat = statSync(full_page_path);
|
||||
|
||||
if (page_stat.isDirectory()) {
|
||||
if (page.match(/\(|\)/)) continue;
|
||||
const new_page_files = grabPageDirRecursively({
|
||||
page_dir: full_page_path,
|
||||
});
|
||||
pages_files.push(...new_page_files);
|
||||
} else if (page.match(/\.(ts|js)x?$/)) {
|
||||
const pages_file = grabPageFileObject({
|
||||
file_path: full_page_path,
|
||||
});
|
||||
|
||||
if (pages_file) {
|
||||
pages_files.push(pages_file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pages_files;
|
||||
}
|
||||
|
||||
function grabPageFileObject({
|
||||
file_path,
|
||||
}: {
|
||||
file_path: string;
|
||||
}): PageFiles | undefined {
|
||||
let url_path = file_path
|
||||
.replace(/.*\/pages\//, "/")
|
||||
?.replace(/\.(ts|js)x?$/, "");
|
||||
|
||||
let file_name = url_path.split("/").pop();
|
||||
if (!file_name) return;
|
||||
|
||||
return {
|
||||
local_path: file_path,
|
||||
url_path,
|
||||
file_name,
|
||||
};
|
||||
}
|
||||
@@ -3,6 +3,7 @@ const AppNames = {
|
||||
defaultAssetPrefix: "_bunext/static",
|
||||
name: "Bunext",
|
||||
defaultDistDir: ".bunext",
|
||||
RootPagesComponentName: "__root",
|
||||
} as const;
|
||||
|
||||
export default AppNames;
|
||||
|
||||
+16
-54
@@ -3,10 +3,10 @@ import path from "path";
|
||||
export default function grabDirNames() {
|
||||
const ROOT_DIR = process.cwd();
|
||||
const SRC_DIR = path.join(ROOT_DIR, "src");
|
||||
const ROUTES_DIR = path.join(SRC_DIR, "routes");
|
||||
const API_DIR = path.join(ROUTES_DIR, "api");
|
||||
const PAGES_DIR = path.join(SRC_DIR, "pages");
|
||||
const API_DIR = path.join(PAGES_DIR, "api");
|
||||
const PUBLIC_DIR = path.join(ROOT_DIR, "public");
|
||||
const HYDRATION_DST_DIR = path.join(PUBLIC_DIR, "routes");
|
||||
const HYDRATION_DST_DIR = path.join(PUBLIC_DIR, "pages");
|
||||
const CONFIG_FILE = path.join(ROOT_DIR, "bunext.config.ts");
|
||||
|
||||
const BUNX_CWD_DIR = path.resolve(ROOT_DIR, ".bunext");
|
||||
@@ -14,15 +14,22 @@ export default function grabDirNames() {
|
||||
const BUNX_HYDRATION_SRC_DIR = path.resolve(
|
||||
BUNX_CWD_DIR,
|
||||
"client",
|
||||
"hydration-src"
|
||||
"hydration-src",
|
||||
);
|
||||
|
||||
const BUNX_ROOT_DIR = path.resolve(__dirname, "../../");
|
||||
const BUNX_ROOT_SRC_DIR = path.join(BUNX_ROOT_DIR, "src");
|
||||
const BUNX_ROOT_PRESETS_DIR = path.join(BUNX_ROOT_SRC_DIR, "presets");
|
||||
const BUNX_ROOT_500_FILE_NAME = `server-error`;
|
||||
const BUNX_ROOT_500_PRESET_COMPONENT = path.join(
|
||||
BUNX_ROOT_PRESETS_DIR,
|
||||
`${BUNX_ROOT_500_FILE_NAME}.tsx`,
|
||||
);
|
||||
|
||||
return {
|
||||
ROOT_DIR,
|
||||
SRC_DIR,
|
||||
ROUTES_DIR,
|
||||
PAGES_DIR,
|
||||
API_DIR,
|
||||
PUBLIC_DIR,
|
||||
HYDRATION_DST_DIR,
|
||||
@@ -30,54 +37,9 @@ export default function grabDirNames() {
|
||||
CONFIG_FILE,
|
||||
BUNX_TMP_DIR,
|
||||
BUNX_HYDRATION_SRC_DIR,
|
||||
BUNX_ROOT_SRC_DIR,
|
||||
BUNX_ROOT_PRESETS_DIR,
|
||||
BUNX_ROOT_500_PRESET_COMPONENT,
|
||||
BUNX_ROOT_500_FILE_NAME,
|
||||
};
|
||||
}
|
||||
|
||||
// const rootDir = params?.dir || process.cwd();
|
||||
// const appDir = path.resolve(__dirname, "..");
|
||||
// const entrypoint = path.join(
|
||||
// appDir,
|
||||
// "functions",
|
||||
// "server",
|
||||
// "start-server.ts"
|
||||
// );
|
||||
|
||||
// const bunextDir = path.join(rootDir, ".bunext");
|
||||
// const bunextClientDir = path.join(bunextDir, "client");
|
||||
// const bunextClientRoutesDir = path.join(bunextClientDir, "routes");
|
||||
// const bunextClientRoutesSrcDir = path.join(bunextClientRoutesDir, "src");
|
||||
// const bunextClientRoutesDstDir = path.join(bunextClientRoutesDir, "dst");
|
||||
|
||||
// const bunextServerDir = path.join(bunextDir, "server");
|
||||
// const bunextServerPagesDir = path.join(bunextServerDir, "pages");
|
||||
|
||||
// const publicDir = path.join(rootDir, "public");
|
||||
// const configFile = path.join(rootDir, "bunext.config.ts");
|
||||
|
||||
// const srcDir = path.join(rootDir, "src");
|
||||
// const pagesDir = path.join(srcDir, "pages");
|
||||
// const componentsDir = path.join(srcDir, "components");
|
||||
// const stylesDir = path.join(srcDir, "styles");
|
||||
// const utilsDir = path.join(srcDir, "utils");
|
||||
// const typesDir = path.join(srcDir, "types");
|
||||
|
||||
// return {
|
||||
// rootDir,
|
||||
// pagesDir,
|
||||
// componentsDir,
|
||||
// publicDir,
|
||||
// stylesDir,
|
||||
// utilsDir,
|
||||
// typesDir,
|
||||
// configFile,
|
||||
// appDir,
|
||||
// entrypoint,
|
||||
// srcDir,
|
||||
// bunextDir,
|
||||
// bunextClientDir,
|
||||
// bunextClientRoutesDir,
|
||||
// bunextClientRoutesSrcDir,
|
||||
// bunextClientRoutesDstDir,
|
||||
// bunextServerDir,
|
||||
// bunextServerPagesDir,
|
||||
// };
|
||||
|
||||
@@ -5,14 +5,24 @@ type Params = {
|
||||
export default function grabPageName(params: Params) {
|
||||
const pathArr = params.path.split("/");
|
||||
|
||||
const routesIndex = pathArr.findIndex((p) => p == "routes");
|
||||
const routesIndex = pathArr.findIndex((p) => p == "pages");
|
||||
|
||||
const newPathArr = [...pathArr].slice(routesIndex + 1);
|
||||
|
||||
const filename = newPathArr
|
||||
.filter((p) => Boolean(p.match(/./)))
|
||||
.map((p) => p.replace(/\.\w+$/, "").replace(/[^a-z]/g, ""))
|
||||
.map((p) =>
|
||||
p
|
||||
.replace(/\.\w+$/, "")
|
||||
.replace(/\[/g, "-")
|
||||
.replace(/\.\.\./g, "-")
|
||||
.replace(/[^a-z\-]/g, ""),
|
||||
)
|
||||
.join("-");
|
||||
|
||||
if (filename.endsWith(`-index`)) {
|
||||
return filename.replace(/-index$/, "");
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
@@ -4,12 +4,10 @@ import deserializeQuery from "./deserialize-query";
|
||||
|
||||
type Params = {
|
||||
req: Request;
|
||||
server: Server;
|
||||
};
|
||||
|
||||
export default async function grabRouteParams({
|
||||
req,
|
||||
server,
|
||||
}: Params): Promise<BunxRouteParams> {
|
||||
const url = new URL(req.url);
|
||||
|
||||
@@ -26,7 +24,6 @@ export default async function grabRouteParams({
|
||||
const routeParams: BunxRouteParams = {
|
||||
req,
|
||||
url,
|
||||
server,
|
||||
query,
|
||||
body,
|
||||
};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import grabDirNames from "./grab-dir-names";
|
||||
|
||||
export default function grabRouter() {
|
||||
const { ROUTES_DIR } = grabDirNames();
|
||||
const { PAGES_DIR } = grabDirNames();
|
||||
|
||||
if (process.env.NODE_ENV == "production") {
|
||||
return global.ROUTER;
|
||||
@@ -9,6 +9,6 @@ export default function grabRouter() {
|
||||
|
||||
return new Bun.FileSystemRouter({
|
||||
style: "nextjs",
|
||||
dir: ROUTES_DIR,
|
||||
dir: PAGES_DIR,
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user