-
Notifications
You must be signed in to change notification settings - Fork 37
/
minifier.go
52 lines (44 loc) · 1.23 KB
/
minifier.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
package air
import (
"errors"
"sync"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
"github.com/tdewolff/minify/v2/html"
"github.com/tdewolff/minify/v2/js"
"github.com/tdewolff/minify/v2/json"
"github.com/tdewolff/minify/v2/svg"
"github.com/tdewolff/minify/v2/xml"
)
// minifier is a minifier that minifies contents based on the MIME types.
type minifier struct {
a *Air
loadOnce sync.Once
minifier *minify.M
}
// newMinifier returns a new instance of the `minifier` with the a.
func newMinifier(a *Air) *minifier {
return &minifier{
a: a,
}
}
// load loads the stuff of the m up.
func (m *minifier) load() {
m.minifier = minify.New()
m.minifier.Add("text/html", &html.Minifier{})
m.minifier.Add("text/css", &css.Minifier{})
m.minifier.Add("application/javascript", &js.Minifier{})
m.minifier.Add("application/json", &json.Minifier{})
m.minifier.Add("application/xml", &xml.Minifier{})
m.minifier.Add("image/svg+xml", &svg.Minifier{})
}
// minify minifies the b based on the mimeType.
func (m *minifier) minify(mimeType string, b []byte) ([]byte, error) {
m.loadOnce.Do(m.load)
mb, err := m.minifier.Bytes(mimeType, b)
if errors.Is(err, minify.ErrNotExist) {
mb = b
err = nil
}
return mb, err
}