forked from go-catupiry/catu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
minifier.go
executable file
·70 lines (59 loc) · 1.82 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package bolo
import (
"bytes"
"regexp"
"github.com/labstack/echo/v4"
"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"
)
var m *minify.M
func setupMinifier(app App) {
m = minify.New()
if app.GetConfiguration().GetBoolF("MINIFY_HTML", true) {
m.AddFunc("text/html", html.Minify)
}
if app.GetConfiguration().GetBoolF("MINIFY_CSS", true) {
m.AddFunc("text/css", css.Minify)
}
if app.GetConfiguration().GetBoolF("MINIFY_IMAGE", true) {
m.AddFunc("image/svg+xml", svg.Minify)
}
// minify js is disabled by default because may fails on some new js expressions like a JSON directly inside a script as component:
if app.GetConfiguration().GetBoolF("MINIFY_JS", false) {
m.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), js.Minify)
}
if app.GetConfiguration().GetBoolF("MINIFY_JSON", true) {
m.AddFuncRegexp(regexp.MustCompile("[/+]json$"), json.Minify)
}
}
func MinifiHTML(templateName string, data interface{}, c *RequestContext) (string, error) {
html := new(bytes.Buffer)
err := c.App.RenderTemplate(html, templateName, data)
if err != nil {
return "", err
}
buf2 := new(bytes.Buffer)
if err := m.Minify("text/html", buf2, html); err != nil {
return "", err
}
return buf2.String(), nil
}
func MinifiAndRender(code int, name string, data interface{}, c *RequestContext) error {
var err error
if c.Echo().Renderer == nil {
return echo.ErrRendererNotRegistered
}
buf := new(bytes.Buffer)
if err = c.Echo().Renderer.Render(buf, name, data, c); err != nil {
return err
}
buf2 := new(bytes.Buffer)
if err := m.Minify("text/html", buf2, buf); err != nil {
panic(err)
}
return c.HTMLBlob(code, buf2.Bytes())
}