generated from curveball/starter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.js
executable file
·75 lines (61 loc) · 1.91 KB
/
serve.js
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
#!/usr/bin/env node
/* eslint-disable @typescript-eslint/no-var-requires */
/* eslint-disable no-console */
const esbuild = require('esbuild');
const esbuildSass = require('esbuild-sass-plugin');
const http = require('http');
// build & compile scss → css
esbuild.build({
entryPoints: ['./src/styles/main.scss'],
outfile: './public/css/custom.css',
watch: true,
plugins: [esbuildSass.sassPlugin()]
}).catch(() => process.exit(1));
// Start esbuild's server on a random local port
esbuild.serve({
servedir: 'public/',
}, {
bundle: true,
outfile: 'public/script.js',
entryPoints: ['src/App.tsx'],
}).then(result => {
// The result tells us where esbuild's local server is
const {host, port} = result;
// Then start a proxy server on port 3000
http.createServer((req, res) => {
const options = {
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
};
console.log(req.url);
// Forward each incoming request to esbuild
const proxyReq = http.request(options, proxyRes => {
// If esbuild returns "not found", send a custom 404 page
if (proxyRes.statusCode === 404) {
const newOptions = {
...options,
path: '/',
};
const proxyReq = http.request(newOptions, proxyRes => {
// Otherwise, forward the response from esbuild to the client
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
proxyReq.end();
return;
}
// Otherwise, forward the response from esbuild to the client
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
// Forward the body of the request to esbuild
req.pipe(proxyReq, { end: true });
}).listen(8902);
console.log(
`Starting dev server
Listening on http://127.0.0.1:%i`
, 8902);
});