Documentation
¶
Overview ¶
Package plugin is the public extension API of the Crest compiler. Built-in elements and third-party plugins use the same API; there are no private hooks.
Index ¶
- func Dedent(s string) string
- func ImageSize(format string, data []byte) (w, h int, err error)
- type AssetRef
- type AssetSink
- type AttrSpec
- type AttrType
- type BuildCache
- type BuildContext
- type Diagnostic
- type DirAssetSink
- type DiskCache
- type ElementPlugin
- type ElementSpec
- type Fix
- type Key
- type Mode
- type Result
- type RuntimeCall
- type Severity
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AssetRef ¶
type AssetRef struct {
Logical string `json:"logical"`
Hash string `json:"hash"`
Integrity string `json:"integrity"`
}
AssetRef names an emitted artifact. Identical bytes give an identical ref.
type AssetSink ¶
type AssetSink interface {
Emit(ctx context.Context, kind, ext string, data []byte) (AssetRef, error)
}
AssetSink stores build artifacts. Emit is content-addressed, idempotent, and safe for concurrent use.
func InlineSink ¶
InlineSink returns a sink whose refs are data: URIs, for small artifacts or apps that serve no static files. Artifacts over maxBytes are an error.
type AttrSpec ¶
type AttrSpec struct {
Name string `json:"name"`
Type AttrType `json:"type"`
Required bool `json:"required,omitempty"`
Enum []string `json:"enum,omitempty"`
Default string `json:"default,omitempty"`
Doc string `json:"doc,omitempty"`
}
AttrSpec describes one attribute.
type AttrType ¶
type AttrType string
AttrType is the type of a plugin attribute: one of the constants below, or a Go type name such as "images.Ref" that go build checks.
type BuildCache ¶
type BuildCache interface {
Do(ctx context.Context, key Key, fn func(context.Context) ([]byte, error)) ([]byte, error)
}
BuildCache runs fn once per key and keeps its output across builds.
type BuildContext ¶
type BuildContext struct {
File string
Sink AssetSink
Cache BuildCache
Has func(tag string) bool
Report func(Diagnostic)
}
BuildContext is what the compiler hands a plugin's Build.
type Diagnostic ¶
type Diagnostic struct {
Code string `json:"code"`
Severity Severity `json:"severity"`
File string `json:"file"`
Line int `json:"line"`
Col int `json:"col"`
Message string `json:"message"`
Fix *Fix `json:"fix,omitempty"`
Doc string `json:"doc,omitempty"`
}
Diagnostic is the one diagnostic shape shared by crest check --json, fletch check --json, the dev error page, and MCP.
func DiagnosticAt ¶
DiagnosticAt builds a diagnostic positioned at pos.
type DirAssetSink ¶
type DirAssetSink struct {
// contains filtered or unexported fields
}
DirAssetSink writes artifacts to a directory as <kind>.<hash8>.<ext> and resolves their logical names ("gen/<file>") to URLs under a prefix. It implements crest.AssetResolver, so it can be installed with crest.WithAssetResolver.
func DirSink ¶
func DirSink(dir, urlPrefix string) *DirAssetSink
DirSink returns a sink that writes into dir and serves under urlPrefix.
func (*DirAssetSink) AssetURL ¶
func (s *DirAssetSink) AssetURL(logical string) string
AssetURL maps "gen/<file>" to "<prefix>/<file>"; other names pass through.
type DiskCache ¶
type DiskCache struct {
// contains filtered or unexported fields
}
DiskCache is a BuildCache stored in a directory, with concurrent callers of the same key sharing one run.
type ElementPlugin ¶
type ElementPlugin interface {
// Spec describes the element. The compiler checks attributes against it
// before Build runs.
Spec() ElementSpec
// Build expands one use of the element. el is a copy whose attributes
// passed the Spec check and include their defaults. An error is
// reported as W0340, at the position of an *ast.Error or else at the
// element. The same input must give the same Result.
Build(ctx context.Context, bc *BuildContext, el *ast.Element) (Result, error)
}
ElementPlugin compiles one element tag (or tag prefix).
type ElementSpec ¶
type ElementSpec struct {
Tag string `json:"tag"`
Prefix bool `json:"prefix,omitempty"`
Description string `json:"description,omitempty"`
RawBody bool `json:"raw_body,omitempty"`
Mode Mode `json:"mode"`
Version string `json:"version"`
Attrs []AttrSpec `json:"attrs,omitempty"`
}
ElementSpec describes an element to the compiler, the LSP, and AX.
func (ElementSpec) Matches ¶
func (s ElementSpec) Matches(tag string) bool
Matches reports whether the spec handles tag.
type Fix ¶
type Fix struct {
Kind string `json:"kind"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Col int `json:"col,omitempty"`
Insert string `json:"insert,omitempty"`
Replace string `json:"replace,omitempty"`
With string `json:"with,omitempty"`
Command string `json:"command,omitempty"`
}
Fix is a machine-applicable remedy. Kind is "edit", "command", or "none". An edit either inserts Insert at Line:Col, or replaces the text Replace that starts there with With.
type Mode ¶
type Mode uint8
Mode says whether an element accepts dynamic attribute values.
func (Mode) MarshalText ¶
MarshalText encodes m as static_only or static_or_runtime.
type Result ¶
type Result struct {
// HTML is static markup with no asset references.
HTML string
// Crest nodes are compiled as if written in the template; they may
// contain other plugin elements.
Crest []ast.Node
// Call delegates to a runtime helper for dynamic values.
Call *RuntimeCall
Assets []AssetRef
}
Result is a plugin's output. Exactly one of HTML, Crest, or Call is set.
type RuntimeCall ¶
RuntimeCall is a Go expression, evaluated at render time, that must yield a crest.Component. The generated file imports Imports.