JavaScript API
compileFromAst
Compile an existing AST through rewrite and generation.
function compileFromAst(
ast: Ast,
options?: CompileFromAstOptions,
): Promise<CodeResult>;
function compileFromAst(
program: Program,
code: string,
options?: CompileFromAstOptions,
): Promise<CodeResult>;Compiles an existing AST through rewrite and generation, with plugins, in memory.
Parameters
The function accepts either an Ast instance, or a bare ESTree Program together with its source text.
| Name | Type | Description |
|---|---|---|
ast | Ast | An AST instance. |
options | CompileFromAstOptions | Optional compile options. |
| Name | Type | Description |
|---|---|---|
program | Program | A bare ESTree program. |
code | string | The source text the program's spans anchor to. Required with program. |
options | CompileFromAstOptions | Optional compile options. |
CompileFromAstOptions
| Field | Type | Default | Description |
|---|---|---|---|
cwd | string | process.cwd() | Current working directory. |
file | string | index.ts | The file being compiled. Its extension is the final fallback for the grammar. |
language | Language | inferred | The grammar of the code. Overrides the AST language and extension inference. |
sourceType | SourceType | inferred | The module system of the code. Overrides the grammar's module kind. |
output | OutputOptions | { file: file } | Output code settings. output.file derives the default source-map URL. |
sourceMap | SourceMapOptions | off | The source-map inputs and outputs requested for the run. |
plugins | PluginOption[] | [] | The plugins to run. |
The grammar precedence is options.language > Ast.language > file extension. compileFromAst consumes the AST language but returns only code and an optional map.
An empty output.file or sourceMap.output.url is treated as unset, same as absent.
Returns
Promise<CodeResult>:
| Field | Type | Description |
|---|---|---|
code | string | The generated code. |
map | SourceMap | null | The source map for the generated code, when one was produced. |
Example
import type { AstResult, CodeResult } from "telarel";
import { compileFromAst, parse } from "telarel";
const { ast }: AstResult = await parse("const x: number = 1;");
const result: CodeResult = await compileFromAst(ast);Last updated on
Copyright © 2026, Alpheus