Browse Source

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

Wirecog 6 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
 // Use of this source code is governed by a BSD-style
3
 // Use of this source code is governed by a BSD-style
4
 // license, which can be found in the LICENSE file.
4
 // license, which can be found in the LICENSE file.
5
 
5
 
6
+// +build !clientonly
7
+
6
 package isokit
8
 package isokit
7
 
9
 
8
 import (
10
 import (
9
-	"net/http"
10
-
11
 	"honnef.co/go/js/dom"
11
 	"honnef.co/go/js/dom"
12
 )
12
 )
13
 
13
 
19
 	//	SetError(key string, message string)
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
 func FormValue(fp *FormParams, key string) string {
22
 func FormValue(fp *FormParams, key string) string {
31
 
23
 
32
 	var result string
24
 	var result string

+ 39
- 0
formjs.go View File

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

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

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
+// +build !clientonly
2
+
1
 package isokit
3
 package isokit
2
 
4
 
3
 import (
5
 import (

+ 2
- 0
redirect.go View File

3
 // Use of this source code is governed by a BSD-style
3
 // Use of this source code is governed by a BSD-style
4
 // license, which can be found in the LICENSE file.
4
 // license, which can be found in the LICENSE file.
5
 
5
 
6
+// +build !clientonly
7
+
6
 package isokit
8
 package isokit
7
 
9
 
8
 import (
10
 import (

+ 67
- 18
static.go View File

3
 // Use of this source code is governed by a BSD-style
3
 // Use of this source code is governed by a BSD-style
4
 // license, which can be found in the LICENSE file.
4
 // license, which can be found in the LICENSE file.
5
 
5
 
6
+// +build !clientonly
7
+
6
 package isokit
8
 package isokit
7
 
9
 
8
 import (
10
 import (
11
 	"os"
13
 	"os"
12
 	"path/filepath"
14
 	"path/filepath"
13
 	"regexp"
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
 var StaticAssetsPath string
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
 func findStaticAssets(ext string, paths []string) []string {
25
 func findStaticAssets(ext string, paths []string) []string {
25
 
26
 
41
 	return files
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
 	var result []byte = make([]byte, 0)
52
 	var result []byte = make([]byte, 0)
47
 
53
 
53
 		os.Mkdir(StaticAssetsPath+"/js", 0711)
59
 		os.Mkdir(StaticAssetsPath+"/js", 0711)
54
 	}
60
 	}
55
 
61
 
56
-	destinationFile := StaticAssetsPath + "/js/cogimports.js"
62
+	destinationFile := StaticAssetsPath + "/js/" + outputFileName
57
 
63
 
58
 	for i := 0; i < len(jsfiles); i++ {
64
 	for i := 0; i < len(jsfiles); i++ {
59
 		b, err := ioutil.ReadFile(jsfiles[i])
65
 		b, err := ioutil.ReadFile(jsfiles[i])
63
 		result = append(result, b...)
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
 	var result []byte = make([]byte, 0)
109
 	var result []byte = make([]byte, 0)
76
 
110
 
82
 		os.Mkdir(StaticAssetsPath+"/css", 0711)
116
 		os.Mkdir(StaticAssetsPath+"/css", 0711)
83
 	}
117
 	}
84
 
118
 
85
-	destinationFile := StaticAssetsPath + "/css/cogimports.css"
119
+	destinationFile := StaticAssetsPath + "/css/" + outputFileName
86
 
120
 
87
 	for i := 0; i < len(cssfiles); i++ {
121
 	for i := 0; i < len(cssfiles); i++ {
88
 		b, err := ioutil.ReadFile(cssfiles[i])
122
 		b, err := ioutil.ReadFile(cssfiles[i])
92
 		result = append(result, b...)
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
 	}
155
 	}
107
 
156
 
108
 	jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
157
 	jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
109
-	bundleJavaScript(jsfiles)
158
+	bundleJavaScript(jsfiles, ShouldMinifyStaticAssets)
110
 	cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
159
 	cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
111
-	bundleCSS(cssfiles)
160
+	bundleCSS(cssfiles, ShouldMinifyStaticAssets)
112
 }
161
 }
113
 
162
 
114
 func init() {
163
 func init() {

+ 7
- 0
templateset.go View File

17
 	"strings"
17
 	"strings"
18
 )
18
 )
19
 
19
 
20
+var CogStaticAssetsSearchPaths []string
21
+
20
 type TemplateSet struct {
22
 type TemplateSet struct {
21
 	members           map[string]*Template
23
 	members           map[string]*Template
22
 	Funcs             template.FuncMap
24
 	Funcs             template.FuncMap
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
+}