-
Notifications
You must be signed in to change notification settings - Fork 27
/
build-web.js
executable file
·172 lines (164 loc) · 5.32 KB
/
build-web.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/usr/bin/env node
/* eslint-env node */
/* eslint @typescript-eslint/no-var-requires:0 */
const esbuild = require("esbuild");
const path = require("path");
const fs = require("fs");
const { createServer } = require("http");
const { withWatcher } = require("./scripts/esbuild/watcher");
const { wasmTextPlugin } = require("./scripts/esbuild/wasm-text");
let dev = false;
let watch = false;
for (const arg of process.argv.slice(2)) {
switch (arg) {
case "--development":
dev = true;
break;
case "--watch":
watch = true;
break;
}
}
let buildConfig = {
bundle: true,
logLevel: "info",
entryPoints: [
path.join(__dirname, "src", "web", "shell", "shell"),
path.join(__dirname, "src", "web", "tests", "tests"),
path.join(__dirname, "src", "web", "benchmarks", "benchmarks"),
path.join(__dirname, "src", "web", "examples", "prompt", "prompt"),
path.join(__dirname, "src", "web", "examples", "fetch", "fetch"),
path.join(__dirname, "src", "web", "thurtle", "thurtle"),
],
entryNames: dev ? "[name]" : "[name]-c$[hash]",
assetNames: "[name]-c$[hash]",
// target: "es6",
outdir: path.join(__dirname, "public/waforth/dist"),
publicPath: "/waforth/dist",
external: ["fs", "stream", "util", "events", "path"],
minify: !dev,
loader: {
".wasm": "binary",
".js": "jsx",
".fs": "text",
".f": "text",
".fr": "text",
".fth": "text",
".svg": "file",
},
define: {
WAFORTH_VERSION: watch
? `"dev"`
: // : `"${new Date().toISOString().replace(/T.|)}>#g, "")}"`,
JSON.stringify(JSON.parse(fs.readFileSync("package.json")).version),
},
sourcemap: true,
metafile: true,
plugins: [
wasmTextPlugin({ debug: true }),
// Resolve 'waforth' to the main entrypoint (for examples)
{
name: "waforth",
setup: (build) => {
build.onResolve({ filter: /^waforth$/ }, () => {
return { path: path.join(__dirname, "src", "web", "waforth.ts") };
});
},
},
],
};
const INDEX_TEMPLATE = `<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="shortcut icon" href="/waforth/favicon.ico" type="image/x-icon" />
<link rel="icon" href="/waforth/favicon.ico" type="image/x-icon" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<meta name="theme-color" content="#000000" />
<link rel="apple-touch-icon" href="/waforth/apple-touch-icon.png" />
<link href="/waforth/dist/$BASE.css" rel="stylesheet" />
<title>$TITLE</title>
</head>
<body>
<script type="text/javascript" src="/waforth/dist/$BASE.js"></script>
</body>
</html>
`;
async function handleBuildFinished(result) {
const indexes = [
["WAForth", "shell", "public/waforth"],
["WAForth Tests", "tests", "public/waforth/tests"],
["WAForh Benchmarks", "benchmarks", "public/waforth/benchmarks"],
["WAForth Prompt Example", "prompt", "public/waforth/examples/prompt"],
["WAForth Fetch Example", "fetch", "public/waforth/examples/fetch"],
["Thurtle", "thurtle", "public/waforth/thurtle", true],
];
for (const [title, base, outpath, bs] of indexes) {
let index = INDEX_TEMPLATE.replace(/\$BASE/g, base).replace(
/\$TITLE/g,
title
);
if (bs) {
index = index.replace(
"<body>",
`<body>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous" />
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg+OMhuP+IlRH9sENBO0LRn5q+8nbTov4+1p" crossorigin="anonymous"></script>
`
);
}
for (const [out] of Object.entries(result.metafile.outputs)) {
const outfile = path.basename(out);
const sourcefile = outfile.replace(/-c\$[^.]+\./, ".");
// console.log("%s -> %s", sourcefile, outfile);
index = index.replace(`/${sourcefile}`, `/${outfile}`);
}
await fs.promises.mkdir(outpath, { recursive: true });
await fs.promises.writeFile(path.join(outpath, "index.html"), index);
}
}
if (watch) {
// Simple static file server
createServer(async function (req, res) {
const url = req.url.replace(/\?.*/g, "");
let f = path.join(__dirname, "public", url);
try {
if ((await fs.promises.lstat(f)).isDirectory()) {
f = path.join(f, "index.html");
}
} catch (e) {
// pass
}
try {
const data = await fs.promises.readFile(f);
res.writeHead(
200,
url.endsWith(".svg")
? {
"Content-Type": "image/svg+xml",
}
: undefined
);
res.end(data);
} catch (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
}
}).listen(8080);
console.log("listening on port 8080");
buildConfig = withWatcher(buildConfig, handleBuildFinished, 8081);
}
(async () => {
await fs.promises.mkdir("public/waforth", { recursive: true });
await fs.promises.copyFile(
"public/favicon.ico",
"public/waforth/favicon.ico"
);
try {
handleBuildFinished(await esbuild.build(buildConfig));
} catch (e) {
process.exit(1);
}
})();