native

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: 15 Imported by: 0

Documentation

Overview

Package native compiles the modules of an app's server build to Go, so pages render without the JavaScript engine (ADR 0011). The modules come from the Vite plugin in packages/tern/src/vite/native.ts, as ESTree.

Index

Constants

View Source
const IRVersion = 1

IRVersion is the format of .tern/native/ir.json that this package reads.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compiler

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

Compiler turns the IR into Go.

type IR

type IR struct {
	Version int    `json:"version"`
	Svelte  string `json:"svelte"`
	Entry   string `json:"entry"`
	// Bundled holds the modules the bundle has; the rest were tree-shaken.
	// Nil means all of them.
	Bundled map[string]bool `json:"bundled"`
	// Env is import.meta.env in the server build.
	Env map[string]any `json:"env"`
	// FileURLs maps import.meta's asset URL placeholders to the URLs
	// the bundle has.
	FileURLs map[string]string `json:"fileUrls"`
	Modules  []struct {
		ID      string             `json:"id"`
		Imports map[string]*string `json:"imports"`
		AST     *Node              `json:"ast"`
		// Code is the module's source, for functions' toString.
		Code string `json:"code"`
	} `json:"modules"`
}

IR is the server build's modules, as the Vite plugin recorded them.

func LoadIR

func LoadIR(path string) (*IR, error)

LoadIR reads the IR file.

type Node

type Node struct {
	Type  string
	Start int
	End   int
	// contains filtered or unexported fields
}

Node is an ESTree node.

func (*Node) Bool

func (n *Node) Bool(key string) bool

Bool is a boolean field.

func (*Node) Child

func (n *Node) Child(key string) *Node

Child is a node-valued field, or nil.

func (*Node) Has

func (n *Node) Has(key string) bool

Has reports whether the node has a non-null field.

func (*Node) List

func (n *Node) List(key string) []*Node

List is a list-of-nodes field; elements may be nil, for array holes.

func (*Node) Raw

func (n *Node) Raw(key string) json.RawMessage

Raw is a field's JSON, for literal values.

func (*Node) Str

func (n *Node) Str(key string) string

Str is a string field.

func (*Node) UnmarshalJSON

func (n *Node) UnmarshalJSON(b []byte) error

UnmarshalJSON keeps a node's fields raw and decodes them as they are used.

type Options

type Options struct {
	// Package is the Go package name; the default is native.
	Package string
	// Prefix starts the names the file declares, so it can share a package:
	// with "tern", the realm type is ternRealm and New is ternNew.
	Prefix string
	// Register, if set, is an assignable expression, such as
	// "ternManifest.Native", that an init function sets to New.
	Register string
	// EmbedSources, if set, is the path the caller writes Result.Sources
	// to, which the file embeds; otherwise the sources are in the file.
	EmbedSources string
}

Options say how the Go comes out.

type Problem

type Problem struct {
	Module string
	Offset int
	What   string
}

Problem is something the compiler couldn't compile. The code compiles to a stub that raises js.Unsupported when it runs, so only a request that reaches it falls back to the JavaScript engine.

func (Problem) String

func (p Problem) String() string

type Result

type Result struct {
	// Source is the Go file, package native.
	Source []byte
	// Problems lists what couldn't be compiled, in module order.
	Problems []Problem
	// Modules is how many modules were compiled.
	Modules int
	// Mismatches lists the intrinsics not used because the function they
	// stand for has changed, with its fingerprint now.
	Mismatches []string
	// Sources is the modules' code, gzipped, for Options.EmbedSources.
	Sources []byte
}

Result is a compiled app.

func Compile

func Compile(ir *IR, opts Options) (*Result, error)

Compile compiles the IR's modules reachable from its entry.

Jump to

Keyboard shortcuts

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