-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
157 lines (136 loc) · 3.6 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package caddy_esbuild_plugin
import (
"fmt"
"github.com/caddyserver/caddy/v2"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/evanw/esbuild/pkg/api"
"go.uber.org/zap"
"net/http"
"path/filepath"
"strings"
"time"
)
type Esbuild struct {
Target string `json:"target,omitempty"`
LiveReload bool `json:"auto_reload,omitempty"`
Scss bool `json:"scss,omitempty"`
Env bool `json:"env,omitempty"`
Loader map[string]string `json:"loader,omitempty"`
FileHash bool `json:"file_hash,omitempty"`
Defines map[string]string `json:"defines,omitempty"`
Sources []api.EntryPoint `json:"source,omitempty"`
NodePaths []string `json:"n_ode_paths,omitempty"`
logger *zap.Logger
esbuild *api.BuildResult
hashes map[string]string
globalQuit chan struct{}
lastDuration *time.Duration
metafile *Metafile
}
func (m *Esbuild) Cleanup() error {
close(m.globalQuit)
return nil
}
func init() {
caddy.RegisterModule(Esbuild{})
}
// CaddyModule returns the Caddy module information.
func (Esbuild) CaddyModule() caddy.ModuleInfo {
return caddy.ModuleInfo{
ID: "http.handlers.esbuild",
New: func() caddy.Module { return new(Esbuild) },
}
}
func (m *Esbuild) Provision(ctx caddy.Context) error {
m.logger = ctx.Logger(m)
m.hashes = make(map[string]string)
m.globalQuit = make(chan struct{})
m.Defines = make(map[string]string)
m.initEsbuild()
var sources []string
for _, s := range m.Sources {
sources = append(sources, s.InputPath)
}
var loaders []string
for ext, l := range m.Loader {
loaders = append(loaders, ext+"="+l)
}
m.logger.Info("Initialized esbuild",
zap.String("target", m.Target),
zap.Strings("sources", sources),
zap.Strings("loaders", loaders),
zap.Bool("sass", m.Scss),
zap.Bool("env", m.Env),
zap.Bool("live_reload", m.LiveReload),
zap.Strings("node_path", m.NodePaths))
return nil
}
// Validate implements caddy.Validator.
func (m *Esbuild) Validate() error {
if len(m.Sources) == 0 {
return fmt.Errorf("no source file")
}
for _, l := range m.Loader {
_, err := ParseLoader(l)
if err != nil {
return err
}
}
return nil
}
func (m *Esbuild) ServeHTTP(w http.ResponseWriter, r *http.Request, h caddyhttp.Handler) error {
if r.Method != "GET" {
return h.ServeHTTP(w, r)
}
outdir := m.Target
if outdir == "" {
outdir = "/_build"
}
file := r.RequestURI
if index := strings.Index(file, "?"); index > 1 {
file = file[:index-1]
}
if file == outdir+"/__livereload" {
_ = m.handleLiveReload(w, r)
return nil
}
if file == outdir+"/manifest.json" {
_ = m.handleManifest(w, r)
return nil
}
if m.esbuild != nil {
for _, f := range m.esbuild.OutputFiles {
if file == f.Path {
return m.handleAsset(w, r, f)
}
}
if m.Target == "" {
for target, output := range m.metafile.Outputs {
entrypoint := output.EntryPoint
if !strings.HasPrefix(entrypoint, "/") {
entrypoint = "/" + entrypoint
}
target, _ = filepath.Abs(target)
m.logger.Debug("Handling output files",
zap.String("file", file),
zap.String("entrypoint", entrypoint),
zap.String("target", target))
if file == entrypoint && file != "/" {
for _, f := range m.esbuild.OutputFiles {
if target == f.Path {
return m.handleAsset(w, r, f)
}
}
}
}
}
}
return h.ServeHTTP(w, r)
}
// Interface guards
var (
_ caddy.Provisioner = (*Esbuild)(nil)
_ caddy.Validator = (*Esbuild)(nil)
_ caddy.CleanerUpper = (*Esbuild)(nil)
_ caddyhttp.MiddlewareHandler = (*Esbuild)(nil)
)