-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathesbuildConfig.js
55 lines (47 loc) · 1.77 KB
/
esbuildConfig.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
import esbuild from 'esbuild'
import * as http from 'http';
let context = await esbuild.context({
entryPoints: ['./src/App.jsx'],
bundle: true,
minify: true,
outfile: './public/build/bundle.js',
jsx: 'automatic',
loader: { '.js': 'jsx', '.eot': 'file', '.ttf': 'file', '.woff': 'file', '.woff2': 'file', '.png': 'dataurl' }
})
await context.watch();
// Serve the esbuild server on a random port (the requests are forwarded from port 9001 --> this)
let { host, port } = await context.serve({
servedir: 'public',
port: 1274
})
// https://github.com/evanw/esbuild/issues/1601
// Esbuild doesn't work with react router out of the box. Seems like the issue has to do with it
// running on nodeJS and not having access to browser APIs like history (which routers rely on). <--???? not sure if this is the reason tho
// So this is a workaround proxy server that forwards the requests to esbuild
const proxy = http.createServer((req, res) => {
const options = {
hostname: host,
port: port,
path: req.url,
method: req.method,
headers: req.headers,
}
const proxyReq = http.request(options, proxyRes => {
if (proxyRes.statusCode === 404) {
const redirectReq = http.request({ ...options, path: "/" }, (proxyRes) => {
// Forward the response from esbuild to the client
res.writeHead(proxyRes.statusCode, proxyRes.headers);
proxyRes.pipe(res, { end: true });
});
redirectReq.end();
} else {
// 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 });
})
proxy.listen(9091);
console.log(`🥯 Started bagels on: http://127.0.0.1:9091`);