fix esbuild

This commit is contained in:
rizrmd 2024-06-03 11:17:26 +07:00
parent ec36cdab7b
commit 15147d9160
4 changed files with 27 additions and 7 deletions

View File

@ -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<void>((resolve) => {
const ival = setInterval(async () => {
file = Bun.file(build_path);

View File

@ -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];

View File

@ -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() {

View File

@ -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;
}
}