This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
167 lines (138 loc) · 4.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/fatih/color"
"github.com/spf13/pflag"
"github.com/gascore/gasx"
"github.com/gascore/gasx/acss"
"github.com/gascore/gasx/html"
)
func main() {
var (
isDev = pflag.BoolP("dev", "d", true, "Development mode")
platform = pflag.StringP("platform", "p", "wasm", "Target platform")
watch = pflag.BoolP("watch", "w", false, "Watch for file changings")
serveDist = pflag.BoolP("serve", "s", true, "Serve ./dist directory")
)
pflag.Parse()
build(*platform, *isDev)
currentDir, err := os.Getwd()
gasx.Must(err)
if *watch {
go serve(currentDir)
gasx.Must(gasx.StartWatcher(
func(path string) {
fmt.Println(color.GreenString("Triggered:"), strings.TrimPrefix(path, currentDir+"/app/"))
build(*platform, *isDev)
},
[]string{"_gas.go"},
[]string{},
[]string{"app/"},
))
}
if *serveDist {
serve(currentDir)
}
}
func build(platform string, isDev bool) {
htmlCompiler := html.NewCompiler()
acssGen := acss.Generator{
BreakPoints: map[string]string{
"lg": "@media(min-width:1200px)",
"md": "@media(min-width:1000px)",
"sm": "@media(min-width:750px)",
},
Custom: map[string]string{
"b": "1px solid #dedede",
// burn.css variables
"color-primary": "var(--color-primary)",
"color-second-primary": "var(--color-second-primary)",
"color-lightGrey": "var(--color-lightGrey)",
"color-grey": "var(--color-grey)",
"color-darkGrey": "var(--color-darkGrey)",
"color-error": "var(--color-error)",
"color-success": "var(--color-success)",
"main-bg": "var(--main-bg)",
"grid-maxWidth": "var(--grid-maxWidth)",
"grid-gutter": "var(--grid-gutter)",
"font-size": "var(--font-size)",
"-main-color": "var(--main-color)",
},
}
acssGen.Init()
htmlCompiler.AddOnElementInfo(acssGen.OnElementInfo())
builder := &gasx.Builder{
BlockCompilers: []gasx.BlockCompiler{
htmlCompiler.Block(),
},
}
compileFiles(builder)
gasx.ClearDir("dist")
gasx.CopyDir("app/static", "dist")
chooseIndexHTML(isDev)
compileCode(builder, platform, isDev)
compileStyles(builder, acssGen, isDev)
gasx.Log("Building finished")
}
func compileFiles(builder *gasx.Builder) {
gasx.Log("Compiling *.gox, *.gos")
gasx.RunCommand("chmod 777 -R $GOPATH/pkg/mod;")
files, err := gasx.GasFiles([]string{"gos"}) // get files
gasx.Must(err)
gasx.Must(builder.ParseFiles(files)) // compile them
}
func chooseIndexHTML(isDev bool) {
var choose string
if isDev {
choose = "index.dev.html"
} else {
choose = "index.prod.html"
}
gasx.CopyFile("dist/"+choose, "dist/index.html")
if !isDev {
gasx.DeleteFile("dist/index.dev.html")
gasx.DeleteFile("dist/index.prod.html")
}
}
func compileStyles(builder *gasx.Builder, acssGen acss.Generator, isDev bool) {
gasx.Log("Compiling css")
depsPaths, err := gasx.GrepStyles()
depsFile, err := gasx.UniteFilesByPaths(depsPaths)
gasx.Must(err)
gasx.NewFile("dist/deps.css", depsFile)
gasx.NewFile("dist/acss.css", acssGen.GetStyles()) // save css styles generated by acss
var isProd string
if !isDev {
isProd = "--env production "
}
gasx.RunCommand("postcss " + isProd + "app/styles/main.pcss -o dist/main.css")
}
func compileCode(builder *gasx.Builder, platform string, isDev bool) {
gasx.Log("Compiling code")
switch platform {
case "gopherjs":
gasx.RunCommand(fmt.Sprintf("cd app; gopherjs build -m -o ../dist/index.js; cd .."))
if !isDev {
gasx.RunCommand("uglifyjs dist/index.js -o dist/index.m.js")
}
case "wasm":
gasx.RunCommand(fmt.Sprintf("cd app; GOOS=js GOARCH=wasm go build -o ../dist/main.wasm; cd .."))
indexJSFile := "index.js"
if !isDev {
indexJSFile = "index.m.js"
}
gasx.NewFile("dist/"+indexJSFile, gasx.GetWASMExecScript())
case "tinygo":
gasx.ErrorMsg("comming soon(?)")
default:
gasx.ErrorMsg("invalid target platform")
}
}
func serve(currentDir string) {
gasx.Log("Starting static server")
currentSrv := &http.Server{Addr: ":8080", Handler: http.FileServer(http.Dir(currentDir + "/dist"))}
currentSrv.ListenAndServe()
}