-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.go
62 lines (57 loc) · 1.49 KB
/
watch.go
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
package main
import (
"fmt"
"log"
"net/http"
"github.com/evanw/esbuild/pkg/api"
)
func main() {
events := make(chan struct{})
api.Build(api.BuildOptions{
Bundle: true,
EntryPoints: []string{"in.js"},
Outfile: "out.js",
Watch: &api.WatchMode{
OnRebuild: func(result api.BuildResult) {
if len(result.Errors) > 0 {
fmt.Printf("watch build failed: %d errors\n", len(result.Errors))
} else {
fmt.Printf("watch build succeeded: %d warnings\n", len(result.Warnings))
}
events <- struct{}{}
},
},
Write: true,
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
// Add headers needed for server-sent events (SSE):
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
flusher, ok := w.(http.Flusher)
if !ok {
log.Fatalln("Your browser does not support server-sent events (SSE).")
return
}
for {
select {
case <-events:
// NOTE: I needed to add "data" to get server-sent events to work. YMMV.
fmt.Fprintf(w, "event: reload\ndata\n\n")
flusher.Flush()
case <-r.Context().Done():
// No-op
return
}
}
})
http.HandleFunc("/out.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "out.js")
})
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatalln(err)
}
}