Common isomorphic functionality in one kit.

static.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(filepath.Join(StaticAssetsPath, "js")); os.IsNotExist(err) {
  45. os.Mkdir(filepath.Join(StaticAssetsPath, "js"), 0711)
  46. }
  47. destinationFile := filepath.Join(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. outputFileName := "cogimports.css"
  75. if shouldMinify == true {
  76. outputFileName = "cogimports.min.css"
  77. }
  78. var result []byte = make([]byte, 0)
  79. if StaticAssetsPath == "" {
  80. return
  81. }
  82. if _, err := os.Stat(filepath.Join(StaticAssetsPath, "css")); os.IsNotExist(err) {
  83. os.Mkdir(filepath.Join(StaticAssetsPath, "css"), 0711)
  84. }
  85. destinationFile := filepath.Join(StaticAssetsPath, "css", outputFileName)
  86. for i := 0; i < len(cssfiles); i++ {
  87. b, err := ioutil.ReadFile(cssfiles[i])
  88. if err != nil {
  89. log.Println(err)
  90. }
  91. result = append(result, b...)
  92. }
  93. if shouldMinify == true {
  94. m := minify.New()
  95. m.AddFunc("text/css", css.Minify)
  96. b, err := m.Bytes("text/css", result)
  97. if err != nil {
  98. log.Println(err)
  99. }
  100. err = ioutil.WriteFile(destinationFile, b, 0644)
  101. } else {
  102. err := ioutil.WriteFile(destinationFile, result, 0644)
  103. if err != nil {
  104. log.Println(err)
  105. }
  106. }
  107. }
  108. func BundleStaticAssets() {
  109. if ShouldBundleStaticAssets == false {
  110. return
  111. }
  112. jsfiles := findStaticAssets(".js", CogStaticAssetsSearchPaths)
  113. bundleJavaScript(jsfiles, ShouldMinifyStaticAssets)
  114. cssfiles := findStaticAssets(".css", CogStaticAssetsSearchPaths)
  115. bundleCSS(cssfiles, ShouldMinifyStaticAssets)
  116. }
  117. func init() {
  118. CogStaticAssetsSearchPaths = make([]string, 0)
  119. }