tsgotranspile

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jul 15, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package tsgotranspile wraps tsgo's compiler.Program.Emit for in-memory TypeScript -> JavaScript transpilation. It is Ramune's canonical TS->JS path (the role esbuild.Transform used to fill for CLI-level type stripping and down-level lowering). Bundling stays on esbuild.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FirstError

func FirstError(diags []*ast.Diagnostic) error

FirstError returns a formatted error matching the first CategoryError diagnostic in diags, or nil when none are at error severity. Callers that only care about fail/pass can compare the return to nil.

Types

type JsxEmit

type JsxEmit = core.JsxEmit

Type aliases so external callers can construct Options without reaching into the vendored tsgo internals (which stay in internal/).

const (
	JsxEmitNone        JsxEmit = core.JsxEmitNone
	JsxEmitPreserve    JsxEmit = core.JsxEmitPreserve
	JsxEmitReact       JsxEmit = core.JsxEmitReact
	JsxEmitReactJSX    JsxEmit = core.JsxEmitReactJSX
	JsxEmitReactJSXDev JsxEmit = core.JsxEmitReactJSXDev
)

JSX emit modes.

type ModuleKind

type ModuleKind = core.ModuleKind

Type aliases so external callers can construct Options without reaching into the vendored tsgo internals (which stay in internal/).

const (
	ModuleKindCommonJS ModuleKind = core.ModuleKindCommonJS
	ModuleKindESNext   ModuleKind = core.ModuleKindESNext
	ModuleKindPreserve ModuleKind = core.ModuleKindPreserve
)

Module kinds.

type Options

type Options struct {
	FileName               string            // defaults to "input.ts"
	Target                 core.ScriptTarget // ScriptTargetESNext / ES2017 / ...
	Module                 core.ModuleKind   // usually ModuleKindCommonJS
	JSX                    core.JsxEmit      // JsxEmitPreserve / JsxEmitReact / ...
	JsxFactory             string            // e.g. "__jsx"; paired with JsxEmitReact
	JsxFragmentFactory     string            // e.g. "__Fragment"; paired with JsxEmitReact
	RemoveComments         bool
	InlineSourceMap        bool
	ExperimentalDecorators bool
	EmitDecoratorMetadata  bool
}

Options controls a single Transpile call. Every caller in Ramune picks Target / Module deliberately per backend, so there is no default.

type Result

type Result struct {
	JS          string
	Diagnostics []*ast.Diagnostic
}

Result is the output of a Transpile call. Diagnostics covers parse errors and emitter diagnostics; semantic (type-check) diagnostics are intentionally omitted - use `ramune check` for that.

func Transpile

func Transpile(source string, opts Options) (Result, error)

Transpile is a convenience wrapper that calls the package-level default Transpiler. For repeated calls (REPL, nodecompat require) construct a dedicated *Transpiler via New() - it amortizes the parse cost of bundled lib.d.ts across calls.

type ScriptTarget

type ScriptTarget = core.ScriptTarget

Type aliases so external callers can construct Options without reaching into the vendored tsgo internals (which stay in internal/).

const (
	ScriptTargetES2017 ScriptTarget = core.ScriptTargetES2017
	ScriptTargetESNext ScriptTarget = core.ScriptTargetESNext
)

Script target values. Only the ones Ramune actually uses are surfaced.

type Transpiler

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

Transpiler is a reusable tsgo-backed TS->JS transpiler. It caches the parsed lib.d.ts SourceFile ASTs across calls so repeated Transpile invocations (REPL, nodecompat loader) skip the dominant cost of re-parsing the bundled TypeScript lib set each time.

A Transpiler is safe for concurrent use - calls serialize internally because binding mutates AST node state on the shared lib SourceFiles. For workloads that want true parallelism, create multiple Transpilers.

func New

func New() *Transpiler

New returns a Transpiler with a fresh lib.d.ts cache. The first call to Transpile populates the cache (paying the one-time parse cost); subsequent calls reuse it.

func (*Transpiler) Transpile

func (t *Transpiler) Transpile(source string, opts Options) (Result, error)

Transpile runs tsgo's full emit pipeline (TypeEraser, ImportElision, RuntimeSyntax, Decorators, JSX, ESTransform, ModuleTransform, printer) over source. The source is overlaid onto a synthetic absolute path so bundled lib.d.ts lookups continue to resolve through the shared host.

Jump to

Keyboard shortcuts

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