fuzzyworld

dao-game / files / tools / esbuild-build-and-serve.ts

esbuild-build-and-serve.ts

Blame: the commit that last changed each line · file · history

fc222723 2022-03-05 Pavel Safronov1import esbuild from 'esbuild';
2import http from 'http';
5648264d 2022-03-22 Pavel Safronov3import { serveOptions, buildOptions, hostPort, clearOutDir } from './esbuild-settings'
4
5clearOutDir();
fc222723 2022-03-05 Pavel Safronov6
9e157243 2022-03-21 Pavel Safronov7esbuild.serve(serveOptions, buildOptions).then((result) => {
8 const { host, port } = result;
9 http
10 .createServer((req, res) => {
11 const options: http.RequestOptions = {
12 hostname: host,
13 port: port,
14 path: req.url,
15 method: req.method,
16 headers: req.headers,
17 };
18 const proxyReq = http.request(options, (proxyRes) => {
19 if (proxyRes.statusCode === 404) {
20 res.writeHead(404, { 'Content-Type': 'text/html' });
21 res.end('<h1>A custom 404 page</h1>');
22 return;
23 }
fc222723 2022-03-05 Pavel Safronov24
9e157243 2022-03-21 Pavel Safronov25 res.writeHead(proxyRes.statusCode!, proxyRes.headers);
26 proxyRes.pipe(res, { end: true });
27 });
fc222723 2022-03-05 Pavel Safronov28
9e157243 2022-03-21 Pavel Safronov29 req.pipe(proxyReq, { end: true });
30 })
31 .listen(hostPort, () => {
32 const url = `http://localhost:${hostPort}/`;
33 // childProcess.exec(`start ${url}`);
34 console.log(`Server serving at ${url}`);
35 });
36});