This commit is contained in:
Rizky 2024-03-20 21:21:01 +07:00
parent 091a7e4f47
commit 2a7617c28a
17 changed files with 200 additions and 288 deletions

View File

@ -7,5 +7,6 @@
"**/.DS_Store": true,
"**/Thumbs.db": true
},
"hide-files.files": []
"hide-files.files": [],
"typescript.tsdk": "node_modules/typescript/lib"
}

File diff suppressed because one or more lines are too long

View File

@ -81,7 +81,7 @@ export const code_action: SAction["code"]["action"] = async function (
) {
return { type: "check-typings", hash: true };
}
} catch (e) { }
} catch (e) {}
return { type: "check-typings", hash: false };
}
case "push-typings": {

View File

@ -65,10 +65,9 @@ export const site_load: SAction["site"]["load"] = async function (
meta: undefined,
entry: [],
},
code: {
snapshot: compressed,
mode: site.code_mode as "old" | "vsc",
},
// code: {
// snapshot: compressed,
// },
};
}
}

View File

@ -155,10 +155,11 @@ if (typeof global.server_hook === "function") {
absWorkingDir: src_path,
entryPoints: ["index.tsx"],
bundle: true,
outfile: build_file,
outdir: build_path,
minify: true,
treeShaking: true,
format: "cjs",
format: "esm",
splitting: true,
logLevel: "silent",
sourcemap: true,
plugins: [
@ -212,13 +213,13 @@ if (typeof global.server_hook === "function") {
}
}
const build_file = code.path(id_site, mode, "build", "index.js");
const out = Bun.file(build_file);
const src = (await out.text()).replace(
"//# sourceMappingURL=index.js.map",
`//# sourceMappingURL=/nova-load/code/${id_site}/${mode}/index.js.map`
);
// await Bun.write(out, src);
// const build_file = code.path(id_site, mode, "build", "index.js");
// const out = Bun.file(build_file);
// const src = (await out.text()).replace(
// "//# sourceMappingURL=index.js.map",
// `//# sourceMappingURL=/nova-load/code/${id_site}/${mode}/index.js.map`
// );
// // await Bun.write(out, src);
}
};
@ -252,8 +253,8 @@ const codeError = async (
const path = code.path(
id_site,
"site",
"build",
mode === "server" ? "server.log" : "error.log"
"src",
mode === "server" ? "server.log" : "index.log"
);
await Bun.write(path, error);

View File

@ -4,10 +4,19 @@ import { BuildContext } from "esbuild";
import { dirAsync, exists, existsAsync, writeAsync } from "fs-jetpack";
import { dirname } from "path";
export type CodeMode = "site" | "server";
export type CodeBuild = {
server: BuildContext | null;
site: BuildContext | null;
};
export type CodeMode = keyof CodeBuild;
export const code = {
path(id_site: string, mode: CodeMode, type: "src" | "build", path?: string) {
path(
id_site: string,
mode: CodeMode,
type: "src" | "build" | "build_cache",
path?: string
) {
let file_path = "";
if (path) {
file_path = path[0] === "/" ? path : `/${path}`;
@ -15,7 +24,7 @@ export const code = {
return dir.data(`/code/${id_site}/${mode}/${type}${file_path}`);
},
server: {} as Record<string, {}>,
esbuild: {} as Record<string, Record<CodeMode, null | BuildContext>>,
esbuild: {} as Record<string, CodeBuild>,
prep(id_site: string, mode: CodeMode) {
if (exists(dir.data(""))) {
Bun.spawn({

View File

@ -86,9 +86,6 @@
"@types/react-is": "^18.2.3",
"@types/textdiff-create": "^1.0.1",
"@types/ua-parser-js": "^0.7.38",
"@parcel/config-default": "^2.10.3",
"@parcel/core": "^2.10.3",
"@parcel/transformer-raw": "^2.10.3",
"@tinijs/parcel-reporter-copy-public": "0.0.2",
"@types/lodash.capitalize": "4.2.8",
"@types/lodash.debounce": "^4.0.8",
@ -101,7 +98,7 @@
"@types/tinycolor2": "^1.4.5",
"@types/uuid": "9.0.6",
"@types/vscode": "^1.83.1",
"parcel": "^2.10.3",
"parcel": "^2.12.0",
"parcel-bundler-splitable": "0.0.4",
"postcss": "8.4.31",
"tailwindcss": "3.3.4"

View File

@ -24,18 +24,17 @@ export const EmptySite = {
meta: undefined as void | Record<string, IMeta>,
entry: [] as string[],
},
code: {
snapshot: {} as
| undefined
| Record<
string,
{
id_doc: number;
bin: Uint8Array;
}
>,
mode: "old" as "old" | "vsc",
},
// code: {
// snapshot: {} as
// | undefined
// | Record<
// string,
// {
// id_doc: number;
// bin: Uint8Array;
// }
// >,
// },
};
export type ESite = typeof EmptySite;
@ -297,7 +296,7 @@ export const EDGlobal = {
open: false,
mode: "js" as "js" | "css" | "html",
lastMode: "js" as "js" | "css" | "html",
type: "item" as "item" | "prop-master" | "prop-instance",
type: "item" as "item" | "prop-master" | "prop-instance" | "comp-types",
prop_kind: "" as PropFieldKind,
prop_name: "",
on_close: () => {},

View File

@ -6,13 +6,13 @@ export const loadSite = async (p: PG, site: ESite, note: string) => {
p.site = site;
if (!p.script.db && !p.script.api) {
if (p.site.code.mode === "old" && !location.pathname.startsWith('/ed/')) {
if (!location.pathname.startsWith("/ed/")) {
await viLoadLegacy({
mode: p.mode,
site: {
api_url: p.site.config.api_url,
id: p.site.id,
api: {
api: {
get() {
return p.script.api;
},

View File

@ -34,7 +34,6 @@ export const loadSession = (p: PG) => {
p.user.id = "ab1390f5-40d5-448e-a8c3-84b0fb600930";
p.user.username = "anonymous";
}
};
export const edInitSync = (p: PG) => {
@ -242,20 +241,3 @@ export const edInitSync = (p: PG) => {
}
return true;
};
export const evalCJS = (src: string) => {
if (src) {
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 {};
};

View File

@ -87,8 +87,6 @@ const CodeBody = () => {
? "http://localhost:3000?"
: "https://prasi-vsc.avolut.com/?tkn=prasi&";
const code_mode = p.site.code.mode;
return (
<div className="relative w-full h-full flex flex-col">
<div className="border-b flex justify-between h-[40px] items-stretch">
@ -246,43 +244,6 @@ const CodeBody = () => {
></div>
</Tooltip>
</div>
{/* <div className="flex items-center">
{code_mode === "vsc" && (
<div
className="flex items-center p-[2px] px-2 mr-2 cursor-pointer text-[11px] space-x-1 hover:bg-blue-100 hover:border-slate-200 border border-transparent transition-all"
onClick={async () => {
if (
confirm(
"Are you sure want to turn off VSCode?\nThis will enable old npm module"
)
) {
localStorage.vsc_opened = "yes";
await _db.site.update({
where: { id: p.site.id },
data: { code_mode: "old" },
});
location.reload();
}
}}
>
<svg
width="15"
height="15"
viewBox="0 0 15 15"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M10.5 4C8.567 4 7 5.567 7 7.5C7 9.433 8.567 11 10.5 11C12.433 11 14 9.433 14 7.5C14 5.567 12.433 4 10.5 4ZM7.67133 11C6.65183 10.175 6 8.91363 6 7.5C6 6.08637 6.65183 4.82498 7.67133 4H4.5C2.567 4 1 5.567 1 7.5C1 9.433 2.567 11 4.5 11H7.67133ZM0 7.5C0 5.01472 2.01472 3 4.5 3H10.5C12.9853 3 15 5.01472 15 7.5C15 9.98528 12.9853 12 10.5 12H4.5C2.01472 12 0 9.98528 0 7.5Z"
fill="currentColor"
fillRule="evenodd"
clipRule="evenodd"
></path>
</svg>
<div>Turn off VSCode</div>
</div>
)}
</div> */}
</div>
{p.ui.popup.code.show_log && (
<div className="h-[150px] overflow-auto font-mono p-2 text-xs whitespace-pre-wrap border-b">

View File

@ -174,43 +174,61 @@ export const EdSidePropComp: FC<{ meta: IMeta }> = ({ meta }) => {
dragPreviewRender={() => <></>}
/>
</DndProvider>
<div
className="m-1 border border-blue-200 px-2 self-start text-[13px] hover:bg-blue-100 cursor-pointer select-none"
onClick={() => {
if (mprops) {
const indexes: (number | undefined)[] = [];
mprops.forEach((e) => indexes.push(e.get("idx")));
let idx: any = (indexes.sort().pop() || 0) + 1;
if (indexes.length === 0) {
idx = 1;
} else {
idx = parseInt(idx) + 1;
}
<div className="flex">
<div
className="m-1 border border-blue-200 px-2 self-start text-[13px] hover:bg-blue-100 cursor-pointer select-none flex-1"
onClick={() => {
if (mprops) {
const indexes: (number | undefined)[] = [];
mprops.forEach((e) => indexes.push(e.get("idx")));
let idx: any = (indexes.sort().pop() || 0) + 1;
if (indexes.length === 0) {
idx = 1;
} else {
idx = parseInt(idx) + 1;
}
let name = `prop_${idx}`;
while (mprops.get(name)) {
idx = parseInt(idx) + 1;
name = `prop_${idx}`;
}
let name = `prop_${idx}`;
while (mprops.get(name)) {
idx = parseInt(idx) + 1;
name = `prop_${idx}`;
}
const map = new Y.Map() as FMCompDef;
syncronize(map, {
idx: idx,
name,
type: "string",
value: '"hello"',
valueBuilt: '"hello"',
meta: {
type: "text",
},
});
mprops.set(name, map);
propPopover.name = name;
const map = new Y.Map() as FMCompDef;
syncronize(map, {
idx: idx,
name,
type: "string",
value: '"hello"',
valueBuilt: '"hello"',
meta: {
type: "text",
},
});
mprops.set(name, map);
propPopover.name = name;
p.render();
}
}}
>
+ New Prop
</div>
<div
className="m-1 ml-0 border border-blue-200 px-2 self-start text-[13px] hover:bg-blue-100 cursor-pointer select-none flex items-center space-x-1"
onClick={() => {
p.ui.popup.script.mode = "js";
p.ui.popup.script.open = true;
p.ui.popup.script.type = "comp-types";
p.render();
}
}}
>
+ New Prop
}}
>
<span
dangerouslySetInnerHTML={{
__html: `<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-square-pilcrow"><rect width="18" height="18" x="3" y="3" rx="2"/><path d="M12 12H9.5a2.5 2.5 0 0 1 0-5H17"/><path d="M12 7v10"/><path d="M16 7v10"/></svg>`,
}}
></span>
<span>Typings</span>
</div>
</div>
</div>
</div>

View File

@ -3,7 +3,6 @@ import { validate } from "uuid";
import { GlobalContext, useLocal } from "web-utils";
import { DeadEnd } from "../../utils/ui/deadend";
import { Loading } from "../../utils/ui/loading";
import { evalCJS } from "../ed/logic/ed-sync";
import { Vi } from "../vi/vi";
import { base } from "./base/base";
import { scanComponent } from "./base/component";
@ -47,15 +46,11 @@ export const Root = () => {
base.route.router = router;
base.route.pages = pages;
const site_script = evalCJS(
await (
await fetch(
`${w._prasi.basepath}/_prasi/code/index.js`.replace("//", "/")
)
).text()
const site_exports = await import(
`${w._prasi.basepath}/_prasi/code/index.js`.replace("//", "/")
);
if (site_script) {
for (const [k, v] of Object.entries(site_script)) {
if (site_exports) {
for (const [k, v] of Object.entries(site_exports)) {
(window as any)[k] = v;
}
}

View File

@ -4,7 +4,6 @@ import { loadApiProxyDef } from "../../../base/load/api/api-proxy-def";
import { dbProxy } from "../../../base/load/db/db-proxy";
import { w } from "../../../utils/types/general";
import { PG } from "../../ed/logic/ed-global";
import { evalCJS } from "../../ed/logic/ed-sync";
import { treeRebuild } from "../../ed/logic/tree/build";
import { simpleHash } from "../utils/simple-hash";
@ -50,44 +49,25 @@ export const viLoadSnapshot = async (p: PG) => {
}
}
if (p.site.code.snapshot) {
for (const [name, build] of Object.entries(p.site.code.snapshot)) {
const doc = new Y.Doc();
Y.applyUpdate(doc, decompress(build.bin));
p.code[name] = { doc: doc as any };
const code = p.code[name].doc;
if (code) {
const src = code.getMap("map").get("files")?.get("index.js");
applyEnv(p, src);
treeRebuild(p);
p.render();
code.on("update", (e, origin) => {
const src = code.getMap("map").get("files")?.get("index.js");
applyEnv(p, src);
treeRebuild(p);
p.render();
});
}
}
}
await applyEnv(p);
};
export const applyEnv = (p: PG, src?: string) => {
if (src) {
const w = window as any;
export const applyEnv = async (p: PG) => {
const w = window as any;
if (p.site.config.api_url) {
w.db = dbProxy(p.site.config.api_url);
w.api = apiProxy(p.site.config.api_url);
}
if (p.site.config.api_url) {
w.db = dbProxy(p.site.config.api_url);
w.api = apiProxy(p.site.config.api_url);
}
const module = evalCJS(src);
p.global_prop = Object.keys(module);
if (typeof module === "object") {
for (const [k, v] of Object.entries(module)) {
const url = `/prod/${p.site.id}/_prasi/code/index.js`;
const fn = new Function("callback", `import("${url}").then(callback)`);
await new Promise((resolve) => {
fn((exports: any) => {
for (const [k, v] of Object.entries(exports)) {
w[k] = v;
}
}
}
resolve();
});
});
};

View File

@ -16,9 +16,6 @@ export const ViGlobal = {
api_url: "",
api: null as any,
db: null as any,
code: {
mode: "old" as "old" | "vsc",
},
},
site_url: null as unknown as URL,
script: {

BIN
bun.lockb

Binary file not shown.

View File

@ -5,6 +5,11 @@
"app/static",
"app/web/static",
],
"include": [
"app/srv/**/*",
"app/web/**/*",
"pkgs/**/*"
],
"compilerOptions": {
"lib": [
"ESNext"