-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathesbuild.runtime.ts
55 lines (44 loc) · 1.58 KB
/
esbuild.runtime.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
const esbuild = require("esbuild");
const { copy, move } = require("fs-extra");
const { rmSync, readFileSync, writeFileSync, createReadStream } = require("fs");
const path = require("path");
const { createHash } = require("crypto");
const OUTDIR = "dist/runtime";
rmSync(OUTDIR, { recursive: true, force: true });
(async () => {
await esbuild.build({
entryPoints: ["src/runtime.tsx"],
outdir: OUTDIR,
keepNames: true,
minify: false,
format: "esm",
});
// copy assets
await copy("bundled/assets", `${OUTDIR}/assets`);
await copy("src/runtime.html", `${OUTDIR}/runtime.html`);
// make file hash for cache busting
const fileHash = (
(await generateFileHash(`${OUTDIR}/runtime.js`)) as string
).substring(0, 8);
const newFileName = `runtime.${fileHash}.js`;
await move(`${OUTDIR}/runtime.js`, `${OUTDIR}/${newFileName}`);
const htmlPath = path.join(__dirname, "dist", "runtime", "runtime.html");
const content = readFileSync(htmlPath, "utf8").replace(/runtime.js/g, newFileName);
writeFileSync(htmlPath, content, "utf8");
})();
function generateFileHash(filePath) {
return new Promise((resolve, reject) => {
const hash = createHash("sha256");
const fileStream = createReadStream(filePath);
fileStream.on("data", (chunk) => {
hash.update(chunk);
});
fileStream.on("end", () => {
const fileHash = hash.digest("hex");
resolve(fileHash);
});
fileStream.on("error", (err) => {
reject(err);
});
});
}