generated from digitallyinduced/thin-react-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
52 lines (48 loc) · 1.64 KB
/
server.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
import esbuild from 'esbuild'
import { createServer, request } from 'http'
import { spawn } from 'child_process'
const clients = []
esbuild
.build({
entryPoints: ['./app.jsx'],
bundle: true,
outfile: 'public/app.js',
banner: { js: ' (() => new EventSource("/esbuild").onmessage = () => location.reload())();' },
define: {
'process.env.NODE_ENV': JSON.stringify("development")
},
watch: {
onRebuild(error, result) {
clients.forEach((res) => res.write('data: update\n\n'))
clients.length = 0
console.log(error ? error : '...')
},
},
})
.catch(() => process.exit(1))
esbuild.serve({ servedir: './public', port: 3001 }, {}).then(() => {
createServer((req, res) => {
const { url, method, headers } = req
if (req.url === '/esbuild')
return clients.push(
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
})
)
const path = ~url.split('/').pop().indexOf('.') ? url : `/index.html` //for PWA with router
req.pipe(
request({ hostname: '0.0.0.0', port: 3001, path, method, headers }, (prxRes) => {
res.writeHead(prxRes.statusCode, prxRes.headers)
prxRes.pipe(res, { end: true })
}),
{ end: true }
)
}).listen(3000)
setTimeout(() => {
const op = { darwin: ['open'], linux: ['xdg-open'], win32: ['cmd', '/c', 'start'] }
const ptf = process.platform
if (clients.length === 0) spawn(op[ptf][0], [...[op[ptf].slice(1)], `http://localhost:3000`])
}, 1000) //open the default browser only if it is not opened yet
})