dao-game / files / tools / esbuild-build-and-serve.ts
esbuild-build-and-serve.ts
fc222723 2022-03-05 Pavel Safronov | 1 | import esbuild from 'esbuild'; |
| 2 | import http from 'http'; | |
5648264d 2022-03-22 Pavel Safronov | 3 | import { serveOptions, buildOptions, hostPort, clearOutDir } from './esbuild-settings' |
| 4 | ||
| 5 | clearOutDir(); | |
fc222723 2022-03-05 Pavel Safronov | 6 | |
9e157243 2022-03-21 Pavel Safronov | 7 | esbuild.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 Safronov | 24 | |
9e157243 2022-03-21 Pavel Safronov | 25 | res.writeHead(proxyRes.statusCode!, proxyRes.headers); |
| 26 | proxyRes.pipe(res, { end: true }); | |
| 27 | }); | |
fc222723 2022-03-05 Pavel Safronov | 28 | |
9e157243 2022-03-21 Pavel Safronov | 29 | 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 | }); |