-
Notifications
You must be signed in to change notification settings - Fork 0
/
min.go
51 lines (43 loc) · 1.28 KB
/
min.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
package min
// github.com/tdewolff/minify binding for glub.
// No Configuration required.
import (
"sync"
"bytes"
"log"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
"github.com/omeid/slurp"
)
func Min(c *slurp.C, mediatype string) slurp.Stage {
return func(in <-chan slurp.File, out chan<- slurp.File) {
supported := make(map[string]uint8)
supported["text/css"] = 1
supported["text/html"] = 1
supported["text/javascript"] = 1
var wg sync.WaitGroup
defer wg.Wait()
_, ok := supported[mediatype]
if ok {
for file := range in {
m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
m.AddFunc("text/javascript", js.Minify)
wg.Add(1)
go func(file slurp.File) {
defer wg.Done()
minified := new(bytes.Buffer)
if err := m.Minify(mediatype, minified, file.Reader); err != nil {
log.Fatal("js.Minify:", err)
}
file.Reader = minified
file.FileInfo.SetSize(int64(minified.Len()))
out <- file
}(file)
}
}
}
}