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