codegen

package
v1.12.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 28 Imported by: 0

Documentation

Overview

Package codegen generates the code that connects a Tern app's routes to the runtime: tern.gen.go for the Go server, the client route manifest for Vite, and TypeScript types for every route.

Index

Constants

View Source
const (
	RoutesDir   = "src/routes"
	AppHTML     = "src/app.html"
	GenDir      = ".tern"
	GoFile      = "tern.gen.go"
	ClientEntry = ".tern/generated/client.js"
	ClientOut   = ".tern/output/client"
	ServerEntry = ".tern/generated/server.js"
	ServerOut   = ".tern/output/server"
)

Project layout conventions.

View Source
const ConfigFile = "tern.json"

ConfigFile is the optional project configuration, shared by the code generator, the Vite plugin and the runtime.

View Source
const ContentDir = "src/content"

ContentDir holds Markdown collections read with the content package.

View Source
const EmailsDir = "src/emails"

EmailsDir holds email templates, one Svelte component per email.

View Source
const LiveDir = ".tern/goscript/live"

LiveDir holds the Go packages of live components (<script lang="go" live>), which are compiled into the app's binary.

View Source
const LiveTypesDir = GenDir + "/goscript/types"

LiveTypesDir holds the TypeScript types of live components' state and Funcs, which tern check uses to type-check their markup.

View Source
const LocalesDir = "src/locales"

LocalesDir holds the app's messages, one <locale>.json per language.

View Source
const MarkdownPagesDir = ".tern/generated/pages"

MarkdownPagesDir holds the Svelte components generated from +page.md files.

View Source
const OpenAPIFile = GenDir + "/generated/openapi.json"

OpenAPIFile is the generated OpenAPI description of the typed endpoints.

Variables

View Source
var ServiceWorkerSources = []string{"src/service-worker.ts", "src/service-worker.js"}

ServiceWorkerSources are the files that define an app's service worker.

Functions

func FindServiceWorker added in v1.1.0

func FindServiceWorker(root string) string

FindServiceWorker returns the service worker source in root, or "".

func GoScriptID added in v1.3.0

func GoScriptID(rel string) string

GoScriptID is a component's ID for its path relative to the app root; it matches componentID in packages/tern/src/vite/goscript.ts.

Types

type Config added in v1.0.0

type Config struct {
	// BasePath serves the app under a URL prefix such as "/app". It must
	// start with a slash and must not end with one. The default is "".
	BasePath string `json:"basePath"`
	// I18n configures translations in src/locales, if the app has them.
	I18n struct {
		// DefaultLocale is used when no other locale fits, and for messages
		// missing from a locale. It defaults to "en", or the first locale.
		DefaultLocale string `json:"defaultLocale"`
	} `json:"i18n"`
	// ServiceWorker configures src/service-worker.ts, if the app has one.
	ServiceWorker struct {
		// Register is whether the client registers the service worker. It
		// defaults to true; set it to false to register it yourself.
		Register *bool `json:"register"`
	} `json:"serviceWorker"`
	// Native is whether tern build compiles the server renderer to Go, so
	// pages render without the JavaScript engine (ADR 0011). It defaults
	// to true.
	Native *bool `json:"native"`
}

Config is the contents of tern.json.

func LoadConfig added in v1.0.0

func LoadConfig(root string) (Config, error)

LoadConfig reads tern.json from root. A missing file is an empty config.

func (Config) NativeEnabled added in v1.5.0

func (c Config) NativeEnabled() bool

NativeEnabled reports whether tern build compiles the server renderer to Go.

type Result

type Result struct {
	Manifest *routes.Manifest
	// Changed lists the files that were written because their content changed.
	Changed []string
	// GoChanged reports whether Go code changed, so the server must be rebuilt.
	GoChanged bool
	// Prerender reports whether any route is prerendered at build time.
	Prerender bool
	// Warnings are problems that do not stop the build, such as missing
	// translations.
	Warnings []string
}

Result describes what Generate produced.

func Generate

func Generate(root string) (*Result, error)

Generate scans the project at root and writes all generated files. Files whose content is unchanged are not rewritten, so file watchers only see real changes.

Jump to

Keyboard shortcuts

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