-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaddyfile.go
161 lines (141 loc) · 3.84 KB
/
caddyfile.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
158
159
160
161
package caddy_esbuild_plugin
import (
"github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile"
"github.com/caddyserver/caddy/v2/modules/caddyhttp"
"github.com/evanw/esbuild/pkg/api"
"os"
"path/filepath"
"strings"
)
func init() {
httpcaddyfile.RegisterHandlerDirective("esbuild", parseCaddyfileEsbuild)
}
// parseCaddyfileEsbuild sets up a basic rewrite handler from Caddyfile tokens. Syntax:
//
// esbuild [source]
// esbuild ./assets/index.js {
// auto_reload
// sass
// target /_build
// }
//
// sass requires cgo to work
//
// Only URI components which are given in <to> will be set in the resulting URI.
// See the docs for the rewrite handler for more information.
func parseCaddyfileEsbuild(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
if !h.Next() {
return nil, h.ArgErr()
}
var esbuild Esbuild
esbuild.Target = ""
esbuild.LiveReload = false
esbuild.Scss = false
esbuild.FileHash = false
esbuild.Loader = make(map[string]string)
esbuild.Defines = make(map[string]string)
esbuild.NodePaths = []string{}
esbuild.Loader[".png"] = "file"
esbuild.Loader[".svg"] = "file"
esbuild.Loader[".js"] = "jsx"
for h.NextArg() {
val := h.Val()
switch val {
case "file_hash":
esbuild.FileHash = true
case "live_reload":
esbuild.LiveReload = true
case "scss":
if esbuild.hasSassSupport() == false {
return nil, h.Err("sass requires caddy to be compiled with CGO and libsass available")
}
esbuild.Scss = true
case "env":
esbuild.Env = true
default:
alias := parseSourceName(val)
esbuild.Sources = append(esbuild.Sources, api.EntryPoint{
OutputPath: alias,
InputPath: val,
})
}
}
for nesting := h.Nesting(); h.NextBlock(nesting); {
switch h.Val() {
case "file_hash":
esbuild.FileHash = true
case "live_reload":
esbuild.LiveReload = true
case "scss":
if esbuild.hasSassSupport() == false {
return nil, h.Err("sass requires caddy to be compiled with CGO and libsass available")
}
esbuild.Scss = true
case "env":
esbuild.Env = true
case "source":
if !h.NextArg() {
return nil, h.Err("source requires asset filename: source ./src/index.js")
}
source := h.Val()
alias := parseSourceName(source)
if h.NextArg() {
alias = h.Val()
}
esbuild.Sources = append(esbuild.Sources, api.EntryPoint{
OutputPath: alias,
InputPath: source,
})
case "target":
if !h.NextArg() {
return nil, h.Err("source requires path: target /build")
}
target := h.Val()
if !strings.HasPrefix(target, "/") {
target = "/" + target
}
if strings.HasSuffix(target, "/") {
target = strings.TrimSuffix(target, "/")
}
esbuild.Target = target
case "loader":
if !h.NextArg() {
return nil, h.Err("loader require filetype and loader: loader .svg text")
}
filetype := h.Val()
if !h.NextArg() {
return nil, h.Err("loader require filetype and loader: loader .svg text")
}
loaderValue := h.Val()
esbuild.Loader[filetype] = loaderValue
case "define":
if !h.NextArg() {
return nil, h.Err("loader require filetype and loader: loader .svg text")
}
define := h.Val()
if !h.NextArg() {
return nil, h.Err("loader require filetype and loader: loader .svg text")
}
value := h.Val()
esbuild.Defines[define] = value
case "node_path":
if !h.NextArg() {
return nil, h.Err("node_ath requires path: node_path ../node_modules")
}
path := h.Val()
esbuild.NodePaths = append(esbuild.NodePaths, path)
}
}
if len(esbuild.NodePaths) == 0 {
cwd, _ := os.Getwd()
pattern := filepath.Join(cwd, "*", "node_modules")
nodePaths, _ := filepath.Glob(pattern)
esbuild.NodePaths = nodePaths
}
return &esbuild, nil
}
func parseSourceName(source string) string {
alias := filepath.Base(source)
alias = strings.TrimSuffix(alias, filepath.Ext(alias))
return alias
}