Documentation
¶
Overview ¶
Package vite renders the tags that load a Vite frontend, following the Laravel "hot file" model.
In development the starter's Vite plugin writes public/hot with the dev server URL; Tags then points the browser at that server. Otherwise Tags reads public/build/.vite/manifest.json and emits modulepreload, stylesheet and script tags for each entry and its recursive imports, deduplicated across entries. Every tag gets the CSP nonce, and the manifest's "integrity" values become SRI attributes.
The app builds one Vite with New from the public directory; views reach it through the "vite" template function (package view), Inertia uses Vite.Version as its asset version, and Vite.Asset resolves files Vite processed. Every starter except the API one ships a Vite setup.
See https://github.com/AlexGalhardo/vukem/blob/main/docs/frontend.md.
Index ¶
- func NewContext(ctx context.Context, v *Vite) context.Context
- type Config
- type Vite
- func (v *Vite) Asset(p string) (string, bool)
- func (v *Vite) HotURL() string
- func (v *Vite) Middleware() func(http.Handler) http.Handler
- func (v *Vite) ReactRefresh(nonce string) template.HTML
- func (v *Vite) Tags(nonce string, entries ...string) (template.HTML, error)
- func (v *Vite) Version() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct {
// FS is the public directory (os.DirFS("public") or an embed.FS) holding
// the "hot" file and the build directory.
FS fs.FS
// Dev honours the hot file and re-reads the manifest on every call. Off
// (production), a stray hot file is ignored and the manifest is cached.
Dev bool
// BuildDir is the build directory inside FS and the URL prefix of built
// files (default "build").
BuildDir string
}
Config configures a Vite.
type Vite ¶
type Vite struct {
// contains filtered or unexported fields
}
Vite renders Vite tags; it is safe for concurrent use.
func FromContext ¶ added in v0.19.0
FromContext returns the Vite stored in ctx, or nil.
func New ¶
New returns a Vite. The manifest is read on first use, so an app can boot before the first build while the dev server runs.
func (*Vite) Asset ¶
Asset returns the URL of a file Vite processed ("resources/img/logo.png"), and false when neither the dev server nor the manifest knows it. While the dev server runs, a file that exists in the public directory (a vendored "js/htmx.min.js") also returns false: the app serves it, the dev server (publicDir off in the starter plugin) does not.
func (*Vite) HotURL ¶
HotURL returns the dev server URL while it runs (Dev on and a valid hot file), and "" otherwise; inertia.SSRConfig.HotURL uses it.
func (*Vite) Middleware ¶ added in v0.19.0
Middleware stores v in every request context (see FromContext), so templ components can call FromContext(ctx).Tags.
func (*Vite) ReactRefresh ¶
ReactRefresh returns the @vitejs/plugin-react preamble while the dev server runs, and nothing otherwise. Place it before Tags.
func (*Vite) Tags ¶
Tags returns the HTML that loads entries (manifest keys such as "resources/js/app.ts"). nonce is the request's CSP nonce ("" for none).
Example ¶
package main
import (
"fmt"
"testing/fstest"
"github.com/AlexGalhardo/vukem/vite"
)
func main() {
// Apps pass os.DirFS("public"); `vite build` writes the manifest there.
public := fstest.MapFS{
"build/.vite/manifest.json": {Data: []byte(`{
"resources/js/app.ts": {"file": "assets/app-1.js", "src": "resources/js/app.ts", "isEntry": true}
}`)},
}
v, err := vite.New(vite.Config{FS: public})
if err != nil {
fmt.Println(err)
return
}
// Views call {{vite "resources/js/app.ts"}}, which passes the CSP nonce.
tags, _ := v.Tags("n0nce", "resources/js/app.ts")
fmt.Println(tags)
}
Output: <script type="module" src="/build/assets/app-1.js" nonce="n0nce"></script>
Example (Dev) ¶
package main
import (
"fmt"
"testing/fstest"
"github.com/AlexGalhardo/vukem/vite"
)
func main() {
// In development the Vite plugin writes public/hot with the server URL.
public := fstest.MapFS{"hot": {Data: []byte("http://localhost:5173")}}
v, _ := vite.New(vite.Config{FS: public, Dev: true})
tags, _ := v.Tags("n0nce", "resources/js/app.ts")
fmt.Println(v.HotURL())
fmt.Println(tags)
}
Output: http://localhost:5173 <script type="module" src="http://localhost:5173/@vite/client" nonce="n0nce"></script> <script type="module" src="http://localhost:5173/resources/js/app.ts" nonce="n0nce"></script>