-
Notifications
You must be signed in to change notification settings - Fork 16
/
build.js
76 lines (66 loc) · 1.77 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
const esbuild = require("esbuild");
const fs = require("fs/promises");
const fsButWihtoutPromises = require("fs");
const path = require("path");
function findClosingBrace(string) {
let c = 0;
let i = 0;
// prob doesn't work with unicode
while (i < string.length) {
const ch = string[i];
if (ch === "{") c++;
else if (ch === "}") c--;
if (c == -1) return i;
i++;
}
return null;
}
function transformToUseC(args) {
const content = fsButWihtoutPromises.readFileSync(args.path, "utf8");
const splits = content.split(/["']use c["'];/);
let result = splits[0];
for (let i = 1; i < splits.length; i++) {
const endOfCCode = findClosingBrace(splits[i]);
const cCode = splits[i].slice(0, endOfCCode);
result += `return runC("${encodeURIComponent(cCode)}");`;
result += splits[i].slice(endOfCCode, splits[i].length);
}
return result;
}
const useCPlugin = {
name: "use-c",
setup(build) {
build.onLoad({ filter: /.js$/ }, (args) => ({
contents: transformToUseC(args),
loader: "js",
}));
build.onLoad({ filter: /.jsx$/ }, (args) => ({
contents: transformToUseC(args),
loader: "jsx",
}));
},
};
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function build() {
await fs.rm("dist", { recursive: true, force: true });
await fs.mkdir("dist", { recursive: true });
console.log("Building project");
await sleep(Math.random() * 5000); // gotta keep up with the trends
await esbuild.build({
entryPoints: ["client.jsx"],
outfile: "dist/js.js",
minify: true,
bundle: true,
sourcemap: true,
plugins: [useCPlugin],
});
await fs.cp("index.html", "dist/index.html");
}
build().catch((err) => {
console.error(err);
process.exit(1);
});