-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdev-server.mjs
More file actions
99 lines (89 loc) · 2.3 KB
/
dev-server.mjs
File metadata and controls
99 lines (89 loc) · 2.3 KB
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
//@ts-check
import { watch } from "rollup";
import createServer from "connect";
import serveStatic from "serve-static";
import config from "./rollup.config.mjs";
import cors from "cors";
import fs from "node:fs";
import { globSync } from "glob";
import Handlebars from "handlebars";
import path from "node:path";
process.env.NODE_ENV = "development";
const watcher = watch({
...config,
watch: {
skipWrite: true, // we write it out ourselves below
},
});
const components = globSync("*/**/*.component.js", {
ignore: ["dist/**"],
}).map((filePath) => filePath.split(path.sep)[2].replace(".component.js", ""));
let didStartServer = false;
function startServer() {
if (didStartServer) {
return;
}
const app = createServer();
app.use(
"/components",
cors({
origin: [
"https://yusu.org",
"https://yorksu.org",
"https://yu-development.sums.su",
],
}),
);
app.use("/components", serveStatic(".dev"));
const htmlTemplate = Handlebars.compile(
fs.readFileSync("./dev-banner.html", { encoding: "utf-8" }),
);
app.use((req, res) => {
if (req.originalUrl === "/") {
const page = htmlTemplate({
components: components.map((c) => ({
name: `yorksu-${c}`,
url: `http://localhost:3000/components/${c}.component.js`,
})),
});
res
.writeHead(200, {
"Content-Length": Buffer.byteLength(page),
"Content-Type": "text/html",
})
.end(page);
return;
}
res.writeHead(404).end("not found");
});
app.listen(3000, "0.0.0.0", () => {
console.log("Ready on http://localhost:3000");
});
didStartServer = true;
}
/** @type {any} */
let buildStart = null;
watcher.on("event", async (event) => {
switch (event.code) {
case "START":
buildStart = process.hrtime();
break;
case "BUNDLE_END":
if (buildStart) {
const took = process.hrtime(buildStart);
console.log(`Built in ${took[0] * 1000 + took[1] / 1000000}ms`);
}
await event.result.write({
dir: ".dev",
});
console;
startServer();
break;
case "ERROR":
console.error("⚠️ BUILD ERROR 💣");
console.error(event.error);
}
if ("result" in event && event.result) {
await event.result.close();
}
});