silverbullet/build.ts

80 lines
2.2 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-07 22:27:47 +08:00
import { bundleFolder } from "./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-07 22:27:47 +08:00
async function prepareAssets(dest: string) {
2022-10-08 22:29:43 +08:00
await copy("web/fonts", dest, { overwrite: true });
await copy("web/index.html", `${dest}/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(
"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: [],
// });
2022-10-08 21:46:56 +08:00
let bundleJs = await Deno.readTextFile("dist/client.js");
bundleJs = patchDenoLibJS(bundleJs);
await Deno.writeTextFile("dist/client.js", bundleJs);
2022-10-07 22:27:47 +08:00
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: {
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-06 17:31:49 +08:00
outdir: "./dist",
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();