93 lines
1.5 KiB
Go
93 lines
1.5 KiB
Go
package assets
|
|
|
|
import (
|
|
"embed"
|
|
"encoding/json"
|
|
|
|
"github.com/a-h/templ"
|
|
"github.com/labstack/echo/v4"
|
|
. "maragu.dev/gomponents"
|
|
. "maragu.dev/gomponents/html"
|
|
)
|
|
|
|
var (
|
|
//go:embed dist/*
|
|
statics embed.FS
|
|
manifest map[string]string
|
|
entrypoints map[string]map[string]map[string][]string
|
|
)
|
|
|
|
func Render(ctx echo.Context, statusCode int, t templ.Component) error {
|
|
buf := templ.GetBuffer()
|
|
defer templ.ReleaseBuffer(buf)
|
|
|
|
if err := t.Render(ctx.Request().Context(), buf); err != nil {
|
|
return err
|
|
}
|
|
|
|
return ctx.HTML(statusCode, buf.String())
|
|
}
|
|
|
|
func Asset(name string) string {
|
|
if manifest == nil {
|
|
value, _ := statics.ReadFile("static/manifest.json")
|
|
json.Unmarshal(value, &manifest)
|
|
}
|
|
|
|
path, ok := manifest[name]
|
|
|
|
if !ok {
|
|
return ""
|
|
}
|
|
|
|
return path
|
|
}
|
|
|
|
func entrypointFiles(app, category string) []string {
|
|
if entrypoints == nil {
|
|
value, _ := statics.ReadFile("static/entrypoints.json")
|
|
json.Unmarshal(value, &entrypoints)
|
|
}
|
|
|
|
entry, ok := entrypoints["entrypoints"][app]
|
|
|
|
if !ok {
|
|
return []string{}
|
|
}
|
|
|
|
files, ok := entry[category]
|
|
|
|
if !ok {
|
|
return []string{}
|
|
}
|
|
|
|
return files
|
|
}
|
|
|
|
func EntrypointJs(app string) []Node {
|
|
files := entrypointFiles(app, "js")
|
|
var results []Node
|
|
|
|
for _, file := range files {
|
|
results = append(
|
|
results,
|
|
Script(Src(file)),
|
|
)
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
func EntrypointCss(app string) []Node {
|
|
files := entrypointFiles(app, "css")
|
|
var results []Node
|
|
|
|
for _, file := range files {
|
|
results = append(
|
|
results,
|
|
Link(Rel("stylesheet"), Href(file)),
|
|
)
|
|
}
|
|
|
|
return results
|
|
}
|