-
Notifications
You must be signed in to change notification settings - Fork 0
/
importmap.go
180 lines (142 loc) · 3.03 KB
/
importmap.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package acid
import (
"bytes"
"embed"
"encoding/json"
"errors"
"fmt"
"html/template"
"path/filepath"
"strings"
)
type CDN int
const (
JsDelivr CDN = iota
)
var (
currentCDN CDN
im *importMap
errImportMapNotSetup = errors.New("importmap hasn't been setup")
)
type Package struct {
Name string `json:"name,omitempty"`
URL string `json:"url,omitempty"`
}
type importMap struct {
Packages []Package
Structure Structure
}
type Structure struct {
Imports map[string]string `json:"imports,omitempty"`
}
func newImportMap() *importMap {
return &importMap{
Packages: []Package{},
Structure: Structure{
Imports: make(map[string]string),
},
}
}
func UseCDN(cdn CDN) {
currentCDN = cdn
}
func Pin(name string, version string) {
pin(name, buildURL(name, version))
}
func pin(name, url string) {
if im == nil {
im = newImportMap()
}
pkg := Package{
Name: name,
URL: url,
}
im.Packages = append(im.Packages, pkg)
im.Structure.Imports[pkg.Name] = pkg.URL
}
func PinAllFrom(fs *embed.FS) {
err := CalculateDigests(fs, "static")
if err != nil {
panic(err)
}
files, err := listFiles(fs, "static")
if err != nil {
panic(err)
}
for _, file := range files {
if filepath.Ext(file) != ".js" {
continue
}
url := assetsWithDigests.ReverseMap[file]
name := strings.TrimSuffix(strings.TrimPrefix(file, "static/javascript/"), ".js")
pin(name, fmt.Sprintf("/assets/%s", url))
}
}
func listFiles(fileSystem *embed.FS, path string) ([]string, error) {
if len(path) == 0 {
path = "."
}
entries, err := fileSystem.ReadDir(path)
if err != nil {
return nil, err
}
// var out []string
out := make([]string, 0, len(entries))
for _, entry := range entries {
fullPath := filepath.Join(path, entry.Name())
if entry.IsDir() {
res, err := listFiles(fileSystem, fullPath)
if err != nil {
return nil, err
}
out = append(out, res...)
continue
}
out = append(out, fullPath)
}
return out, err
}
func buildURL(p string, version string) string {
switch currentCDN {
case JsDelivr:
return fmt.Sprintf("https://cdn.jsdelivr.net/npm/%s@%s/+esm", p, version)
default:
panic("Unknown CDN")
}
}
func Packages() []Package {
return im.Packages
}
func renderImportMap() (template.HTML, error) {
if im == nil {
return "", errImportMapNotSetup
}
return im.Render()
}
func (m *importMap) Imports() (template.HTML, error) {
b, err := json.MarshalIndent(m.Structure, "", "\t")
if err != nil {
return "", err
}
return template.HTML(b), nil // #nosec G203 -- Don't want this to be escaped
}
// Render returns a HTML snippet to use in a template.
func (m *importMap) Render() (template.HTML, error) {
tmpl, err := template.New("").Parse(`
<script type="importmap">
{{ .Imports }}
</script>
{{ range .Packages }}
<link rel="modulepreload" href="{{ .URL }}">
{{ end }}
`)
if err != nil {
return "", err
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, m)
if err != nil {
return "", err
}
return template.HTML(buf.String()), nil // #nosec G203 -- Don't want this to be escaped
}