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.

NameTypeDescription
astAstAn AST instance.
optionsCompileFromAstOptionsOptional compile options.
NameTypeDescription
programProgramA bare ESTree program.
codestringThe source text the program's spans anchor to. Required with program.
optionsCompileFromAstOptionsOptional compile options.

CompileFromAstOptions

FieldTypeDefaultDescription
cwdstringprocess.cwd()Current working directory.
filestringindex.tsThe file being compiled. Its extension is the final fallback for the grammar.
languageLanguageinferredThe grammar of the code. Overrides the AST language and extension inference.
sourceTypeSourceTypeinferredThe module system of the code. Overrides the grammar's module kind.
outputOutputOptions{ file: file }Output code settings. output.file derives the default source-map URL.
sourceMapSourceMapOptionsoffThe source-map inputs and outputs requested for the run.
pluginsPluginOption[][]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>:

FieldTypeDescription
codestringThe generated code.
mapSourceMap | nullThe 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

On this page