-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
71 lines (65 loc) · 1.67 KB
/
build.js
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
const esbuild = require("esbuild");
const livereload = require("livereload");
const StaticServer = require('static-server');
build();
async function build() {
let watch = false;
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);
if (args.watch) {
watch = {
onRebuild(error) {
var dstr = "[" + new Date().toLocaleTimeString() + "] ";
if (error) {
console.error(dstr + 'Change detected; rebuild failed:', error);
return;
}
console.log(dstr + 'Change detected; rebuild OK');
},
};
}
esbuild.build({
entryPoints: ["src/app.js"],
outfile: "public/app.js",
bundle: true,
sourcemap: true,
minify: false,
watch: watch,
}).catch(err => {
console.error("Unexpected error; quitting.");
if (err) console.error(err);
process.exit(1);
}).then(() => {
console.log("Build finished.");
if (args.watch) {
livereload.createServer().watch("./public");
console.log("Watching changes, with livereload...");
var server = new StaticServer({
rootPath: './public',
port: 8080,
index: 'index.html',
});
server.start(function () {
console.log('Server listening at ' + server.port);
});
}
});
}