-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
executable file
·94 lines (91 loc) · 2.41 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
const fs = require("fs");
const child_process = require("child_process");
const statik = require("node-static");
const esbuild = require("esbuild");
const http = require("http");
const buildConfig = {
entryPoints: ["src/index.js"],
bundle: true,
minify:true,
loader: { ".css": "text" },
};
function build() {
var gitId = "undefined";
try {
gitId = child_process
.execSync("git rev-parse --short HEAD")
.asciiSlice()
.replace("\n", "");
gitId = "'" + gitId + "'";
} catch (e) {}
esbuild
.build(
Object.assign(
{
define: { GITID: gitId},
sourcemap: true,
outfile: "build/index.js",
},
buildConfig
)
)
.catch(() => {})
.then(() => {
fs.copyFile("src/index.html", "build/index.html", () => {});
if (process.argv.includes("electron")) {
fs.copyFile("src/electron.js", "build/electron.js", () => {});
fs.copyFile("./package.electron.json", "build/package.json", () => {});
} else {
fs.copyFile("src/sw.js", "build/sw.js", () => {});
fs.copyFile("src/menifest.json", "build/menifest.json", () => {});
fs.copyFile("docs/icon.png", "build/icon.png", () => {});
}
console.log("Build finished!");
});
}
function liveserver() {
const file = new statik.Server("./src");
http
.createServer(function (request, response) {
if (request.url == "/index.js") {
response.writeHead(200, { "Content-Type": "text/javascript" });
res = esbuild.buildSync(
Object.assign(
{ write: false, define: { GITID: "'LIVESERVER'" } },
buildConfig
)
);
response.write(res.outputFiles[0].text);
response.end();
}
file.serve(request, response);
})
.listen(8080);
}
if (!process.argv.includes("liveserver")) {
build();
if (process.argv.includes("watch")) {
fs.watch("./src", () => {
console.log("File changed rebuilding");
build();
});
}
if (process.argv.includes("serve")) {
const file = new statik.Server("./build");
http
.createServer(function (request, response) {
request
.addListener("end", function () {
//
// Serve files!
//
file.serve(request, response);
})
.resume();
})
.listen(8080);
}
} else {
liveserver();
}