diff --git a/app/srv/api/prod.ts b/app/srv/api/prod.ts index 4c864cfc..24038ef8 100644 --- a/app/srv/api/prod.ts +++ b/app/srv/api/prod.ts @@ -116,7 +116,7 @@ export const _ = { const root = `/code/${site_id}/site/src`; await ensureLib(root, site_id); await ensureFiles(root, site_id); - await initFrontEnd(root, site_id, true); + await initFrontEnd(root, site_id); await new Promise((resolve) => { const ival = setInterval(async () => { file = Bun.file(build_path); diff --git a/app/srv/ws/sync/code/parts/init/frontend.ts b/app/srv/ws/sync/code/parts/init/frontend.ts index 17c32e04..7766bb1d 100644 --- a/app/srv/ws/sync/code/parts/init/frontend.ts +++ b/app/srv/ws/sync/code/parts/init/frontend.ts @@ -11,7 +11,7 @@ import { conns } from "../../../entity/conn"; import { SyncType } from "../../../type"; import { sendWS } from "../../../sync-handler"; import { removeAsync } from "fs-jetpack"; - +import { watch } from "fs"; const decoder = new TextDecoder(); export const initFrontEnd = async ( root: string, @@ -23,6 +23,7 @@ export const initFrontEnd = async ( if (existing) { if (force) { try { + existing.watch.close(); await existing.ctx.dispose(); delete code.internal.frontend[id_site]; } catch (e) {} @@ -103,8 +104,27 @@ export const initFrontEnd = async ( }, ], }); - code.internal.frontend[id_site] = { ctx: build_ctx, timeout: null }; - await build_ctx.watch(); + code.internal.frontend[id_site] = { + ctx: build_ctx, + timeout: null, + rebuilding: false, + watch: watch(dir.data(root), async (event, filename) => { + const ctx = code.internal.frontend[id_site]; + if ( + ctx && + (filename?.endsWith(".tsx") || + filename?.endsWith(".ts") || + filename?.endsWith(".css") || + filename?.endsWith(".html")) + ) { + if (!ctx.rebuilding) { + ctx.rebuilding = true; + await ctx.ctx.rebuild(); + ctx.rebuilding = false; + } + } + }), + }; } catch (e: any) { console.error("Error building front end", id_site); delete code.internal.frontend[id_site]; diff --git a/app/srv/ws/sync/code/parts/internal.ts b/app/srv/ws/sync/code/parts/internal.ts index 9df267af..efa03107 100644 --- a/app/srv/ws/sync/code/parts/internal.ts +++ b/app/srv/ws/sync/code/parts/internal.ts @@ -18,7 +18,7 @@ export const codeInternal = { if (!g.prasi_code.frontend) g.prasi_code.frontend = {}; return g.prasi_code.frontend as Record< SITE_ID, - { ctx: BuildContext; timeout: any } + { ctx: BuildContext; timeout: any, watch: FSWatcher, rebuilding: boolean } >; }, get typings() { diff --git a/app/web/src/nova/ed/logic/tree/build/push-tree.tsx b/app/web/src/nova/ed/logic/tree/build/push-tree.tsx index 5399cc15..98181c65 100644 --- a/app/web/src/nova/ed/logic/tree/build/push-tree.tsx +++ b/app/web/src/nova/ed/logic/tree/build/push-tree.tsx @@ -34,8 +34,8 @@ export const pushTreeNode = ( ?.forEach((v, k) => { if (typeof v === "object") { if ((v as any)?.get("meta")?.get("type") === "content-element") { - const mitem = (v as any).get("content") as MItem; - if (meta.item.id === mitem.get("id")) { + const mitem = (v as any)?.get("content") as MItem; + if (mitem && meta.item.id === mitem.get("id")) { meta.mitem = mitem; } }