silverbullet/build.ts

76 lines
2.1 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";
import { denoPlugin } from "./packages/esbuild_deno_loader/mod.ts";
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-07 22:27:47 +08:00
import { bundleFolder } from "./json_bundle.ts";
import { bundleRun } from "./packages/plugos/bin/plugos-bundle.ts";
2022-10-06 17:31:49 +08:00
// @ts-ignore trust me
const esbuild: typeof esbuildWasm = Deno.run === undefined
? esbuildWasm
: esbuildNative;
2022-10-07 22:27:47 +08:00
async function prepareAssets(dest: string) {
2022-10-06 17:31:49 +08:00
await copy("packages/web/fonts", dest, { overwrite: true });
await copy("packages/web/index.html", `${dest}/index.html`, {
overwrite: true,
});
const compiler = sass(
Deno.readTextFileSync("packages/web/styles/main.scss"),
{
load_paths: ["packages/web/styles"],
},
);
2022-10-07 20:13:55 +08:00
await Deno.writeTextFile(
"dist/main.css",
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: [],
// });
await bundleFolder("dist", "dist_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: {
"client": "packages/web/boot.ts",
"worker": "packages/plugos/environments/sandbox_worker.ts",
},
2022-10-06 17:31:49 +08:00
outdir: "./dist",
absWorkingDir: Deno.cwd(),
bundle: true,
treeShaking: true,
sourcemap: "linked",
watch: {
onRebuild(error, result) {
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();