|
@@ -3,6 +3,8 @@
|
3
|
3
|
// Use of this source code is governed by a BSD-style
|
4
|
4
|
// license, which can be found in the LICENSE file.
|
5
|
5
|
|
|
6
|
+// +build !clientonly
|
|
7
|
+
|
6
|
8
|
package isokit
|
7
|
9
|
|
8
|
10
|
import (
|
|
@@ -11,15 +13,14 @@ import (
|
11
|
13
|
"os"
|
12
|
14
|
"path/filepath"
|
13
|
15
|
"regexp"
|
|
16
|
+
|
|
17
|
+ "github.com/tdewolff/minify"
|
|
18
|
+ "github.com/tdewolff/minify/css"
|
|
19
|
+ "github.com/tdewolff/minify/js"
|
14
|
20
|
)
|
15
|
21
|
|
16
|
22
|
var StaticAssetsPath string
|
17
|
|
-var CogStaticAssetsSearchPaths []string
|
18
|
|
-
|
19
|
|
-func RegisterStaticAssetsSearchPath(path string) {
|
20
|
|
- //fmt.Println("cog search path: ", path)
|
21
|
|
- CogStaticAssetsSearchPaths = append(CogStaticAssetsSearchPaths, path)
|
22
|
|
-}
|
|
23
|
+var ShouldMinifyStaticAssets bool
|
23
|
24
|
|
24
|
25
|
func findStaticAssets(ext string, paths []string) []string {
|
25
|
26
|
|
|
@@ -41,7 +42,12 @@ func findStaticAssets(ext string, paths []string) []string {
|
41
|
42
|
return files
|
42
|
43
|
}
|
43
|
44
|
|
44
|
|
-func bundleJavaScript(jsfiles []string) {
|
|
45
|
+func bundleJavaScript(jsfiles []string, shouldMinify bool) {
|
|
46
|
+
|
|
47
|
+ outputFileName := "cogimports.js"
|
|
48
|
+ if shouldMinify == true {
|
|
49
|
+ outputFileName = "cogimports.min.js"
|
|
50
|
+ }
|
45
|
51
|
|
46
|
52
|
var result []byte = make([]byte, 0)
|
47
|
53
|
|
|
@@ -53,7 +59,7 @@ func bundleJavaScript(jsfiles []string) {
|
53
|
59
|
os.Mkdir(StaticAssetsPath+"/js", 0711)
|
54
|
60
|
}
|
55
|
61
|
|
56
|
|
- destinationFile := StaticAssetsPath + "/js/cogimports.js"
|
|
62
|
+ destinationFile := StaticAssetsPath + "/js/" + outputFileName
|
57
|
63
|
|
58
|
64
|
for i := 0; i < len(jsfiles); i++ {
|
59
|
65
|
b, err := ioutil.ReadFile(jsfiles[i])
|
|
@@ -63,14 +69,42 @@ func bundleJavaScript(jsfiles []string) {
|
63
|
69
|
result = append(result, b...)
|
64
|
70
|
}
|
65
|
71
|
|
66
|
|
- err := ioutil.WriteFile(destinationFile, result, 0644)
|
67
|
|
- if err != nil {
|
68
|
|
- log.Println(err)
|
|
72
|
+ if shouldMinify == true {
|
|
73
|
+
|
|
74
|
+ m := minify.New()
|
|
75
|
+ m.AddFunc("text/javascript", js.Minify)
|
|
76
|
+ b, err := m.Bytes("text/javascript", result)
|
|
77
|
+
|
|
78
|
+ if err != nil {
|
|
79
|
+ log.Println(err)
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ err = ioutil.WriteFile(destinationFile, b, 0644)
|
|
83
|
+
|
|
84
|
+ if err != nil {
|
|
85
|
+ log.Println(err)
|
|
86
|
+ }
|
|
87
|
+
|
|
88
|
+ } else {
|
|
89
|
+ err := ioutil.WriteFile(destinationFile, result, 0644)
|
|
90
|
+
|
|
91
|
+ if err != nil {
|
|
92
|
+ log.Println(err)
|
|
93
|
+ }
|
|
94
|
+
|
69
|
95
|
}
|
70
|
96
|
|
71
|
97
|
}
|
72
|
98
|
|
73
|
|
-func bundleCSS(cssfiles []string) {
|
|
99
|
+func bundleCSS(cssfiles []string, shouldMinify bool) {
|
|
100
|
+
|
|
101
|
+ return
|
|
102
|
+ outputFileName := "cogimports.css"
|
|
103
|
+ if shouldMinify == true {
|
|
104
|
+ outputFileName = "cogimports.min.css"
|
|
105
|
+ }
|
|
106
|
+
|
|
107
|
+ log.Println("output css name: "+outputFileName+" bool val: ", shouldMinify)
|
74
|
108
|
|
75
|
109
|
var result []byte = make([]byte, 0)
|
76
|
110
|
|
|
@@ -82,7 +116,7 @@ func bundleCSS(cssfiles []string) {
|
82
|
116
|
os.Mkdir(StaticAssetsPath+"/css", 0711)
|
83
|
117
|
}
|
84
|
118
|
|
85
|
|
- destinationFile := StaticAssetsPath + "/css/cogimports.css"
|
|
119
|
+ destinationFile := StaticAssetsPath + "/css/" + outputFileName
|
86
|
120
|
|
87
|
121
|
for i := 0; i < len(cssfiles); i++ {
|
88
|
122
|
b, err := ioutil.ReadFile(cssfiles[i])
|
|
@@ -92,9 +126,24 @@ func bundleCSS(cssfiles []string) {
|
92
|
126
|
result = append(result, b...)
|
93
|
127
|
}
|
94
|
128
|
|
95
|
|
- err := ioutil.WriteFile(destinationFile, result, 0644)
|
96
|
|
- if err != nil {
|
97
|
|
- log.Println(err)
|
|
129
|
+ if shouldMinify == true {
|
|
130
|
+
|
|
131
|
+ m := minify.New()
|
|
132
|
+ m.AddFunc("text/css", css.Minify)
|
|
133
|
+ b, err := m.Bytes("text/css", result)
|
|
134
|
+
|
|
135
|
+ if err != nil {
|
|
136
|
+ log.Println(err)
|
|
137
|
+ }
|
|
138
|
+
|
|
139
|
+ err = ioutil.WriteFile(destinationFile, b, 0644)
|
|
140
|
+
|
|
141
|
+ } else {
|
|
142
|
+ err := ioutil.WriteFile(destinationFile, result, 0644)
|
|
143
|
+ if err != nil {
|
|
144
|
+ log.Println(err)
|
|
145
|
+ }
|
|
146
|
+
|
98
|
147
|
}
|
99
|
148
|
|
100
|
149
|
}
|
|
@@ -106,9 +155,9 @@ func BundleStaticAssets() {
|
106
|
155
|
}
|
107
|
156
|
|
108
|
157
|
jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
|
109
|
|
- bundleJavaScript(jsfiles)
|
|
158
|
+ bundleJavaScript(jsfiles, ShouldMinifyStaticAssets)
|
110
|
159
|
cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
|
111
|
|
- bundleCSS(cssfiles)
|
|
160
|
+ bundleCSS(cssfiles, ShouldMinifyStaticAssets)
|
112
|
161
|
}
|
113
|
162
|
|
114
|
163
|
func init() {
|