2022-10-17 13:48:21 +00:00
|
|
|
// -- esbuild --
|
|
|
|
// @deno-types="https://deno.land/x/esbuild@v0.14.54/mod.d.ts"
|
|
|
|
import * as esbuildWasm from "https://deno.land/x/esbuild@v0.14.54/wasm.js";
|
|
|
|
import * as esbuildNative from "https://deno.land/x/esbuild@v0.14.54/mod.js";
|
|
|
|
import { denoPlugin } from "https://deno.land/x/esbuild_deno_loader@0.6.0/mod.ts"; //"./esbuild_deno_loader/mod.ts";
|
2022-11-19 15:05:37 +00:00
|
|
|
import { copy } from "https://deno.land/std@0.165.0/fs/copy.ts";
|
2022-10-17 13:48:21 +00:00
|
|
|
|
|
|
|
import sass from "https://deno.land/x/denosass@1.0.4/mod.ts";
|
|
|
|
import { bundleFolder } from "./plugos/asset_bundle/builder.ts";
|
|
|
|
import { patchDenoLibJS } from "./plugos/hack.ts";
|
|
|
|
import { bundle as plugOsBundle } from "./plugos/bin/plugos-bundle.ts";
|
|
|
|
|
2022-11-19 15:05:37 +00:00
|
|
|
import * as flags from "https://deno.land/std@0.165.0/flags/mod.ts";
|
2022-10-17 13:48:21 +00:00
|
|
|
|
|
|
|
// @ts-ignore trust me
|
|
|
|
const esbuild: typeof esbuildWasm = Deno.run === undefined
|
|
|
|
? esbuildWasm
|
|
|
|
: esbuildNative;
|
|
|
|
|
|
|
|
async function prepareAssets(dist: string) {
|
|
|
|
await copy("web/fonts", `${dist}/web`, { overwrite: true });
|
|
|
|
await copy("web/index.html", `${dist}/web/index.html`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
2022-12-22 10:21:12 +00:00
|
|
|
await copy("web/auth.html", `${dist}/web/auth.html`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
2022-12-09 08:14:40 +00:00
|
|
|
await copy("web/images/favicon.png", `${dist}/web/favicon.png`, {
|
2022-10-17 13:48:21 +00:00
|
|
|
overwrite: true,
|
|
|
|
});
|
|
|
|
await copy("web/images/logo.png", `${dist}/web/logo.png`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
|
|
|
await copy("web/manifest.json", `${dist}/web/manifest.json`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
|
|
|
await copy("server/SETTINGS_template.md", `${dist}/SETTINGS_template.md`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
|
|
|
const compiler = sass(
|
|
|
|
Deno.readTextFileSync("web/styles/main.scss"),
|
|
|
|
{
|
|
|
|
load_paths: ["web/styles"],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await Deno.writeTextFile(
|
|
|
|
`${dist}/web/main.css`,
|
|
|
|
compiler.to_string("expanded") as string,
|
|
|
|
);
|
2023-01-01 19:28:25 +00:00
|
|
|
const globalManifest = await plugOsBundle("./plugs/global.plug.yaml");
|
2022-10-17 13:48:21 +00:00
|
|
|
await Deno.writeTextFile(
|
|
|
|
`${dist}/web/global.plug.json`,
|
|
|
|
JSON.stringify(globalManifest, null, 2),
|
|
|
|
);
|
|
|
|
|
|
|
|
// HACK: Patch the JS by removing an invalid regex
|
|
|
|
let bundleJs = await Deno.readTextFile(`${dist}/web/client.js`);
|
|
|
|
bundleJs = patchDenoLibJS(bundleJs);
|
|
|
|
await Deno.writeTextFile(`${dist}/web/client.js`, bundleJs);
|
|
|
|
|
|
|
|
await bundleFolder(dist, "dist/asset_bundle.json");
|
|
|
|
}
|
|
|
|
|
|
|
|
async function bundle(watch: boolean): Promise<void> {
|
|
|
|
let building = false;
|
|
|
|
await doBuild();
|
|
|
|
let timer;
|
|
|
|
if (watch) {
|
|
|
|
const watcher = Deno.watchFs(["web", "dist_bundle/_plug"]);
|
|
|
|
for await (const _event of watcher) {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
console.log("Change detected, rebuilding...");
|
|
|
|
doBuild();
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doBuild() {
|
|
|
|
if (building) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
building = true;
|
|
|
|
await Promise.all([
|
|
|
|
esbuild.build({
|
|
|
|
entryPoints: {
|
|
|
|
client: "web/boot.ts",
|
|
|
|
service_worker: "web/service_worker.ts",
|
2022-10-20 10:23:12 +00:00
|
|
|
worker: "plugos/environments/sandbox_worker.ts",
|
2022-10-17 13:48:21 +00:00
|
|
|
},
|
|
|
|
outdir: "./dist_bundle/web",
|
|
|
|
absWorkingDir: Deno.cwd(),
|
|
|
|
bundle: true,
|
|
|
|
treeShaking: true,
|
|
|
|
sourcemap: "linked",
|
|
|
|
minify: true,
|
|
|
|
jsxFactory: "h",
|
|
|
|
jsx: "automatic",
|
|
|
|
jsxFragment: "Fragment",
|
|
|
|
jsxImportSource: "https://esm.sh/preact@10.11.1",
|
|
|
|
plugins: [
|
|
|
|
denoPlugin({
|
|
|
|
importMapURL: new URL("./import_map.json", import.meta.url),
|
|
|
|
}),
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
await prepareAssets("dist_bundle");
|
|
|
|
building = false;
|
|
|
|
console.log("Built!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = flags.parse(Deno.args, {
|
|
|
|
boolean: ["watch"],
|
|
|
|
alias: { w: "watch" },
|
|
|
|
default: {
|
|
|
|
watch: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await bundle(args.watch);
|
|
|
|
if (!args.watch) {
|
|
|
|
esbuild.stop();
|
|
|
|
}
|