This commit is contained in:
Benjamin Toby 2026-03-15 09:03:20 +01:00
parent ec2dc0c4dc
commit 075e2f88d6
5 changed files with 42 additions and 15 deletions

View File

@ -28,6 +28,7 @@ export default function watcher() {
global.RECOMPILING = true;
await allPagesBundler();
reloadServer();
global.LAST_BUILD_TIME = Date.now();

View File

@ -23,7 +23,9 @@ export default async function genWebHTML({
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();
const cssExists = await Bun.file(
path.join(HYDRATION_DST_DIR, pageName + ".css"),
).exists();
let html = `<!DOCTYPE html>\n`;
html += `<html>\n`;
@ -32,16 +34,16 @@ export default async function genWebHTML({
if (cssExists) {
html += ` <link rel="stylesheet" href="${CSS_SRC}" />\n`;
}
if (isDevelopment()) {
html += `<script>
const hmr = new EventSource("/__hmr");
hmr.addEventListener("update", (event) => {
if (event.data === "reload") {
window.location.reload();
}
});
</script>\n`;
}
// if (isDevelopment()) {
// html += `<script>
// const hmr = new EventSource("/__hmr");
// hmr.addEventListener("update", (event) => {
// if (event.data === "reload") {
// window.location.reload();
// }
// });
// </script>\n`;
// }
html += ` </head>\n`;
html += ` <body>\n`;
html += ` <div id="${ClientRootElementIDName}">${componentHTML}</div>\n`;

View File

@ -1,6 +1,5 @@
import genWebHTML from "./generate-web-html";
import grabPageComponent from "./grab-page-component";
import writeWebPageHydrationScript from "./write-web-page-hydration-script";
type Params = {
req: Request;

View File

@ -3,6 +3,7 @@ import path from "path";
import grabDirNames from "../../../utils/grab-dir-names";
import grabContants from "../../../utils/grab-constants";
import type { PageDistGenParams } from "../../../types";
import isDevelopment from "../../../utils/is-development";
const { BUNX_HYDRATION_SRC_DIR } = grabDirNames();
@ -25,8 +26,26 @@ export default async function (params: PageDistGenParams) {
script += ` }\n`;
script += `}\n`;
script += `const container = document.getElementById("${ClientRootElementIDName}");\n`;
script += `hydrateRoot(container, <App {...window.${ClientWindowPagePropsName}} />);\n`;
script += `let root: any = null;\n\n`;
script += `const container = document.getElementById("${ClientRootElementIDName}");\n\n`;
script += `if (container) {\n`;
script += ` root = hydrateRoot(container, <App {...window.${ClientWindowPagePropsName}} />);\n`;
script += `}\n\n`;
if (isDevelopment()) {
script += `const hmr = new EventSource("/__hmr");\n`;
script += `hmr.addEventListener("update", (event) => {\n`;
// script += ` console.log(\`HMR even received:\`, event);\n`;
script += ` if (event.data && root) {\n`;
script += ` console.log(\`HMR Changes Detected. Reloading ...\`);\n`;
// script += ` console.log("root", root);\n`;
// script += ` root.unmount();\n`;
// script += ` const container = document.getElementById("${ClientRootElementIDName}");\n\n`;
// script += ` root = hydrateRoot(container!, <App {...window.${ClientWindowPagePropsName}} />);\n`;
script += ` root.render(<App {...window.${ClientWindowPagePropsName}} />);\n`;
// script += ` window.location.reload();\n`;
script += ` }\n`;
script += ` });\n`;
}
const SRC_WRITE_FILE = path.join(BUNX_HYDRATION_SRC_DIR, pageSrcTsFileName);
writeFileSync(SRC_WRITE_FILE, script, "utf-8");

View File

@ -1,7 +1,13 @@
export default function isDevelopment() {
const config = global.CONFIG;
if (config.development) return true;
if (process.env.NODE_ENV == "production") {
return false;
}
if (config.development) {
return true;
}
return false;
}