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
|
import * as esbuild from "esbuild"
import * as fs from "fs";
import { livereloadPlugin } from "@jgoz/esbuild-plugin-livereload";
const port = 8085;
const timestamp = (+new Date).toString(36);
const args = (argList => {
let res = {};
let opt, thisOpt, curOpt;
for (let i = 0; i < argList.length; i++) {
thisOpt = argList[i].trim();
opt = thisOpt.replace(/^\-+/, "");
if (opt === thisOpt) {
// argument value
if (curOpt) res[curOpt] = opt;
curOpt = null;
} else {
// argument name
curOpt = opt;
res[curOpt] = true;
}
}
//console.log(res);
return res;
})(process.argv);
const cacheBusterPlugin = {
name: "cacheBusterPlugin",
setup(build) {
build.onEnd(() => {
const dstr = "[" + new Date().toLocaleTimeString() + "] ";
console.log(dstr + "Build finished");
const index = fs.readFileSync("./public/index.html", {encoding: "utf-8"});
const newIndex = index.replaceAll("%%VERSION%%", timestamp);
fs.writeFileSync("./public/index.html", newIndex);
})
},
};
async function build() {
const entryPoints = ["src/index.html", "src/icons.svg", "src/app.css", "src/app.js", "src/fonts/*"];
const plugins = [cacheBusterPlugin];
if (args.watch) plugins.push(livereloadPlugin());
const context = await esbuild.context({
entryPoints: entryPoints,
outdir: "public",
bundle: true,
sourcemap: true,
loader: {
".html": "copy",
".css": "copy",
".svg": "copy",
".aiff": "copy",
".woff2": "copy",
},
write: true,
metafile: true,
plugins: plugins,
});
if (args.watch) {
await context.watch();
await context.serve({
port: port,
});
}
else {
await context.rebuild();
await context.dispose();
process.exit(0);
}
}
void build();
|