2024-07-30 23:24:17 +08:00
|
|
|
import { copy } from "@std/fs";
|
2024-11-08 04:24:00 +08:00
|
|
|
import { fileURLToPath } from "node:url";
|
2022-10-17 21:48:21 +08:00
|
|
|
|
2024-03-16 22:29:24 +08:00
|
|
|
import sass from "denosass";
|
2024-02-09 04:00:45 +08:00
|
|
|
import { bundleFolder } from "./lib/asset_bundle/builder.ts";
|
2022-10-17 21:48:21 +08:00
|
|
|
|
2024-07-30 23:24:17 +08:00
|
|
|
import { parseArgs } from "@std/cli/parse-args";
|
2024-03-16 22:29:24 +08:00
|
|
|
import { patchDenoLibJS } from "./cmd/compile.ts";
|
2024-07-30 20:22:44 +08:00
|
|
|
import { denoPlugins } from "@luca/esbuild-deno-loader";
|
2024-03-16 22:29:24 +08:00
|
|
|
import * as esbuild from "esbuild";
|
2022-10-17 21:48:21 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
export async function bundleAll(
|
|
|
|
watch: boolean,
|
|
|
|
): Promise<void> {
|
|
|
|
let building = false;
|
|
|
|
await buildCopyBundleAssets();
|
|
|
|
let timer;
|
|
|
|
if (watch) {
|
|
|
|
const watcher = Deno.watchFs(["web", "dist_plug_bundle"]);
|
|
|
|
for await (const _event of watcher) {
|
|
|
|
if (timer) {
|
|
|
|
clearTimeout(timer);
|
|
|
|
}
|
|
|
|
timer = setTimeout(() => {
|
|
|
|
console.log("Change detected, rebuilding...");
|
|
|
|
if (building) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
building = true;
|
|
|
|
buildCopyBundleAssets().finally(() => {
|
|
|
|
building = false;
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-17 21:48:21 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
export async function copyAssets(dist: string) {
|
|
|
|
await Deno.mkdir(dist, { recursive: true });
|
2023-01-08 19:24:12 +08:00
|
|
|
await copy("web/fonts", `${dist}`, { overwrite: true });
|
|
|
|
await copy("web/index.html", `${dist}/index.html`, {
|
2022-10-17 21:48:21 +08:00
|
|
|
overwrite: true,
|
|
|
|
});
|
2023-01-08 19:24:12 +08:00
|
|
|
await copy("web/auth.html", `${dist}/auth.html`, {
|
2022-12-22 18:21:12 +08:00
|
|
|
overwrite: true,
|
|
|
|
});
|
2023-01-08 19:24:12 +08:00
|
|
|
await copy("web/images/favicon.png", `${dist}/favicon.png`, {
|
2022-10-17 21:48:21 +08:00
|
|
|
overwrite: true,
|
|
|
|
});
|
2023-01-08 19:24:12 +08:00
|
|
|
await copy("web/images/logo.png", `${dist}/logo.png`, {
|
2022-10-17 21:48:21 +08:00
|
|
|
overwrite: true,
|
|
|
|
});
|
2023-05-24 02:53:53 +08:00
|
|
|
await copy("web/images/logo-dock.png", `${dist}/logo-dock.png`, {
|
|
|
|
overwrite: true,
|
|
|
|
});
|
2023-01-08 19:24:12 +08:00
|
|
|
await copy("web/manifest.json", `${dist}/manifest.json`, {
|
2022-10-17 21:48:21 +08:00
|
|
|
overwrite: true,
|
|
|
|
});
|
|
|
|
const compiler = sass(
|
|
|
|
Deno.readTextFileSync("web/styles/main.scss"),
|
|
|
|
{
|
|
|
|
load_paths: ["web/styles"],
|
|
|
|
},
|
|
|
|
);
|
|
|
|
await Deno.writeTextFile(
|
2023-01-08 19:24:12 +08:00
|
|
|
`${dist}/main.css`,
|
2022-10-17 21:48:21 +08:00
|
|
|
compiler.to_string("expanded") as string,
|
|
|
|
);
|
|
|
|
|
|
|
|
// HACK: Patch the JS by removing an invalid regex
|
2023-01-08 19:24:12 +08:00
|
|
|
let bundleJs = await Deno.readTextFile(`${dist}/client.js`);
|
2022-10-17 21:48:21 +08:00
|
|
|
bundleJs = patchDenoLibJS(bundleJs);
|
2023-01-08 19:24:12 +08:00
|
|
|
await Deno.writeTextFile(`${dist}/client.js`, bundleJs);
|
2022-10-17 21:48:21 +08:00
|
|
|
}
|
2023-05-24 02:53:53 +08:00
|
|
|
async function buildCopyBundleAssets() {
|
|
|
|
await Deno.mkdir("dist_client_bundle", { recursive: true });
|
|
|
|
await Deno.mkdir("dist_plug_bundle", { recursive: true });
|
2022-10-17 21:48:21 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
await bundleFolder(
|
|
|
|
"dist_plug_bundle",
|
|
|
|
"dist/plug_asset_bundle.json",
|
|
|
|
);
|
2023-01-08 19:24:12 +08:00
|
|
|
|
2023-07-02 17:25:32 +08:00
|
|
|
console.log("Now ESBuilding the client and service workers...");
|
|
|
|
|
2024-01-27 00:05:10 +08:00
|
|
|
const result = await esbuild.build({
|
2023-07-02 17:25:32 +08:00
|
|
|
entryPoints: [
|
|
|
|
{
|
|
|
|
in: "web/boot.ts",
|
|
|
|
out: ".client/client",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: "web/service_worker.ts",
|
|
|
|
out: "service_worker",
|
|
|
|
},
|
|
|
|
],
|
|
|
|
outdir: "dist_client_bundle",
|
|
|
|
absWorkingDir: Deno.cwd(),
|
|
|
|
bundle: true,
|
|
|
|
treeShaking: true,
|
|
|
|
sourcemap: "linked",
|
|
|
|
minify: true,
|
|
|
|
jsxFactory: "h",
|
2024-01-27 00:05:10 +08:00
|
|
|
// metafile: true,
|
2023-07-02 17:25:32 +08:00
|
|
|
jsx: "automatic",
|
|
|
|
jsxFragment: "Fragment",
|
2024-07-26 02:58:03 +08:00
|
|
|
jsxImportSource: "https://esm.sh/preact@10.23.1",
|
2024-01-25 23:29:25 +08:00
|
|
|
plugins: denoPlugins({
|
2024-11-08 04:24:00 +08:00
|
|
|
configPath: fileURLToPath(new URL("./deno.json", import.meta.url)),
|
2024-01-25 23:29:25 +08:00
|
|
|
}),
|
2023-07-02 17:25:32 +08:00
|
|
|
});
|
2023-01-08 19:24:12 +08:00
|
|
|
|
2024-01-27 00:05:10 +08:00
|
|
|
if (result.metafile) {
|
|
|
|
const text = await esbuild.analyzeMetafile(result.metafile!);
|
|
|
|
console.log("Bundle info", text);
|
|
|
|
}
|
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
// Patch the service_worker {{CACHE_NAME}}
|
|
|
|
let swCode = await Deno.readTextFile("dist_client_bundle/service_worker.js");
|
|
|
|
swCode = swCode.replaceAll("{{CACHE_NAME}}", `cache-${Date.now()}`);
|
|
|
|
await Deno.writeTextFile("dist_client_bundle/service_worker.js", swCode);
|
2023-01-08 19:24:12 +08:00
|
|
|
|
2023-05-24 02:53:53 +08:00
|
|
|
await copyAssets("dist_client_bundle/.client");
|
|
|
|
await bundleFolder("dist_client_bundle", "dist/client_asset_bundle.json");
|
|
|
|
|
|
|
|
console.log("Built!");
|
2022-10-17 21:48:21 +08:00
|
|
|
}
|
|
|
|
|
2023-01-08 19:24:12 +08:00
|
|
|
if (import.meta.main) {
|
2024-07-30 23:24:17 +08:00
|
|
|
const args = parseArgs(Deno.args, {
|
2023-01-08 19:24:12 +08:00
|
|
|
boolean: ["watch"],
|
|
|
|
alias: { w: "watch" },
|
|
|
|
default: {
|
|
|
|
watch: false,
|
|
|
|
},
|
|
|
|
});
|
2023-05-24 02:53:53 +08:00
|
|
|
await bundleAll(args.watch);
|
2023-01-08 19:24:12 +08:00
|
|
|
if (!args.watch) {
|
|
|
|
esbuild.stop();
|
|
|
|
}
|
2022-10-17 21:48:21 +08:00
|
|
|
}
|