diff --git a/src/functions/server/watcher.tsx b/src/functions/server/watcher.tsx index ae1b463..fa14e7b 100644 --- a/src/functions/server/watcher.tsx +++ b/src/functions/server/watcher.tsx @@ -28,6 +28,7 @@ export default function watcher() { global.RECOMPILING = true; await allPagesBundler(); + reloadServer(); global.LAST_BUILD_TIME = Date.now(); diff --git a/src/functions/server/web-pages/generate-web-html.tsx b/src/functions/server/web-pages/generate-web-html.tsx index 2a05a7b..eff7967 100644 --- a/src/functions/server/web-pages/generate-web-html.tsx +++ b/src/functions/server/web-pages/generate-web-html.tsx @@ -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 = `\n`; html += `\n`; @@ -32,16 +34,16 @@ export default async function genWebHTML({ if (cssExists) { html += ` \n`; } - if (isDevelopment()) { - html += `\n`; - } + // if (isDevelopment()) { + // html += `\n`; + // } html += ` \n`; html += ` \n`; html += `
${componentHTML}
\n`; diff --git a/src/functions/server/web-pages/handle-web-pages.tsx b/src/functions/server/web-pages/handle-web-pages.tsx index 643ceba..2a9fe99 100644 --- a/src/functions/server/web-pages/handle-web-pages.tsx +++ b/src/functions/server/web-pages/handle-web-pages.tsx @@ -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; diff --git a/src/functions/server/web-pages/write-web-page-hydration-script.tsx b/src/functions/server/web-pages/write-web-page-hydration-script.tsx index 92a5f1b..66db5f5 100644 --- a/src/functions/server/web-pages/write-web-page-hydration-script.tsx +++ b/src/functions/server/web-pages/write-web-page-hydration-script.tsx @@ -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, );\n`; + script += `let root: any = null;\n\n`; + script += `const container = document.getElementById("${ClientRootElementIDName}");\n\n`; + script += `if (container) {\n`; + script += ` root = hydrateRoot(container, );\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!, );\n`; + script += ` root.render();\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"); diff --git a/src/utils/is-development.ts b/src/utils/is-development.ts index 108a411..fa73505 100644 --- a/src/utils/is-development.ts +++ b/src/utils/is-development.ts @@ -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; }