fix code build

This commit is contained in:
Rizky 2024-07-27 16:22:44 +07:00
parent 63ff55cfae
commit 7469b95313
5 changed files with 266 additions and 96 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,18 @@
import { transform } from "sucrase";
export const codeBuild = (codes: Record<string, string>) => {
const result = {} as Record<string, string>;
for (const [k, v] of Object.entries(codes)) {
result[k] = transform(v, {
transforms: ["typescript", "imports", "jsx"],
preserveDynamicImport: true,
disableESTransforms: true,
}).code;
const use_strict = `"use strict";`;
if (result[k].startsWith(use_strict)) {
result[k] = result[k].substring(use_strict.length).trim();
}
}
return result;
};

View File

@ -5,6 +5,7 @@ import { IMeta, active } from "../../../ed/logic/ed-global";
import { VG } from "../global"; import { VG } from "../global";
import { ViRender } from "../render"; import { ViRender } from "../render";
import { viScriptArg } from "./arg"; import { viScriptArg } from "./arg";
import { codeBuild } from "./code-build";
import { replaceWithObject, replacement } from "./eval-script"; import { replaceWithObject, replacement } from "./eval-script";
import { extractNavigate } from "./extract-nav"; import { extractNavigate } from "./extract-nav";
@ -204,9 +205,8 @@ const updatePropValueBuilt = (mprop: FMCompDef, src: string) => {
conf.src[id] = src; conf.src[id] = src;
} }
clearTimeout(conf.timeout); clearTimeout(conf.timeout);
conf.timeout = setTimeout(async () => { conf.timeout = setTimeout(() => {
console.log(conf.src); const result = codeBuild(conf.src);
const result = await _api.code_build(conf.src);
let doc = null as unknown as Doc; let doc = null as unknown as Doc;
for (const [k, v] of Object.entries(result)) { for (const [k, v] of Object.entries(result)) {

View File

@ -2,6 +2,7 @@ import { createId } from "@paralleldrive/cuid2";
import { IItem, MItem } from "../../../../utils/types/item"; import { IItem, MItem } from "../../../../utils/types/item";
import { FNCompDef } from "../../../../utils/types/meta-fn"; import { FNCompDef } from "../../../../utils/types/meta-fn";
import { IMeta } from "../../utils/types"; import { IMeta } from "../../utils/types";
import { codeBuild } from "./code-build";
const w = window as unknown as { const w = window as unknown as {
prasiEditDevItem: Record<string, Record<string, SingleChange[]>>; prasiEditDevItem: Record<string, Record<string, SingleChange[]>>;
@ -217,9 +218,10 @@ export const devItem = (
for (const [k, v] of Object.entries(compile)) { for (const [k, v] of Object.entries(compile)) {
src[k] = v.value; src[k] = v.value;
} }
let code_result = codeBuild(src);
console.log(src); await new Promise((done) => {
const code_result = await _api.code_build(src); setTimeout(done);
});
if (props) { if (props) {
for (const [k, v] of Object.entries(code_result) as any) { for (const [k, v] of Object.entries(code_result) as any) {

View File

@ -15,6 +15,7 @@ const cache = {
string, string,
{ type: string; content: any; compression: "" | "br" } { type: string; content: any; compression: "" | "br" }
>, >,
dev: {} as Record<string, ReturnType<typeof Bun.file>>,
}; };
export const serveStatic = { export const serveStatic = {
@ -23,27 +24,16 @@ export const serveStatic = {
for (const k of Object.keys(cache.static)) { for (const k of Object.keys(cache.static)) {
delete cache.static[k]; delete cache.static[k];
} }
}
await Promise.all([this.load("app/static"), this.load("app/web/public")]);
if (g.mode === "dev") {
["app/static", "app/web/public"].forEach((base_path) => { ["app/static", "app/web/public"].forEach((base_path) => {
watch(dir.path(`app/static`), async (_, filename) => { watch(dir.path(`app/static`), async (event, filename) => {
if (filename) { if (filename) {
try { cache.dev = {};
const file = Bun.file(dir.path(`${base_path}/${filename}`));
if (await file.exists()) {
cache.static[`/${filename}`] = {
type: mime.getType(filename) || "application/octet-stream",
compression: "",
content: await file.arrayBuffer(),
};
}
} catch (e: any) {
cache.static = {};
}
} }
}); });
}); });
} else {
await Promise.all([this.load("app/static"), this.load("app/web/public")]);
} }
}, },
async load(base_path: string) { async load(base_path: string) {
@ -81,20 +71,23 @@ export const serveStatic = {
return !!cache.static[url.pathname]; return !!cache.static[url.pathname];
}, },
async serve(url: URL) { async serve(url: URL) {
let file = cache.static[url.pathname]; if (g.mode === "prod") {
if (file) { let file = cache.static[url.pathname];
return new Response(file.content, { if (file) {
headers: { return new Response(file.content, {
...CORS_HEADERS, headers: {
...{ "content-type": file.type }, ...CORS_HEADERS,
...(file.compression ? { "content-encoding": file.compression } : {}), ...{ "content-type": file.type },
}, ...(file.compression
}); ? { "content-encoding": file.compression }
} : {}),
},
});
}
if (url.pathname.endsWith(".js")) { if (url.pathname.endsWith(".js")) {
return new Response( return new Response(
` `
console.warn("${url.pathname} not found, force reloading for clearing cache.") console.warn("${url.pathname} not found, force reloading for clearing cache.")
navigator.serviceWorker.getRegistration().then(function(reg) { navigator.serviceWorker.getRegistration().then(function(reg) {
setTimeout(() => { setTimeout(() => {
@ -106,21 +99,44 @@ navigator.serviceWorker.getRegistration().then(function(reg) {
}, 2000); }, 2000);
}); });
`, `,
{ {
headers: { "content-type": "text/javascript" }, headers: { "content-type": "text/javascript" },
} }
); );
} }
file = cache.static["/index.html"]; file = cache.static["/index.html"];
if (file) { if (file) {
return new Response(file.content, { return new Response(file.content, {
headers: { headers: {
...{ "content-type": file.type }, ...{ "content-type": file.type },
...(file.compression ? { "content-encoding": file.compression } : {}), ...(file.compression
}, ? { "content-encoding": file.compression }
}); : {}),
},
});
}
} else {
if (cache.dev[url.pathname]) {
return new Response(cache.dev[url.pathname]);
}
let file = Bun.file(dir.path(`/app/static${url.pathname}`));
if (await file.exists()) {
cache.dev[url.pathname] = file;
return new Response(file);
}
file = Bun.file(dir.path(`/app/web/public${url.pathname}`));
if (await file.exists()) {
cache.dev[url.pathname] = file;
return new Response(file);
}
file = Bun.file(dir.path(`/app/static/index.html`));
if (await file.exists()) {
cache.dev[`/index.html`] = file;
return new Response(file);
}
} }
return new Response(`Not Found: ${url.pathname}`, { status: 404 }); return new Response(`Not Found: ${url.pathname}`, { status: 404 });
}, },