/
processor.go
58 lines (45 loc) · 1.12 KB
/
processor.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
package json
import (
"github.com/mateussouzaweb/compactor/compactor"
"github.com/mateussouzaweb/compactor/os"
"github.com/mateussouzaweb/compactor/pkg/generic"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/json"
)
// Minify json content
func Minify(content string) (string, error) {
m := minify.New()
m.AddFunc("generic", json.Minify)
content, err := m.String("generic", content)
return content, err
}
// Optimize processor
func Optimize(options *compactor.Options, file *compactor.File) error {
if !options.ShouldCompress(file.Path) {
return nil
}
content := file.Content
content, err := Minify(content)
if err != nil {
return err
}
destination := file.Destination
perm := file.Permission
err = os.Write(destination, content, perm)
if err != nil {
return err
}
return nil
}
// Plugin return the compactor plugin instance
func Plugin() *compactor.Plugin {
return &compactor.Plugin{
Namespace: "json",
Extensions: []string{".json"},
Init: generic.Init,
Resolve: generic.Resolve,
Related: generic.Related,
Transform: generic.Transform,
Optimize: Optimize,
}
}