-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
55 lines (45 loc) · 1.42 KB
/
main.go
File metadata and controls
55 lines (45 loc) · 1.42 KB
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
package main
import (
"fmt"
"regexp"
"syscall/js"
"github.com/tdewolff/minify/v2"
_css "github.com/tdewolff/minify/v2/css"
_html "github.com/tdewolff/minify/v2/html"
_js "github.com/tdewolff/minify/v2/js"
_json "github.com/tdewolff/minify/v2/json"
)
var min = minify.New()
func main() {
done := make(chan struct{}, 0)
jsGlobal := js.Global()
jsGlobal.Set("wasmMinifyJs", js.FuncOf(minifyJs))
jsGlobal.Set("wasmMinifyCss", js.FuncOf(minifyCss))
jsGlobal.Set("wasmMinifyHtml", js.FuncOf(minifyHtml))
jsGlobal.Set("wasmMinifyJson", js.FuncOf(minifyJson))
<-done
}
func minifyJs(this js.Value, args []js.Value) interface{} {
min.AddFuncRegexp(regexp.MustCompile("^(application|text)/(x-)?(java|ecma)script$"), _js.Minify)
return getStr("text/javascript", args[0].String())
}
func minifyCss(this js.Value, args []js.Value) interface{} {
min.AddFunc("text/css", _css.Minify)
return getStr("text/css", args[0].String())
}
func minifyHtml(this js.Value, args []js.Value) interface{} {
min.AddFunc("text/html", _html.Minify)
return getStr("text/html", args[0].String())
}
func minifyJson(this js.Value, args []js.Value) interface{} {
min.AddFuncRegexp(regexp.MustCompile("[/+]json$"), _json.Minify)
return getStr("application/json", args[0].String())
}
func getStr(mediaType string, code string) string {
str, err := min.String(mediaType, code)
if err != nil {
return fmt.Sprintf("error: %v", err)
} else {
return str;
}
}