Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
titaniumnetwork-dev
GitHub Repository: titaniumnetwork-dev/Ultraviolet
Path: blob/main/build.js
302 views
1
import { rimraf } from "rimraf";
2
import { copyFile, mkdir, readFile, writeFile } from "node:fs/promises";
3
import { build } from "esbuild";
4
import { execSync } from "node:child_process";
5
6
// read version from package.json
7
const pkg = JSON.parse(await readFile("package.json"));
8
process.env.ULTRAVIOLET_VERSION = pkg.version;
9
10
const isDevelopment = process.argv.includes("--dev");
11
12
await rimraf("dist");
13
await mkdir("dist");
14
15
// don't compile these files
16
await copyFile("src/sw.js", "dist/sw.js");
17
await copyFile("src/uv.config.js", "dist/uv.config.js");
18
19
let builder = await build({
20
platform: "browser",
21
sourcemap: true,
22
minify: !isDevelopment,
23
entryPoints: {
24
"uv.bundle": "./src/rewrite/index.js",
25
"uv.client": "./src/client/index.js",
26
"uv.handler": "./src/uv.handler.js",
27
"uv.sw": "./src/uv.sw.js",
28
},
29
define: {
30
"process.env.ULTRAVIOLET_VERSION": JSON.stringify(
31
process.env.ULTRAVIOLET_VERSION
32
),
33
"process.env.ULTRAVIOLET_COMMIT_HASH": (() => {
34
try {
35
let hash = JSON.stringify(
36
execSync("git rev-parse --short HEAD", {
37
encoding: "utf-8",
38
}).replace(/\r?\n|\r/g, "")
39
);
40
41
return hash;
42
} catch (e) {
43
return "unknown";
44
}
45
})(),
46
},
47
bundle: true,
48
treeShaking: true,
49
metafile: isDevelopment,
50
logLevel: "info",
51
outdir: "dist/",
52
});
53
if (isDevelopment) {
54
await writeFile("metafile.json", JSON.stringify(builder.metafile));
55
}
56
57