-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
82 lines (72 loc) · 1.73 KB
/
main.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"net/http"
"os"
"github.com/davecgh/go-spew/spew"
"github.com/evanw/esbuild/pkg/api"
)
var port = "9090"
func main() {
result := api.Build(api.BuildOptions{
EntryPoints: []string{"./src/js/main.js"},
Outfile: "./src/build/out.js",
Bundle: true,
Loader: map[string]api.Loader{
".js": api.LoaderJSX,
".ttf": api.LoaderBase64,
},
Write: true,
Define: map[string]string{"ENV": "\"DEV\""},
Watch: &api.WatchMode{
OnRebuild: func(result api.BuildResult) {
if len(result.Errors) > 0 {
fmt.Printf("js watch build failed: %v", len(result.Errors))
spew.Dump(result.Errors)
} else {
spew.Dump("recompiled js...")
}
},
},
})
if len(result.Errors) > 0 {
spew.Dump(result.Errors)
os.Exit(1)
}
result = api.Build(api.BuildOptions{
EntryPoints: []string{"./src/css/main.css"},
Outfile: "./src/build/style.css",
Loader: map[string]api.Loader{
".svg": api.LoaderBase64,
".ttf": api.LoaderBase64,
},
Bundle: true,
Write: true,
Watch: &api.WatchMode{
OnRebuild: func(result api.BuildResult) {
if len(result.Errors) > 0 {
fmt.Printf("css watch build failed: %d errors\n", len(result.Errors))
spew.Dump(result.Errors)
} else {
spew.Dump("recompiled css...")
}
},
},
})
if len(result.Errors) > 0 {
spew.Dump(result.Errors)
os.Exit(1)
}
fmt.Printf("watching on localhost:%s...\n", port)
go serve()
// Returning from main() exits immediately in Go.
// Block forever so we keep watching and don't exit.
<-make(chan bool)
}
func serve() {
err := http.ListenAndServe(fmt.Sprintf(":%s", port), http.FileServer(http.Dir("src")))
if err != nil {
fmt.Println("Failed to start server", err)
return
}
}