From 6dd00f256109f7eae224051f167ac1da3babcabb Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Thu, 5 Mar 2026 05:50:41 +0100 Subject: [PATCH] Updates --- CLAUDE.md | 106 ++++++++++++++++++++++ src/functions/server/server-params-gen.ts | 4 + src/functions/server/watcher.tsx | 26 +++++- 3 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 CLAUDE.md diff --git a/CLAUDE.md b/CLAUDE.md new file mode 100644 index 0000000..1ee6890 --- /dev/null +++ b/CLAUDE.md @@ -0,0 +1,106 @@ + +Default to using Bun instead of Node.js. + +- Use `bun ` instead of `node ` or `ts-node ` +- Use `bun test` instead of `jest` or `vitest` +- Use `bun build ` instead of `webpack` or `esbuild` +- Use `bun install` instead of `npm install` or `yarn install` or `pnpm install` +- Use `bun run + + +``` + +With the following `frontend.tsx`: + +```tsx#frontend.tsx +import React from "react"; + +// import .css files directly and it works +import './index.css'; + +import { createRoot } from "react-dom/client"; + +const root = createRoot(document.body); + +export default function Frontend() { + return

Hello, world!

; +} + +root.render(); +``` + +Then, run index.ts + +```sh +bun --hot ./index.ts +``` + +For more information, read the Bun API docs in `node_modules/bun-types/docs/**.md`. diff --git a/src/functions/server/server-params-gen.ts b/src/functions/server/server-params-gen.ts index 0edf440..8d2932d 100644 --- a/src/functions/server/server-params-gen.ts +++ b/src/functions/server/server-params-gen.ts @@ -55,6 +55,10 @@ export default async function (params?: Params): Promise { ), ); + return new Response(file); + } else if (url.pathname.startsWith("/favicon.")) { + const file = Bun.file(path.join(PUBLIC_DIR, url.pathname)); + return new Response(file); } else { return await handleWebPages({ req, server }); diff --git a/src/functions/server/watcher.tsx b/src/functions/server/watcher.tsx index 0abe40e..8f9e452 100644 --- a/src/functions/server/watcher.tsx +++ b/src/functions/server/watcher.tsx @@ -1,12 +1,9 @@ import { watch } from "fs"; import grabDirNames from "../../utils/grab-dir-names"; -import writeWebPageHydrationScript from "./web-pages/write-web-page-hydration-script"; import grabPageName from "../../utils/grab-page-name"; import path from "path"; import { execSync } from "child_process"; import serverParamsGen from "./server-params-gen"; -import grabRouter from "../../utils/grab-router"; -import type { FC } from "react"; const { ROOT_DIR, BUNX_HYDRATION_SRC_DIR, HYDRATION_DST_DIR, ROUTES_DIR } = grabDirNames(); @@ -55,9 +52,18 @@ export default function watcher() { // }); // } + // 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" }); @@ -67,11 +73,21 @@ export default function watcher() { }); const encoder = new TextEncoder(); - const msg = encoder.encode(`event: update\ndata: reload\n\n`); + const msg = encoder.encode( + `event: update\ndata: reload\n\n`, + ); for (const controller of global.HMR_CONTROLLERS) { - controller.enqueue(msg); + controller.enqueue(msg.toString()); } + + // 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(); global.RECOMPILING = false; } else if (event == "rename") { await reloadServer();