Web & UI/Crest Plugins

Crest Plugins

Extend Crest with compile-time and runtime custom elements via ElementPlugin, register them in fletch.plugins.go, and verify deterministic output.

crestgithub.com/fletchhq/fletch/crest/plugin· fletch docs show crest-plugins

On this page

github.com/fletchhq/fletch/crest/plugin lets you add custom tags to Crest templates that expand to static HTML, emit fingerprinted assets, splice Crest AST nodes, or generate typed Go calls for runtime rendering. The package depends only on the Go standard library (AssetSink and BuildCache), and every built-in <f:*> tag uses this same public API.

Built-in elements

Fletch ships four element packages under github.com/fletchhq/fletch/elements:

Tag Package Mode Attributes and options
<f:icon> elements/icon StaticOnly name (required, "set:icon" reads assets/icons/set/icon.svg), class, size (int, default 20). Configure root via icon.WithDir.
<f:img> elements/img StaticOrRuntime src (path under assets/ or runtime images.Ref), preset (enum from img.Presets(...)), sizes, alt (required; use alt="" for decorative images), priority (bool, default false), class. Options: img.Presets, img.WithProcessor, img.WithDir.
<f:markdown> elements/markdown StaticOnly (RawBody) Compiles inline Markdown body at build time.
<f:csrf> elements/csrf StaticOnly name (default _csrf).
<f:icon name="lucide:arrow-right" size="18" class="inline-block" />

<f:img
  src="images/hero.jpg"
  preset="hero"
  sizes="(min-width: 1024px) 960px, 100vw"
  alt="Studio workbench"
  priority
/>

<f:markdown>
  ## Release notes

  Markdown inside this tag compiles at build time.
</f:markdown>

<form method="post" action="/checkout">
  <f:csrf />
</form>

Registering elements with fletch.plugins.go

Fletch does not use Go’s dynamic plugin.so loader. Instead, you register built-in and custom elements in a root fletch.plugins.go file guarded by the fletchbuild build tag:

//go:build fletchbuild

package main

import (
	"github.com/fletchhq/fletch/cmd/fletchbuild"
	"github.com/fletchhq/fletch/elements/icon"
	"github.com/fletchhq/fletch/elements/img"
	"example.com/shop/presets"
)

func main() {
	fletchbuild.Main(fletchbuild.Elements(
		icon.Element(),
		img.Element(img.Presets(presets.All...)),
	))
}

When compiling templates, Fletch builds that entry point with CGO_ENABLED=0:

go build -tags fletchbuild -o .fletch/bin/compiler ./fletch.plugins.go

The compiled binary is cached using a SHA-256 key written to .fletch/bin/compiler.key. That key hashes GOVERSION, fletch.plugins.go, go.mod, go.sum, and every imported in-module .go file, so the compiler rebuilds only when plugin code or dependencies change.

Writing a custom ElementPlugin

Implement plugin.ElementPlugin to define a new element:

type ElementPlugin interface {
	Spec() ElementSpec
	Build(ctx context.Context, bc *BuildContext, el *ast.Element) (Result, error)
}

Declaring ElementSpec

Spec() describes the tag name, attribute schema, body handling, and execution mode. The compiler validates attributes against Attrs before calling Build.

Field Description
Tag Element tag name.
Prefix Namespace prefix (for example, f).
Description Short description surfaced in inspection output.
RawBody When true, passes the element body unparsed (used by <f:markdown>).
Mode plugin.StaticOnly or plugin.StaticOrRuntime.
Version Plugin version included in cache keys when output changes.
Attrs Slice of plugin.AttrSpec definitions.

Each AttrSpec accepts Name, Type (plugin.String, plugin.Int, plugin.Bool, plugin.Enum, or plugin.URL), Required, Default, Enum, and Doc.

package badge

import (
	"context"

	"github.com/fletchhq/fletch/crest/ast"
	"github.com/fletchhq/fletch/crest/plugin"
)

type Element struct{}

func (Element) Spec() plugin.ElementSpec {
	return plugin.ElementSpec{
		Prefix:      "f",
		Tag:         "badge",
		Description: "Renders a static status badge.",
		Mode:        plugin.StaticOnly,
		Version:     "1",
		Attrs: []plugin.AttrSpec{
			{Name: "label", Type: plugin.String, Required: true, Doc: "Visible badge text."},
			{Name: "tone", Type: plugin.Enum, Default: "neutral", Enum: []string{"neutral", "warn"}, Doc: "Visual tone."},
		},
	}
}

func (Element) Build(ctx context.Context, bc *plugin.BuildContext, el *ast.Element) (plugin.Result, error) {
	return plugin.Result{
		HTML: `<span class="badge">ok</span>`,
	}, nil
}

BuildContext and Result

Build receives a *plugin.BuildContext with:

  • File: path to the .crest file being compiled.
  • Sink: AssetSink for writing or inlining generated assets.
  • Cache: BuildCache for memoizing expensive build steps across templates.
  • Has: func(tag string) bool to check whether another element tag is registered.
  • Report: func(Diagnostic) for emitting compiler warnings or errors (pair with plugin.DiagnosticAt).

Build returns a plugin.Result with exactly one output field populated:

Field Use when
HTML string The element resolves completely to static HTML at compile time.
Crest []ast.Node The element expands into Crest AST nodes for further compilation.
Call *RuntimeCall The element needs runtime evaluation (StaticOrRuntime mode). RuntimeCall carries Imports and an Expr (goast.Expr).

Any Result may also include Assets []plugin.AssetRef, where each reference records Logical, Hash, and Integrity (sha384).

Standard library helpers

github.com/fletchhq/fletch/crest/plugin includes helpers for sinks, caching, and asset work:

Helper Purpose
plugin.DirSink(dir, urlPrefix) Writes emitted assets to dir and returns URLs under urlPrefix.
plugin.InlineSink(maxBytes) Inlines assets up to maxBytes.
plugin.FileCache(dir) Disk-backed BuildCache that deduplicates concurrent builds for the same key.
plugin.KeyOf Computes deterministic cache keys from plugin inputs.
plugin.Dedent Strips shared leading indentation from raw element bodies.
plugin.ImageSize Reads image dimensions without pulling in external libraries.
plugin.DiagnosticAt Builds a positioned Diagnostic for bc.Report.

Diagnostics and determinism checks

The Crest compiler emits structured diagnostics for plugin registration, attribute validation, and build failures:

Code Meaning
W0330 Plugin tag collides with an existing Crest component.
W0331 Invalid or missing attribute according to ElementSpec.Attrs.
W0332 Dynamic attribute expression passed to a StaticOnly element.
W0340–W0344 Plugin build failures, including W0343 for non-deterministic output.

Inspecting registered elements

Print all registered elements, their modes, and their attribute schemas as JSON:

fletch ax inspect elements --json