fix code build
This commit is contained in:
parent
63ff55cfae
commit
7469b95313
File diff suppressed because one or more lines are too long
|
|
@ -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;
|
||||
};
|
||||
|
|
@ -5,6 +5,7 @@ import { IMeta, active } from "../../../ed/logic/ed-global";
|
|||
import { VG } from "../global";
|
||||
import { ViRender } from "../render";
|
||||
import { viScriptArg } from "./arg";
|
||||
import { codeBuild } from "./code-build";
|
||||
import { replaceWithObject, replacement } from "./eval-script";
|
||||
import { extractNavigate } from "./extract-nav";
|
||||
|
||||
|
|
@ -204,9 +205,8 @@ const updatePropValueBuilt = (mprop: FMCompDef, src: string) => {
|
|||
conf.src[id] = src;
|
||||
}
|
||||
clearTimeout(conf.timeout);
|
||||
conf.timeout = setTimeout(async () => {
|
||||
console.log(conf.src);
|
||||
const result = await _api.code_build(conf.src);
|
||||
conf.timeout = setTimeout(() => {
|
||||
const result = codeBuild(conf.src);
|
||||
|
||||
let doc = null as unknown as Doc;
|
||||
for (const [k, v] of Object.entries(result)) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ import { createId } from "@paralleldrive/cuid2";
|
|||
import { IItem, MItem } from "../../../../utils/types/item";
|
||||
import { FNCompDef } from "../../../../utils/types/meta-fn";
|
||||
import { IMeta } from "../../utils/types";
|
||||
import { codeBuild } from "./code-build";
|
||||
|
||||
const w = window as unknown as {
|
||||
prasiEditDevItem: Record<string, Record<string, SingleChange[]>>;
|
||||
|
|
@ -217,9 +218,10 @@ export const devItem = (
|
|||
for (const [k, v] of Object.entries(compile)) {
|
||||
src[k] = v.value;
|
||||
}
|
||||
|
||||
console.log(src);
|
||||
const code_result = await _api.code_build(src);
|
||||
let code_result = codeBuild(src);
|
||||
await new Promise((done) => {
|
||||
setTimeout(done);
|
||||
});
|
||||
|
||||
if (props) {
|
||||
for (const [k, v] of Object.entries(code_result) as any) {
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ const cache = {
|
|||
string,
|
||||
{ type: string; content: any; compression: "" | "br" }
|
||||
>,
|
||||
dev: {} as Record<string, ReturnType<typeof Bun.file>>,
|
||||
};
|
||||
|
||||
export const serveStatic = {
|
||||
|
|
@ -23,27 +24,16 @@ export const serveStatic = {
|
|||
for (const k of Object.keys(cache.static)) {
|
||||
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) => {
|
||||
watch(dir.path(`app/static`), async (_, filename) => {
|
||||
watch(dir.path(`app/static`), async (event, filename) => {
|
||||
if (filename) {
|
||||
try {
|
||||
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 = {};
|
||||
}
|
||||
cache.dev = {};
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
await Promise.all([this.load("app/static"), this.load("app/web/public")]);
|
||||
}
|
||||
},
|
||||
async load(base_path: string) {
|
||||
|
|
@ -81,13 +71,16 @@ export const serveStatic = {
|
|||
return !!cache.static[url.pathname];
|
||||
},
|
||||
async serve(url: URL) {
|
||||
if (g.mode === "prod") {
|
||||
let file = cache.static[url.pathname];
|
||||
if (file) {
|
||||
return new Response(file.content, {
|
||||
headers: {
|
||||
...CORS_HEADERS,
|
||||
...{ "content-type": file.type },
|
||||
...(file.compression ? { "content-encoding": file.compression } : {}),
|
||||
...(file.compression
|
||||
? { "content-encoding": file.compression }
|
||||
: {}),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
@ -118,10 +111,33 @@ navigator.serviceWorker.getRegistration().then(function(reg) {
|
|||
return new Response(file.content, {
|
||||
headers: {
|
||||
...{ "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 });
|
||||
},
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in New Issue