-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.mjs
86 lines (76 loc) · 2.49 KB
/
watch.mjs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import esbuild from 'esbuild'
import {copy} from 'esbuild-plugin-copy'
import http from 'node:http'
esbuild.build({
entryPoints: ['src/hotReload.ts'],
bundle: true,
minify: true,
outdir: 'dist',
target: ['chrome58', 'firefox57', 'safari11'],
})
// https://esbuild.github.io/api/#overview
let context = await esbuild.context({
entryPoints: ['src/index.ts'],
bundle: true,
minify: true,
outdir: 'dist',
format: 'iife',
target: ['chrome58', 'firefox57', 'safari11'], //support edge?
plugins: [
copy({
assets: {
from: ['src/*.html'],
to: ['.'],
},
}),
],
})
// https://esbuild.github.io/api/#live-reload
await context.watch()
const {host, port} = await context.serve({
servedir: 'dist',
})
// https://esbuild.github.io/api/#serve-proxy
http
.createServer((request, response) => {
const options = {
hostname: host,
port: port,
path: request.url,
method: request.method,
headers: request.headers,
}
// Forward each incoming request to esbuild
const esbuildServerRequest = http.request(options, (esbuildServerResponse) => {
// https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction
// writeHead from esbuildServer to client
// if content type is html add code snippet
let modifiedBody = []
if (esbuildServerResponse.headers['content-type']?.startsWith('text/html')) {
esbuildServerResponse
.on('data', (chunk) => {
modifiedBody.push(chunk)
})
.on('end', () => {
modifiedBody = Buffer.concat(modifiedBody).toString()
// https://esbuild.github.io/api/#hot-reloading-css
const hotReloadCssSnippet = Buffer.from(
' <script src="/hotReload.js"></script>\n </body>'
).toString('utf8')
// modifiedBody =
// modifiedBody.substring(0, modifiedBody.indexOf('</body>')) + hotReloadCssSnippet
modifiedBody = modifiedBody.replace('</body>', hotReloadCssSnippet)
// end response to client
response.end(modifiedBody)
})
} else {
response.writeHead(esbuildServerResponse.statusCode, esbuildServerResponse.headers)
esbuildServerResponse.pipe(response, {end: true})
// end response to client
}
})
// Forward the body of the request to esbuild dev server
request.pipe(esbuildServerRequest, {end: true})
})
.listen(3001)
console.log(`Dev Server listening on http://${host}:3001`)