forked from silverbulletmd/silverbullet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_web.ts
141 lines (128 loc) · 3.72 KB
/
build_web.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { copy } from "$std/fs/copy.ts";
import sass from "denosass";
import { bundleFolder } from "./lib/asset_bundle/builder.ts";
import { parse } from "$std/flags/mod.ts";
import { patchDenoLibJS } from "./cmd/compile.ts";
import { denoPlugins } from "esbuild_deno_loader";
import * as esbuild from "esbuild";
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);
}
}
}
export async function copyAssets(dist: string) {
await Deno.mkdir(dist, { recursive: true });
await copy("web/fonts", `${dist}`, { overwrite: true });
await copy("web/index.html", `${dist}/index.html`, {
overwrite: true,
});
await copy("web/auth.html", `${dist}/auth.html`, {
overwrite: true,
});
await copy("web/images/favicon.png", `${dist}/favicon.png`, {
overwrite: true,
});
await copy("web/images/logo.png", `${dist}/logo.png`, {
overwrite: true,
});
await copy("web/images/logo-dock.png", `${dist}/logo-dock.png`, {
overwrite: true,
});
await copy("web/manifest.json", `${dist}/manifest.json`, {
overwrite: true,
});
const compiler = sass(
Deno.readTextFileSync("web/styles/main.scss"),
{
load_paths: ["web/styles"],
},
);
await Deno.writeTextFile(
`${dist}/main.css`,
compiler.to_string("expanded") as string,
);
// HACK: Patch the JS by removing an invalid regex
let bundleJs = await Deno.readTextFile(`${dist}/client.js`);
bundleJs = patchDenoLibJS(bundleJs);
await Deno.writeTextFile(`${dist}/client.js`, bundleJs);
}
async function buildCopyBundleAssets() {
await Deno.mkdir("dist_client_bundle", { recursive: true });
await Deno.mkdir("dist_plug_bundle", { recursive: true });
await bundleFolder(
"dist_plug_bundle",
"dist/plug_asset_bundle.json",
);
console.log("Now ESBuilding the client and service workers...");
const result = await esbuild.build({
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",
// metafile: true,
jsx: "automatic",
jsxFragment: "Fragment",
jsxImportSource: "https://esm.sh/[email protected]",
plugins: denoPlugins({
importMapURL: new URL("./import_map.json", import.meta.url)
.toString(),
}),
});
if (result.metafile) {
const text = await esbuild.analyzeMetafile(result.metafile!);
console.log("Bundle info", text);
}
// 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);
await copyAssets("dist_client_bundle/.client");
await bundleFolder("dist_client_bundle", "dist/client_asset_bundle.json");
console.log("Built!");
}
if (import.meta.main) {
const args = parse(Deno.args, {
boolean: ["watch"],
alias: { w: "watch" },
default: {
watch: false,
},
});
await bundleAll(args.watch);
if (!args.watch) {
esbuild.stop();
}
}