53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
|
package components
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"github.com/a-h/templ"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
//type ScriptAssetOptions struct {
|
||
|
// IsAsync bool
|
||
|
// IsDefer bool
|
||
|
// IsModule bool
|
||
|
// DoImport bool
|
||
|
//}
|
||
|
|
||
|
type NewScriptAssetOptions struct {
|
||
|
IsAsync bool
|
||
|
IsDefer bool
|
||
|
IsModule bool
|
||
|
FuncImports []string
|
||
|
}
|
||
|
|
||
|
func LoadJS(assetName string, opts *NewScriptAssetOptions) templ.Component {
|
||
|
return templ.ComponentFunc(func(ctx context.Context, w io.Writer) error {
|
||
|
if opts == nil {
|
||
|
_, err := io.WriteString(w, `<script type="module" src="/assets/js/`+assetName+`"></script>`)
|
||
|
return err
|
||
|
} else {
|
||
|
if opts.IsModule {
|
||
|
str := `<script type="module" src="/assets/js/` + assetName + `"`
|
||
|
if opts.IsDefer {
|
||
|
str += ` defer`
|
||
|
}
|
||
|
if opts.IsAsync {
|
||
|
str += ` async`
|
||
|
}
|
||
|
_, err := io.WriteString(w, str+`></script>`)
|
||
|
return err
|
||
|
} else {
|
||
|
str := `<script src="/assets/js/` + assetName + `"`
|
||
|
if opts.IsDefer {
|
||
|
str += ` defer`
|
||
|
}
|
||
|
if opts.IsAsync {
|
||
|
str += ` async`
|
||
|
}
|
||
|
_, err := io.WriteString(w, str+`></script>`)
|
||
|
return err
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
}
|