-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_manifest.go
102 lines (87 loc) · 2.32 KB
/
handle_manifest.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
package caddy_esbuild_plugin
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
"go.uber.org/zap"
"net/http"
"path/filepath"
"strings"
)
type Metafile struct {
Inputs map[string]Import `json:"inputs"`
Outputs map[string]Output `json:"outputs"`
}
type Import struct {
Bytes int `json:"bytes"`
Imports []ImportFile `json:"imports"`
}
type ImportFile struct {
Path string `json:"path"`
Kind string `json:"kind"`
}
type Output struct {
Imports []interface{} `json:"imports"`
Exports []interface{} `json:"exports"`
EntryPoint string `json:"entryPoint,omitempty"`
Inputs map[string]Input `json:"inputs"`
Bytes int `json:"bytes"`
}
type Input struct {
BytesInOutput int `json:"bytesInOutput"`
}
func (m *Esbuild) handleManifest(w http.ResponseWriter, r *http.Request) error {
if m.esbuild == nil {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, _ = w.Write([]byte("{}"))
return nil
}
sha := sha1.New()
sha.Write([]byte(m.esbuild.Metafile))
etag := hex.EncodeToString(sha.Sum(nil))
cachedETag := r.Header.Get("If-None-Match")
if cachedETag == etag {
w.WriteHeader(304) //No change
return nil
}
w.Header().Set("ETag", etag)
w.Header().Set("Content-Type", "application/json")
manifest := make(map[string]string)
for target, output := range m.metafile.Outputs {
source := output.EntryPoint
target, _ := filepath.Abs(target)
m.logger.Debug("Source", zap.String("source", source), zap.String("target", target))
if source != "" && m.Target == "" {
target = source
if !strings.HasPrefix(target, "/") {
target = "/" + target
}
}
if source == "" {
//target is in form ../../../../_build/index.js
source, _ = filepath.Abs(target)
}
manifest[source] = target
}
content, err := json.Marshal(manifest)
if err != nil {
m.logger.Warn("Failed to build manifest.json", zap.Error(err))
w.WriteHeader(500)
return nil
}
if _, err = w.Write(content); err != nil {
m.logger.Warn("Failed to handle manifest.json", zap.Error(err))
w.WriteHeader(500)
return nil
}
w.WriteHeader(200)
return nil
}
/*
{
"build/form_task.css": "http://localhost:8080/build/form_task.css",
"build/form_task.js": "http://localhost:8080/build/form_task.js",
"build/search.js": "http://localhost:8080/build/search.js",
}
*/