-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserve.ts
More file actions
79 lines (67 loc) · 3.55 KB
/
serve.ts
File metadata and controls
79 lines (67 loc) · 3.55 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
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ░░░░░░░░▄▀░█▀▄░█▀▀░█▀▀░█░█░█░░░█▀█░█▀▄░░░░░█░░░█▀█░█░█░█▀█░█░█░▀█▀░▀▄░░░░░░░░
// ░░░░░░░▀▄░░█▀▄░█▀▀░█░█░█░█░█░░░█▀█░█▀▄░▀▀▀░█░░░█▀█░░█░░█░█░█░█░░█░░░▄▀░░░░░░░
// ░░░░░░░░░▀░▀░▀░▀▀▀░▀▀▀░▀▀▀░▀▀▀░▀░▀░▀░▀░░░░░▀▀▀░▀░▀░░▀░░▀▀▀░▀▀▀░░▀░░▀░░░░░░░░░
// ░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ * Copyright (c) 2026, the Regular Layout Authors. This file is part * ┃
// ┃ * of the Regular Layout library, distributed under the terms of the * ┃
// ┃ * [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). * ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import * as esbuild from "esbuild";
import * as http from "node:http";
const PORT = 8081;
const build_config: esbuild.BuildOptions = {
entryPoints: ["examples/index.ts"],
bundle: true,
outdir: ".esbuild-serve",
platform: "browser",
format: "esm",
sourcemap: true,
splitting: true,
};
async function serve(): Promise<void> {
const ctx = await esbuild.context(build_config);
const { port } = await ctx.serve({
servedir: ".",
onRequest: (args) => {
console.log(`${args.method} ${args.path} - ${args.status}`);
},
});
const esbuild_host = "127.0.0.1";
console.log(`esbuild server running on ${esbuild_host}:${port}`);
const proxy = http.createServer((req, res) => {
const options: http.RequestOptions = {
hostname: esbuild_host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
};
const proxy_req = http.request(options, (proxy_res) => {
if (proxy_res.statusCode === 404) {
res.writeHead(404, { "Content-Type": "text/html" });
res.end("<h1>404 - Not Found</h1>");
return;
}
res.writeHead(proxy_res.statusCode || 200, proxy_res.headers);
proxy_res.pipe(res, { end: true });
});
proxy_req.on("error", (err) => {
console.error(`Proxy request error for ${req.url}:`, err.message);
res.writeHead(500, { "Content-Type": "text/html" });
res.end("<h1>500 - Internal Server Error</h1>");
});
req.pipe(proxy_req, { end: true });
});
proxy.listen(PORT, () => {
console.log(`Development server running at http://localhost:${PORT}`);
console.log(`Serving directory: ${process.cwd()}`);
console.log("\nWatching for changes...");
});
await ctx.watch();
}
serve().catch((error) => {
console.error(error);
process.exit(1);
});