This commit is contained in:
Rizky 2024-02-09 09:37:03 +07:00
parent 63d1bd3a4d
commit 25c860decd
2 changed files with 85 additions and 33 deletions

View File

@ -7,22 +7,71 @@ import { readDirectoryRecursively } from "../../../../api/site-export";
import { docs } from "../../entity/docs"; import { docs } from "../../entity/docs";
import { CodeMode, code } from "./util-code"; import { CodeMode, code } from "./util-code";
export const codeBuild = async (id_site: any, mode: CodeMode) => { export const codeBuild = async (id_site: any) => {
const src_path = code.path(id_site, mode, "src"); const src_path = code.path(id_site, "site", "src");
if (!(await existsAsync(src_path))) return; if (!(await existsAsync(src_path))) return;
const build_path = code.path(id_site, mode, "build");
await removeAsync(build_path);
await dirAsync(build_path);
const build_file = `${build_path}/index.js`;
await writeAsync(build_file, "");
if (!code.esbuild[id_site]) { if (!code.esbuild[id_site]) {
code.esbuild[id_site] = { site: null, ssr: null }; code.esbuild[id_site] = { site: null, server: null };
} }
if (!code.esbuild[id_site][mode]) { if (!code.esbuild[id_site].server) {
code.esbuild[id_site][mode] = await context({ const server_main = code.path(id_site, "site", "src", "server.ts");
if (!(await existsAsync(server_main))) {
await writeAsync(server_main, "");
const bun_types = Bun.spawn({
cmd: ["npm", "i", "-D", "@types/bun"],
cwd: code.path(id_site, "site", "src"),
});
await bun_types.exited;
}
const build_path = code.path(id_site, "server", "build");
await removeAsync(build_path);
await dirAsync(build_path);
const build_file = `${build_path}/index.js`;
await writeAsync(build_file, "");
code.esbuild[id_site].server = await context({
absWorkingDir: src_path,
entryPoints: ["server.ts"],
bundle: true,
outfile: build_file,
minify: true,
treeShaking: true,
format: "cjs",
logLevel: "silent",
sourcemap: true,
plugins: [
style(),
globalExternals({
react: {
varName: "window.React",
type: "cjs",
},
"react-dom": {
varName: "window.ReactDOM",
type: "cjs",
},
}),
],
});
const esbuild = code.esbuild[id_site].server;
esbuild?.watch();
}
if (!code.esbuild[id_site].site) {
const build_path = code.path(id_site, "site", "build");
await removeAsync(build_path);
await dirAsync(build_path);
const build_file = `${build_path}/index.js`;
await writeAsync(build_file, "");
const index_path = code.path(id_site, "site", "src", "index.tsx");
if (!(await existsAsync(index_path))) {
await writeAsync(index_path, 'export const hello = "world"');
}
code.esbuild[id_site].site = await context({
absWorkingDir: src_path, absWorkingDir: src_path,
entryPoints: ["index.tsx"], entryPoints: ["index.tsx"],
bundle: true, bundle: true,
@ -50,8 +99,8 @@ export const codeBuild = async (id_site: any, mode: CodeMode) => {
setup.onEnd((res) => { setup.onEnd((res) => {
const cdoc = docs.code[id_site]; const cdoc = docs.code[id_site];
if (cdoc) { if (cdoc) {
const doc = cdoc.build[mode]; const doc = cdoc.build["site"];
const build_dir = code.path(id_site, mode, "build"); const build_dir = code.path(id_site, "site", "build");
if (doc) { if (doc) {
codeApplyChanges(build_dir, doc); codeApplyChanges(build_dir, doc);
} }
@ -61,24 +110,27 @@ export const codeBuild = async (id_site: any, mode: CodeMode) => {
}, },
], ],
}); });
const esbuild = code.esbuild[id_site][mode]; const esbuild = code.esbuild[id_site].site;
esbuild?.watch(); esbuild?.watch();
} }
const esbuild = code.esbuild[id_site][mode]; for (const mode of ["site", "server"]) {
if (esbuild) { const esbuild = code.esbuild[id_site][mode as "site" | "server"];
try { if (esbuild) {
await esbuild.rebuild(); try {
} catch (e) { await esbuild.rebuild();
console.error(e); } catch (e) {
console.error(e);
}
} }
}
const out = Bun.file(build_file); const build_file = code.path(id_site, "site", "build", "index.js");
const src = (await out.text()).replace( const out = Bun.file(build_file);
"//# sourceMappingURL=index.js.map", const src = (await out.text()).replace(
`//# sourceMappingURL=/nova-load/code/${id_site}/${mode}/index.js.map` "//# sourceMappingURL=index.js.map",
); `//# sourceMappingURL=/nova-load/code/${id_site}/${mode}/index.js.map`
await Bun.write(out, src); );
await Bun.write(out, src);
}
}; };
const codeApplyChanges = (path: string, doc: DCode) => { const codeApplyChanges = (path: string, doc: DCode) => {

View File

@ -2,9 +2,8 @@ import { dir } from "dir";
import { BuildContext } from "esbuild"; import { BuildContext } from "esbuild";
import { dirAsync, exists, existsAsync, writeAsync } from "fs-jetpack"; import { dirAsync, exists, existsAsync, writeAsync } from "fs-jetpack";
import { dirname } from "path"; import { dirname } from "path";
import { g } from "utils/global";
export type CodeMode = "site" | "ssr"; export type CodeMode = "site" | "server";
export const code = { export const code = {
path(id_site: string, mode: CodeMode, type: "src" | "build", path?: string) { path(id_site: string, mode: CodeMode, type: "src" | "build", path?: string) {
let file_path = ""; let file_path = "";
@ -13,6 +12,9 @@ export const code = {
} }
return dir.data(`/code/${id_site}/${mode}/${type}${file_path}`); return dir.data(`/code/${id_site}/${mode}/${type}${file_path}`);
}, },
package_deps: (path: string) => {
const file = Bun.file(path);
},
esbuild: {} as Record<string, Record<CodeMode, null | BuildContext>>, esbuild: {} as Record<string, Record<CodeMode, null | BuildContext>>,
prep(id_site: string, mode: CodeMode) { prep(id_site: string, mode: CodeMode) {
if (exists(dir.data(""))) { if (exists(dir.data(""))) {
@ -25,9 +27,7 @@ export const code = {
return { return {
path(type: "src" | "build", path: string) { path(type: "src" | "build", path: string) {
return dir.data( return dir.data(
`/code/${id_site}/${mode}/${type}${ `/code/${id_site}/site/${type}${path[0] === "/" ? path : `/${path}`}`
path[0] === "/" ? path : `/${path}`
}`
); );
}, },
new_file(path: string, content: string) { new_file(path: string, content: string) {