templ

package
v2.17.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package templ provides a content-hash cache for .templ files so that `templ generate` is only invoked for files whose contents (or generated counterparts) have changed since the last run.

Cache layout: a flat {<relative-path>: sha256-hex} JSON map persisted at templCachePath. The format mirrors pkg/helpers/wasm/wasm_cache.go so both caches behave consistently and remain easy to inspect by hand.

Index

Constants

View Source
const TemplCachePath = ".gothicCli/templ-cache.json"

TemplCachePath is the on-disk location of the templ hash cache. Exported so callers (and tests) can override behavior if needed.

Variables

This section is empty.

Functions

func DirtyFiles

func DirtyFiles(c *Cache, files []string) []string

DirtyFiles returns the subset of files whose hash differs from the cache or whose generated _templ.go counterpart is missing. Files that hash to "" (unreadable) are also returned so the caller surfaces the error via templ.

func HashFile

func HashFile(path string) string

HashFile computes the SHA-256 hex digest of a file's contents. Returns "" if the file cannot be read so callers can treat the file as dirty.

func ScanTemplFiles

func ScanTemplFiles(root string) ([]string, error)

ScanTemplFiles walks root and returns the relative paths of every .templ file found. Hidden directories (".git", "node_modules", ".gothicCli") are skipped to avoid touching generated or vendored trees.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache holds per-file content hashes of .templ files. It is safe for concurrent use; callers typically Load, mutate via Update, then Save in a single hot-reload cycle.

func Load

func Load() *Cache

Load reads the cache at TemplCachePath. Missing or malformed files yield an empty cache rather than an error — the cache is an optimization, never a correctness boundary.

func LoadAt

func LoadAt(path string) *Cache

LoadAt is Load with an explicit path; useful for tests that need isolation.

func (*Cache) Invalidate

func (c *Cache) Invalidate(key string)

Invalidate removes key from the cache (e.g., when the generated _templ.go counterpart is missing and we want to force regeneration).

func (*Cache) Save

func (c *Cache) Save() error

Save atomically writes the cache to disk (write to .tmp then rename). Failures are silent — the cache is best-effort.

func (*Cache) UpToDate

func (c *Cache) UpToDate(key, hash string) bool

UpToDate reports whether the cached hash for key matches hash and is non-empty.

func (*Cache) Update

func (c *Cache) Update(key, hash string)

Update records hash for key in memory. Call Save to persist.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL