/
minify.go
47 lines (38 loc) · 1 KB
/
minify.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
package compile
import (
"os"
"regexp"
"github.com/tdewolff/minify"
)
// Interfacing with github.com/tdewolff/minify.
var _ Refiner = &RefineMinify{}
var reMinPass = regexp.MustCompile(`(?i)\.min\.[^\.]+$`)
// var minifier = minify.New()
// RefineMinify interfaces with the popular Minifier library.
type RefineMinify struct {
passthrough
MatchPath *regexp.Regexp
Minifier minify.MinifierFunc
}
// Match returns true if pattern fits the file path.
func (rf *RefineMinify) Match(p string) bool {
if reMinPass.MatchString(p) {
return false // skip already minified assets
}
return rf.MatchPath.MatchString(p)
}
// Refine applies a minifier to source and writes the result to destination.
func (rf *RefineMinify) Refine(destination, source string) error {
// log.Println(destination, source)
w, err := os.Create(destination)
if err != nil {
return err
}
defer w.Close()
r, err := os.Open(source)
if err != nil {
return err
}
defer r.Close()
return rf.Minifier.Minify(minify.New(), w, r, nil)
}