forked from silverbulletmd/silverbullet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_client.ts
More file actions
106 lines (91 loc) · 2.94 KB
/
build_client.ts
File metadata and controls
106 lines (91 loc) · 2.94 KB
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
import { copy } from "@std/fs";
import { fileURLToPath } from "node:url";
import sass from "denosass";
import { patchDenoLibJS } from "./client/plugos/plug_compile.ts";
import { denoPlugins } from "@luca/esbuild-deno-loader";
import * as esbuild from "esbuild";
// This builds the client and puts it into client_bundle/client
export async function bundleAll(): Promise<void> {
await buildCopyBundleAssets();
}
export async function copyAssets(dist: string) {
await Deno.mkdir(dist, { recursive: true });
await copy("client/fonts", `${dist}`, { overwrite: true });
await copy("client/html/index.html", `${dist}/index.html`, {
overwrite: true,
});
await copy("client/html/auth.html", `${dist}/auth.html`, {
overwrite: true,
});
await copy("client/images/favicon.png", `${dist}/favicon.png`, {
overwrite: true,
});
await copy("client/images/logo.png", `${dist}/logo.png`, {
overwrite: true,
});
await copy("client/images/logo-dock.png", `${dist}/logo-dock.png`, {
overwrite: true,
});
const compiler = sass(
Deno.readTextFileSync("client/styles/main.scss"),
{
load_paths: ["client/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("client_bundle/client", { recursive: true });
await Deno.mkdir("client_bundle/base_fs", { recursive: true });
console.log("Now ESBuilding the client and service workers...");
const result = await esbuild.build({
entryPoints: [
{
in: "client/boot.ts",
out: ".client/client",
},
{
in: "client/service_worker.ts",
out: "service_worker",
},
],
outdir: "client_bundle/client",
absWorkingDir: Deno.cwd(),
bundle: true,
treeShaking: true,
sourcemap: Deno.args[0] === "--production" ? undefined : "linked",
minify: true,
jsxFactory: "h",
// metafile: true,
jsx: "automatic",
jsxFragment: "Fragment",
jsxImportSource: "npm:preact@10.23.1",
plugins: denoPlugins({
configPath: fileURLToPath(new URL("./deno.json", import.meta.url)),
nodeModulesDir: "auto",
}),
});
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(
"client_bundle/client/service_worker.js",
);
swCode = swCode.replaceAll("{{CACHE_NAME}}", `cache-${Date.now()}`);
await Deno.writeTextFile("client_bundle/client/service_worker.js", swCode);
await copyAssets("client_bundle/client/.client");
console.log("Built!");
}
if (import.meta.main) {
await bundleAll();
esbuild.stop();
}