silverbullet/build.ts

81 lines
2.3 KiB
TypeScript
Raw Normal View History

2022-10-06 17:31:49 +08: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";
2022-10-08 22:29:43 +08:00
import { denoPlugin } from "./esbuild_deno_loader/mod.ts";
2022-10-06 17:31:49 +08:00
import { copy } from "https://deno.land/std@0.158.0/fs/copy.ts";
2022-10-07 20:13:55 +08:00
import sass from "https://deno.land/x/denosass@1.0.4/mod.ts";
2022-10-08 22:47:55 +08:00
import { bundleFolder } from "./common/json_bundle.ts";
2022-10-08 22:29:43 +08:00
import { patchDenoLibJS } from "./common/hack.ts";
2022-10-06 17:31:49 +08:00
// @ts-ignore trust me
const esbuild: typeof esbuildWasm = Deno.run === undefined
? esbuildWasm
: esbuildNative;
2022-10-08 22:47:55 +08:00
async function prepareAssets(dist: string) {
await copy("web/fonts", `${dist}/web`, { overwrite: true });
await copy("web/index.html", `${dist}/web/index.html`, {
2022-10-06 17:31:49 +08:00
overwrite: true,
});
const compiler = sass(
2022-10-08 22:29:43 +08:00
Deno.readTextFileSync("web/styles/main.scss"),
2022-10-06 17:31:49 +08:00
{
2022-10-08 22:29:43 +08:00
load_paths: ["web/styles"],
2022-10-06 17:31:49 +08:00
},
);
2022-10-07 20:13:55 +08:00
await Deno.writeTextFile(
2022-10-08 22:47:55 +08:00
`${dist}/web/main.css`,
2022-10-07 20:13:55 +08:00
compiler.to_string("expanded") as string,
);
2022-10-07 22:27:47 +08:00
// await bundleRun({
// _: [`${__dirname}../plugs/global.plug.yaml`],
// debug: true,
// dist: tmpDist,
// exclude: [],
// });
2022-10-08 22:47:55 +08:00
// HACK: Patch the JS by removing an invalid regex
let bundleJs = await Deno.readTextFile(`${dist}/web/client.js`);
2022-10-08 21:46:56 +08:00
bundleJs = patchDenoLibJS(bundleJs);
2022-10-08 22:47:55 +08:00
await Deno.writeTextFile(`${dist}/web/client.js`, bundleJs);
2022-10-08 21:46:56 +08:00
2022-10-08 22:47:55 +08:00
await bundleFolder(`dist/web`, "dist/web_bundle.json");
2022-10-06 17:31:49 +08:00
}
2022-10-06 21:14:21 +08:00
async function bundle(): Promise<void> {
2022-10-06 17:31:49 +08:00
await Promise.all([
esbuild.build({
2022-10-06 21:14:21 +08:00
entryPoints: {
2022-10-08 22:29:43 +08:00
"client": "web/boot.ts",
"worker": "plugos/environments/sandbox_worker.ts",
2022-10-06 21:14:21 +08:00
},
2022-10-08 22:47:55 +08:00
outdir: "./dist/web",
2022-10-06 17:31:49 +08:00
absWorkingDir: Deno.cwd(),
bundle: true,
treeShaking: true,
sourcemap: "linked",
watch: {
2022-10-08 22:29:43 +08:00
onRebuild(error) {
2022-10-06 17:31:49 +08:00
if (error) {
console.error("watch build failed:", error);
} else {
console.log("watch build succeeded.");
}
2022-10-07 22:27:47 +08:00
prepareAssets("dist").catch(console.error);
2022-10-06 17:31:49 +08:00
},
},
plugins: [
denoPlugin({
importMapURL: new URL("./import_map.json", import.meta.url),
}),
],
}),
]);
2022-10-07 22:27:47 +08:00
await prepareAssets("dist");
2022-10-06 17:31:49 +08:00
console.log("Built!");
}
2022-10-06 21:14:21 +08:00
await bundle();
2022-10-06 17:31:49 +08:00
// esbuild.stop();