viteAsset will now generate a full html tag

This commit is contained in:
Shane C 2024-04-24 11:45:06 -04:00
parent 59be529601
commit 415f75d29f
No known key found for this signature in database
GPG key ID: 565F3A1C80F8AA9D
5 changed files with 56 additions and 6 deletions

2
go.mod
View file

@ -7,6 +7,8 @@ require (
github.com/spf13/viper v1.18.2
)
require golang.org/x/net v0.24.0 // indirect
require (
github.com/andybalholm/brotli v1.1.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect

2
go.sum
View file

@ -102,6 +102,8 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.3.0/go.mod h1:MBQ8lrhLObU/6UmLb4fmbmk5OcyYmqtbGd/9yIeKjEE=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=

View file

@ -5,7 +5,7 @@
{{ block "meta" . }}{{end}}
<title>{{ template "title" . }}</title>
{{ block "scripts" . }}{{end}}
<link rel="stylesheet" href="{{ viteAsset `/styles/app.css` }}">
{{ viteAsset "/styles/app.css" }}
</head>
<body>
<div class="bg-slate-600 w-full">

View file

@ -27,7 +27,7 @@
<div class="navbar-end">
<div tabindex="0" role="button" class="btn btn-ghost btn-circle avatar">
<div class="w-10 rounded-full">
<img alt="User Profile Image" src="{{ viteAsset `/assets/download.png` }}" />
{{ viteAsset "/assets/download.png" `alt="user profile image"` }}
</div>
</div>
</div>

View file

@ -1,13 +1,17 @@
package utils
import (
"fmt"
"github.com/goccy/go-json"
"html/template"
"log"
"mime"
"os"
"strings"
)
func ViteAsset(src string) string {
func ViteAsset(src string, attrs ...string) template.HTML {
file, err := os.ReadFile("dist/.vite/manifest.json")
if err != nil {
log.Fatalln(err)
@ -21,9 +25,51 @@ func ViteAsset(src string) string {
src = strings.TrimPrefix(src, "/")
if _, ok := viteManifestData[src]; ok {
return "/" + viteManifestData[src]["file"].(string)
} else {
var element string
info, ok := viteManifestData[src]
if !ok {
return ""
}
filenameSplit := strings.Split(info["file"].(string), ".")
if len(filenameSplit) < 2 {
return ""
}
fileExt := filenameSplit[len(filenameSplit)-1]
mimeType := mime.TypeByExtension("." + fileExt)
if strings.HasPrefix(mimeType, "image/") {
element = fmt.Sprintf(`<img src="%s"`, info["file"].(string))
for _, attr := range attrs {
element += fmt.Sprintf(` %s`, attr)
}
element += "></img>"
} else if strings.Contains(mimeType, "text/javascript") {
element = fmt.Sprintf(`<script type="module" src="%s"`, info["file"].(string))
for _, attr := range attrs {
element += fmt.Sprintf(` %s`, attr)
}
element += "></script>"
} else if strings.Contains(mimeType, "text/css") {
element = fmt.Sprintf(`<link rel="stylesheet" href="%s">`, info["file"].(string))
}
return template.HTML(element)
}