Browse Source

Added functionality to minify static assets. Added clientonly build tag.

Wirecog, LLC 3 years ago
parent
commit
58577102cf
8 changed files with 159 additions and 28 deletions
  1. 2
    10
      form.go
  2. 39
    0
      formjs.go
  3. 22
    0
      formparams.go
  4. 18
    0
      formparamsjs.go
  5. 2
    0
      gopherjshandlers.go
  6. 2
    0
      redirect.go
  7. 67
    18
      static.go
  8. 7
    0
      templateset.go

+ 2
- 10
form.go View File

@@ -3,11 +3,11 @@
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 (
9
-	"net/http"
10
-
11 11
 	"honnef.co/go/js/dom"
12 12
 )
13 13
 
@@ -19,14 +19,6 @@ type Form interface {
19 19
 	//	SetError(key string, message string)
20 20
 }
21 21
 
22
-type FormParams struct {
23
-	ResponseWriter             http.ResponseWriter
24
-	Request                    *http.Request
25
-	FormElement                *dom.HTMLFormElement
26
-	UseFormFieldsForValidation bool
27
-	FormFields                 map[string]string
28
-}
29
-
30 22
 func FormValue(fp *FormParams, key string) string {
31 23
 
32 24
 	var result string

+ 39
- 0
formjs.go View File

@@ -0,0 +1,39 @@
1
+// The Isomorphic Go Project
2
+// Copyright (c) Wirecog, LLC. All rights reserved.
3
+// Use of this source code is governed by a BSD-style
4
+// license, which can be found in the LICENSE file.
5
+
6
+// +build clientonly
7
+
8
+package isokit
9
+
10
+import (
11
+	"honnef.co/go/js/dom"
12
+)
13
+
14
+type Form interface {
15
+	Validate() bool
16
+	AutofillFields() []string
17
+	Fields() map[string]string
18
+	Errors() map[string]string
19
+	//	SetError(key string, message string)
20
+}
21
+
22
+func FormValue(fp *FormParams, key string) string {
23
+
24
+	var result string
25
+
26
+	field := fp.FormElement.QuerySelector("[name=" + key + "]")
27
+
28
+	switch field.(type) {
29
+	case *dom.HTMLInputElement:
30
+		result = field.(*dom.HTMLInputElement).Value
31
+	case *dom.HTMLTextAreaElement:
32
+		result = field.(*dom.HTMLTextAreaElement).Value
33
+	case *dom.HTMLSelectElement:
34
+		result = field.(*dom.HTMLSelectElement).Value
35
+
36
+	}
37
+
38
+	return result
39
+}

+ 22
- 0
formparams.go View File

@@ -0,0 +1,22 @@
1
+// The Isomorphic Go Project
2
+// Copyright (c) Wirecog, LLC. All rights reserved.
3
+// Use of this source code is governed by a BSD-style
4
+// license, which can be found in the LICENSE file.
5
+
6
+// +build !clientonly
7
+
8
+package isokit
9
+
10
+import (
11
+	"net/http"
12
+
13
+	"honnef.co/go/js/dom"
14
+)
15
+
16
+type FormParams struct {
17
+	FormElement                *dom.HTMLFormElement
18
+	ResponseWriter             http.ResponseWriter
19
+	Request                    *http.Request
20
+	UseFormFieldsForValidation bool
21
+	FormFields                 map[string]string
22
+}

+ 18
- 0
formparamsjs.go View File

@@ -0,0 +1,18 @@
1
+// The Isomorphic Go Project
2
+// Copyright (c) Wirecog, LLC. All rights reserved.
3
+// Use of this source code is governed by a BSD-style
4
+// license, which can be found in the LICENSE file.
5
+
6
+// +build clientonly
7
+
8
+package isokit
9
+
10
+import (
11
+	"honnef.co/go/js/dom"
12
+)
13
+
14
+type FormParams struct {
15
+	FormElement                *dom.HTMLFormElement
16
+	UseFormFieldsForValidation bool
17
+	FormFields                 map[string]string
18
+}

+ 2
- 0
gopherjshandlers.go View File

@@ -1,3 +1,5 @@
1
+// +build !clientonly
2
+
1 3
 package isokit
2 4
 
3 5
 import (

+ 2
- 0
redirect.go View File

@@ -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 (

+ 67
- 18
static.go View File

@@ -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() {

+ 7
- 0
templateset.go View File

@@ -17,6 +17,8 @@ import (
17 17
 	"strings"
18 18
 )
19 19
 
20
+var CogStaticAssetsSearchPaths []string
21
+
20 22
 type TemplateSet struct {
21 23
 	members           map[string]*Template
22 24
 	Funcs             template.FuncMap
@@ -234,3 +236,8 @@ func StaticTemplateBundleFileExists() bool {
234 236
 	}
235 237
 
236 238
 }
239
+
240
+func RegisterStaticAssetsSearchPath(path string) {
241
+	//fmt.Println("cog search path: ", path)
242
+	CogStaticAssetsSearchPaths = append(CogStaticAssetsSearchPaths, path)
243
+}