Common isomorphic functionality in one kit.

static.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // +build !clientonly
  6. package isokit
  7. import (
  8. "io/ioutil"
  9. "log"
  10. "os"
  11. "path/filepath"
  12. "regexp"
  13. "github.com/tdewolff/minify"
  14. "github.com/tdewolff/minify/css"
  15. "github.com/tdewolff/minify/js"
  16. )
  17. var StaticAssetsPath string
  18. var ShouldMinifyStaticAssets bool
  19. func findStaticAssets(ext string, paths []string) []string {
  20. var files []string
  21. for i := 0; i < len(paths); i++ {
  22. //fmt.Println("file search path: ", paths[i])
  23. filepath.Walk(paths[i], func(path string, f os.FileInfo, _ error) error {
  24. if !f.IsDir() {
  25. r, err := regexp.MatchString(ext, f.Name())
  26. if err == nil && r {
  27. files = append(files, path)
  28. }
  29. }
  30. return nil
  31. })
  32. }
  33. return files
  34. }
  35. func bundleJavaScript(jsfiles []string, shouldMinify bool) {
  36. outputFileName := "cogimports.js"
  37. if shouldMinify == true {
  38. outputFileName = "cogimports.min.js"
  39. }
  40. var result []byte = make([]byte, 0)
  41. if StaticAssetsPath == "" {
  42. return
  43. }
  44. if _, err := os.Stat(StaticAssetsPath + "/js"); os.IsNotExist(err) {
  45. os.Mkdir(StaticAssetsPath+"/js", 0711)
  46. }
  47. destinationFile := StaticAssetsPath + "/js/" + outputFileName
  48. for i := 0; i < len(jsfiles); i++ {
  49. b, err := ioutil.ReadFile(jsfiles[i])
  50. if err != nil {
  51. log.Println(err)
  52. }
  53. result = append(result, b...)
  54. }
  55. if shouldMinify == true {
  56. m := minify.New()
  57. m.AddFunc("text/javascript", js.Minify)
  58. b, err := m.Bytes("text/javascript", result)
  59. if err != nil {
  60. log.Println(err)
  61. }
  62. err = ioutil.WriteFile(destinationFile, b, 0644)
  63. if err != nil {
  64. log.Println(err)
  65. }
  66. } else {
  67. err := ioutil.WriteFile(destinationFile, result, 0644)
  68. if err != nil {
  69. log.Println(err)
  70. }
  71. }
  72. }
  73. func bundleCSS(cssfiles []string, shouldMinify bool) {
  74. return
  75. outputFileName := "cogimports.css"
  76. if shouldMinify == true {
  77. outputFileName = "cogimports.min.css"
  78. }
  79. log.Println("output css name: "+outputFileName+" bool val: ", shouldMinify)
  80. var result []byte = make([]byte, 0)
  81. if StaticAssetsPath == "" {
  82. return
  83. }
  84. if _, err := os.Stat(StaticAssetsPath + "/css"); os.IsNotExist(err) {
  85. os.Mkdir(StaticAssetsPath+"/css", 0711)
  86. }
  87. destinationFile := StaticAssetsPath + "/css/" + outputFileName
  88. for i := 0; i < len(cssfiles); i++ {
  89. b, err := ioutil.ReadFile(cssfiles[i])
  90. if err != nil {
  91. log.Println(err)
  92. }
  93. result = append(result, b...)
  94. }
  95. if shouldMinify == true {
  96. m := minify.New()
  97. m.AddFunc("text/css", css.Minify)
  98. b, err := m.Bytes("text/css", result)
  99. if err != nil {
  100. log.Println(err)
  101. }
  102. err = ioutil.WriteFile(destinationFile, b, 0644)
  103. } else {
  104. err := ioutil.WriteFile(destinationFile, result, 0644)
  105. if err != nil {
  106. log.Println(err)
  107. }
  108. }
  109. }
  110. func BundleStaticAssets() {
  111. if ShouldBundleStaticAssets == false {
  112. return
  113. }
  114. jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
  115. bundleJavaScript(jsfiles, ShouldMinifyStaticAssets)
  116. cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
  117. bundleCSS(cssfiles, ShouldMinifyStaticAssets)
  118. }
  119. func init() {
  120. CogStaticAssetsSearchPaths = make([]string, 0)
  121. }