-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bundle.go
273 lines (241 loc) · 5.44 KB
/
bundle.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package singlepage
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"mime"
"path/filepath"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"zgo.at/zstd/zint"
)
const (
_ zint.Bitflag16 = 0
HTML zint.Bitflag16 = 1 << (iota - 1)
CSS
JS
Image
Font
)
// Options for Bundle().
type Options struct {
Root string
Strict bool
Quiet bool
Local zint.Bitflag16
Remote zint.Bitflag16
Minify zint.Bitflag16
}
// Everything is an Options struct with everything enabled.
var Everything = Options{
Local: CSS | JS | Image,
Remote: CSS | JS | Image,
Minify: CSS | JS | Image,
}
var minifier *minify.M
func init() {
minifier = minify.New()
minifier.AddFunc("css", css.Minify)
minifier.AddFunc("html", html.Minify)
minifier.AddFunc("js", js.Minify)
}
// NewOptions creates a new Options instance.
func NewOptions(root string, strict, quiet bool) Options {
return Options{Root: root, Strict: strict, Quiet: quiet}
}
// Commandline modifies the Options from the format accepted in the commandline
// tool's flags.
func (opts *Options) Commandline(local, remote, minify []string) error {
for _, v := range local {
switch strings.TrimSpace(strings.ToLower(v)) {
case "":
continue
case "css":
opts.Local |= CSS
case "js", "javascript":
opts.Local |= JS
case "img", "image", "images":
opts.Local |= Image
case "font", "fonts":
opts.Local |= Font
default:
return fmt.Errorf("unknown value for -local: %q", v)
}
}
for _, v := range remote {
switch strings.TrimSpace(strings.ToLower(v)) {
case "":
continue
case "css":
opts.Remote |= CSS
case "js", "javascript":
opts.Remote |= JS
case "img", "image", "images":
opts.Remote |= Image
case "font", "fonts":
opts.Remote |= Font
default:
return fmt.Errorf("unknown value for -remote: %q", v)
}
}
for _, v := range minify {
switch strings.TrimSpace(strings.ToLower(v)) {
case "":
continue
case "css":
opts.Minify |= CSS
case "js", "javascript":
opts.Minify |= JS
case "html":
opts.Minify |= HTML
default:
return fmt.Errorf("unknown value for -minify: %q", v)
}
}
return nil
}
// Bundle the resources in a HTML document according to the given options.
func Bundle(html []byte, opts Options) (string, error) {
if opts.Root != "./" {
opts.Root = strings.TrimRight(opts.Root, "/")
}
doc, err := goquery.NewDocumentFromReader(bytes.NewReader(html))
if err != nil {
return "", err
}
if err := minifyStyleTags(doc, opts); err != nil {
return "", fmt.Errorf("minifyStyleTags: %w", err)
}
if err := replaceCSSLinks(doc, opts); err != nil {
return "", fmt.Errorf("replaceCSSLinks: %w", err)
}
if err := replaceCSSImports(doc, opts); err != nil {
return "", fmt.Errorf("replaceCSSImports: %w", err)
}
if err := replaceJS(doc, opts); err != nil {
return "", fmt.Errorf("replaceJS: %w", err)
}
if err := replaceImg(doc, opts); err != nil {
return "", fmt.Errorf("replaceImg: %w", err)
}
h, err := doc.Html()
if err != nil {
return "", err
}
if opts.Minify.Has(HTML) {
return minifier.String("html", h)
}
return h, nil
}
func minifyStyleTags(doc *goquery.Document, opts Options) (err error) {
if !opts.Minify.Has(CSS) {
return nil
}
doc.Find(`style`).EachWithBreak(func(i int, s *goquery.Selection) bool {
var f string
f, err = minifier.String("css", s.Text())
if err != nil {
return false
}
s.SetText(f)
return true
})
return err
}
func replaceJS(doc *goquery.Document, opts Options) (err error) {
if !opts.Local.Has(JS) && !opts.Remote.Has(JS) {
return nil
}
var cont bool
doc.Find(`script`).EachWithBreak(func(i int, s *goquery.Selection) bool {
path, ok := s.Attr("src")
if !ok {
if !opts.Minify.Has(JS) {
return true
}
var f string
f, err = minifier.String("js", s.Text())
if err != nil {
return false
}
s.SetText(f)
return true
}
path = opts.Root + path
if isRemote(path) && !opts.Remote.Has(JS) {
return true
}
if !isRemote(path) && !opts.Local.Has(JS) {
return true
}
var f []byte
f, err = readPath(path)
cont, err = warn(opts, err)
if err != nil {
return false
}
if !cont {
return true
}
if opts.Minify.Has(JS) {
f, err = minifier.Bytes("js", f)
if err != nil {
return false
}
}
s.AfterHtml("<script>" + string(f) + "</script>")
s.Remove()
return true
})
return err
}
func replaceImg(doc *goquery.Document, opts Options) (err error) {
if !opts.Local.Has(Image) && !opts.Remote.Has(Image) {
return nil
}
var cont bool
doc.Find(`img`).EachWithBreak(func(i int, s *goquery.Selection) bool {
path, ok := s.Attr("src")
if !ok {
return true
}
path = opts.Root + path
if strings.HasPrefix(path, "data:") {
return true
}
if isRemote(path) && !opts.Remote.Has(Image) {
return true
}
if !isRemote(path) && !opts.Local.Has(Image) {
return true
}
var f []byte
f, err = readPath(path)
cont, err = warn(opts, err)
if err != nil {
return false
}
if !cont {
return true
}
m := mime.TypeByExtension(filepath.Ext(path))
if m == "" {
cont, err = warn(opts, &ParseError{Path: path, Err: errors.New("could not find MIME type")})
if err != nil {
return false
}
if !cont {
return true
}
}
s.SetAttr("src", fmt.Sprintf("data:%v;base64,%v",
m, base64.StdEncoding.EncodeToString(f)))
return true
})
return err
}