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
|
//go:build generate
package main
import (
"bytes"
_ "embed"
"fmt"
"go/format"
"os"
"os/exec"
)
var tailwindCSS = `
@tailwind base;
@tailwind components;
@tailwind utilities;
`
//go:generate templ generate
//go:generate go run generate.go
func main() {
if err := tailwind(); err != nil {
panic(err)
}
}
// Generate tailwind code from templ templates and combine with other misc CSS
func tailwind() error {
fmt.Println("generating tailwind...")
tmp, err := os.CreateTemp(os.TempDir(), "cfg-tailwind*")
if err != nil {
return err
}
defer os.Remove(tmp.Name())
if _, err := tmp.WriteString(tailwindCSS); err != nil {
return err
}
tmp.Close()
styles, err := os.Create("tailwind.go")
if err != nil {
return err
}
defer styles.Close()
var buf bytes.Buffer
cmd := exec.Command("tailwind-ctp", "-i", tmp.Name(), "--minify")
cmd.Stdout = &buf
if err := cmd.Run(); err != nil {
return err
}
code := fmt.Sprintf(`// Code generated by generate.go - DO NOT EDIT.
package static
import "net/http"
func TailwindHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/css")
w.Write([]byte(%q))
}`, buf.String())
formatted, err := format.Source([]byte(code))
if err != nil {
return err
}
styles.Write(formatted)
fmt.Println("finished generating tailwind")
return nil
}
|