diff --git a/app/srv/ws/sync/editor/code/build.ts b/app/srv/ws/sync/editor/code/build.ts index b5687bfb..a41d9b93 100644 --- a/app/srv/ws/sync/editor/code/build.ts +++ b/app/srv/ws/sync/editor/code/build.ts @@ -8,10 +8,14 @@ import { DBCode } from "./prep-code"; import { activity } from "../../entity/activity"; import { sendWS } from "../../sync-handler"; import { SyncType } from "../../type"; +import { gzipAsync } from "../../entity/zlib"; +const encoder = new TextEncoder(); export const codeBuild = async (code: DBCode) => { try { const id_code = code.id; + const outfile = dir.path(`${g.datadir}/site/build/${id_code}/index.js`); + if (!Code.build.ctx[id_code]) { Code.build.ctx[id_code] = await context({ absWorkingDir: dir.path( @@ -20,7 +24,7 @@ export const codeBuild = async (code: DBCode) => { entryPoints: ["index.tsx"], bundle: true, format: "cjs", - outfile: dir.path(`${g.datadir}/site/build/${id_code}/index.js`), + outfile, minify: true, treeShaking: true, sourcemap: true, @@ -55,6 +59,14 @@ export const codeBuild = async (code: DBCode) => { }); }); const result = await Code.build.ctx[id_code].rebuild(); + const out = Bun.file(outfile); + const src = (await out.text()).replace( + "//# sourceMappingURL=index.js.map", + `//# sourceMappingURL=/nova-load/code/${id_code}/index.js.map` + ); + Bun.write(out, src); + const srcgz = await gzipAsync(encoder.encode(src)); + activity.site .room(code.id_site) .findAll({ site_js: code.name }) @@ -66,6 +78,7 @@ export const codeBuild = async (code: DBCode) => { name: code.name, id: code.id, event: "code-done", + src: srcgz, content: result.errors.length > 0 ? `${result.errors.join("\n")}` diff --git a/app/web/src/nova/ed/logic/ed-sync.tsx b/app/web/src/nova/ed/logic/ed-sync.tsx index 70b4dc6a..89649d4f 100644 --- a/app/web/src/nova/ed/logic/ed-sync.tsx +++ b/app/web/src/nova/ed/logic/ed-sync.tsx @@ -6,6 +6,7 @@ import { w } from "../../../utils/types/general"; import { Loading } from "../../../utils/ui/loading"; import { EmptySite, PG } from "./ed-global"; import { treeRebuild } from "./tree/build"; +import { evalCJS } from "../../view/logic/load-code"; const decoder = new TextDecoder(); @@ -78,6 +79,17 @@ export const edInitSync = (p: PG) => { p.ui.popup.code.log += arg.content; } p.ui.popup.code.loading = false; + + if (arg.src) { + const w = window as any; + console.clear(); + const module = evalCJS(decoder.decode(decompress(arg.src))); + if (typeof module === "object") { + for (const [k, v] of Object.entries(module)) { + w[k] = v; + } + } + } p.render(); } else { if (typeof arg.content === "string") diff --git a/app/web/src/nova/ed/panel/main/main.tsx b/app/web/src/nova/ed/panel/main/main.tsx index cf1e72aa..5e089652 100644 --- a/app/web/src/nova/ed/panel/main/main.tsx +++ b/app/web/src/nova/ed/panel/main/main.tsx @@ -12,6 +12,7 @@ export const EdMain = () => { {!p.page.building && ( { - const { load, site_id, page_id } = arg; + const { load, site_id, page_id, mode, isEditor } = arg; + + w.isDesktop = mode !== "mobile"; + w.isMobile = mode === "mobile"; + w.isEditor = isEditor; v.status = "load-code"; v.current.site_id = site_id; diff --git a/app/web/src/nova/view/logic/load-code.ts b/app/web/src/nova/view/logic/load-code.ts index 0cf2647a..d8a4e2be 100644 --- a/app/web/src/nova/view/logic/load-code.ts +++ b/app/web/src/nova/view/logic/load-code.ts @@ -107,23 +107,24 @@ export const loadCompCode = async (comp_id: string) => { }; const importCJS = async (url: string) => { - const module = { exports: { __esModule: true as true | undefined } }; const res = await fetch(url); - const src = await res.text(); + return evalCJS(src); +}; + +export const evalCJS = (src: string) => { if (src) { - try { - const fn = new Function("module", src); - await fn(module); - - const result = { ...module.exports }; - if (result.__esModule) { - delete result.__esModule; - } - - return result; - } catch (e) {} + const module = { exports: { __esModule: true as true | undefined } }; + eval(`try { + ${src} + } catch(e) { + console.error(e); + }`); + const result = { ...module.exports }; + if (result.__esModule) { + delete result.__esModule; + } + return result; } - return {}; }; diff --git a/app/web/src/nova/view/view.tsx b/app/web/src/nova/view/view.tsx index 2df67e18..2805f37c 100644 --- a/app/web/src/nova/view/view.tsx +++ b/app/web/src/nova/view/view.tsx @@ -1,4 +1,4 @@ -import { FC } from "react"; +import { FC, Suspense } from "react"; import { useGlobal } from "web-utils"; import { IContent } from "../../utils/types/general"; import { Loading } from "../../utils/ui/loading"; @@ -7,19 +7,33 @@ import { vInit } from "./logic/init"; import { vLoadCode } from "./logic/load-code"; import { VLoad, VLoadComponent } from "./logic/types"; import { VEntry } from "./render/entry"; +import { ErrorBox } from "./render/meta/script/error-box"; -export const View: FC<{ +type ViewProp = { load: VLoad; component: VLoadComponent; site_id: string; page_id: string; api_url: string; mode: "desktop" | "mobile"; + isEditor?: boolean; bind?: (arg: { render: () => void }) => void; hidden?: (item: IContent) => boolean; hover?: { get: (item: IContent) => boolean; set: (id: string) => void }; active?: { get: (item: IContent) => boolean; set: (id: string) => void }; -}> = ({ +}; + +export const View: FC = (props) => { + return ( + + + + + + ); +}; + +const BoxedView: FC = ({ load, site_id, page_id, @@ -29,6 +43,8 @@ export const View: FC<{ hidden, component, api_url, + mode, + isEditor, }) => { const v = useGlobal(ViewGlobal, "VIEW"); @@ -52,7 +68,7 @@ export const View: FC<{ } if (v.status === "init") { - vInit(v, { load, page_id, site_id }); + vInit(v, { load, page_id, site_id, mode, isEditor: !!isEditor }); if (v.status === "init") { return ; } @@ -61,7 +77,11 @@ export const View: FC<{ if (v.status === "load-code" || v.status === "loading-code") { vLoadCode(v); if (v.status === "load-code" || v.status === "loading-code") { - return ; + return ( + <> + + + ); } } @@ -76,3 +96,4 @@ export const View: FC<{ return
{v.bodyCache}
; }; + diff --git a/app/web/src/utils/sync/ws-client.ts b/app/web/src/utils/sync/ws-client.ts index 12234c74..acfbf942 100644 --- a/app/web/src/utils/sync/ws-client.ts +++ b/app/web/src/utils/sync/ws-client.ts @@ -80,6 +80,7 @@ export const clientStartSync = async (arg: { id: string; event: "code-loading" | "code-done"; content?: string; + src?: Uint8Array; }) => void; activity: (arg: { activity: string;