Code Generation
The codegen package generates JavaScript Web Component classes from go-html layout slots.
See also: Home | Architecture | Node-API | Pipeline | WebAssembly
Package
Import: import "forge.lthn.ai/core/go-html/codegen"
API
GenerateClass
func GenerateClass(tag, slot string) (string, error)
Generates a JavaScript class extending HTMLElement for a custom element. The class includes a connectedCallback that renders content into the element's inner HTML from the named slot.
js, err := codegen.GenerateClass("app-header", "H")
Returns an error if the tag name is invalid (must contain a hyphen per the Web Components spec).
GenerateRegistration
func GenerateRegistration(tag, className string) string
Generates the customElements.define() call to register the component.
reg := codegen.GenerateRegistration("app-header", "AppHeader")
// customElements.define('app-header', AppHeader);
TagToClassName
func TagToClassName(tag string) string
Converts kebab-case tag names to PascalCase class names:
| Tag | Class Name |
|---|---|
app-header |
AppHeader |
my-nav-bar |
MyNavBar |
x-button |
XButton |
GenerateBundle
func GenerateBundle(slots map[string]string) (string, error)
Generates a complete JavaScript bundle containing all Web Component classes and their registrations.
bundle, err := codegen.GenerateBundle(map[string]string{
"H": "app-header",
"C": "app-content",
"F": "app-footer",
})
The bundle is self-contained — include it in a <script> tag and the custom elements are registered automatically.
Web Components Spec Compliance
Generated components follow the Web Components standard:
- Tag names contain at least one hyphen (validated)
- Classes extend
HTMLElement - Registered via
customElements.define() - Use
connectedCallbackfor lifecycle
Use with WASM
The codegen package is used by the WASM entrypoint's registerComponents() function (see WebAssembly). In the browser:
// WASM calls codegen internally
gohtml.registerComponents(JSON.stringify({
"H": "app-header",
"C": "app-content"
}))
For server-side pre-generation (e.g. during build), use the Go API directly:
bundle, _ := codegen.GenerateBundle(slots)
os.WriteFile("dist/components.js", []byte(bundle), 0644)