WebAssembly module for SWC compiler providing JavaScript/TypeScript parsing, transformation, and minification in browsers and Node.js
—
Parse JavaScript and TypeScript source code into Abstract Syntax Trees (AST) with comprehensive syntax support, error reporting, and configurable parser options.
Parses source code into an AST asynchronously, returning a Module by default or Script for non-module code.
/**
* Parse source code into an AST asynchronously
* @param src - Source code string to parse
* @param options - Parser configuration options
* @returns Promise resolving to Module AST
*/
function parse(src: string, options?: ParseOptions): Promise<Module>;
/**
* Parse source code into a Script AST asynchronously
* @param src - Source code string to parse
* @param options - Parser configuration with isModule set to false or "commonjs"
* @returns Promise resolving to Script AST
*/
function parse(src: string, options: ParseOptions & { isModule: false | "commonjs" }): Promise<Script>;Usage Examples:
import * as swc from "@swc/wasm";
// Parse TypeScript code
const ast = await swc.parse(`
interface User {
name: string;
age: number;
}
const user: User = { name: "Alice", age: 25 };
`, {
syntax: "typescript",
target: "es2020"
});
// Parse JavaScript with JSX
const jsxAst = await swc.parse(`
function Component() {
return <div>Hello World</div>;
}
`, {
syntax: "ecmascript",
jsx: true,
target: "es2018"
});
// Parse as Script (non-module)
const scriptAst = await swc.parse(`
var x = 1;
function foo() { return x; }
`, {
syntax: "ecmascript",
isModule: false
});Parses source code into an AST synchronously with identical interface to async version.
/**
* Parse source code into an AST synchronously
* @param src - Source code string to parse
* @param options - Parser configuration options
* @returns Module AST
*/
function parseSync(src: string, options?: ParseOptions): Module;
/**
* Parse source code into a Script AST synchronously
* @param src - Source code string to parse
* @param options - Parser configuration with isModule set to false or "commonjs"
* @returns Script AST
*/
function parseSync(src: string, options: ParseOptions & { isModule: false | "commonjs" }): Script;Usage Examples:
import * as swc from "@swc/wasm";
// Synchronous parsing
const ast = swc.parseSync(`class Example {}`, {
syntax: "typescript",
target: "es2021"
});
// Parse with comments preserved
const astWithComments = swc.parseSync(`
// This is a comment
const value = 42;
`, {
syntax: "ecmascript",
comments: true
});interface ParseOptions extends ParserConfig {
/** Include comments in the AST */
comments?: boolean;
/** Parse as script instead of module */
script?: boolean;
/** Compilation target for syntax validation */
target?: JscTarget;
}type ParserConfig = TsParserConfig | EsParserConfig;
interface TsParserConfig {
/** Parser syntax type */
syntax: "typescript";
/** Enable TSX/JSX parsing */
tsx?: boolean;
/** Enable decorator syntax */
decorators?: boolean;
/** Enable dynamic import syntax */
dynamicImport?: boolean;
}
interface EsParserConfig {
/** Parser syntax type */
syntax: "ecmascript";
/** Enable JSX parsing */
jsx?: boolean;
/** Enable function bind operator */
functionBind?: boolean;
/** Enable decorator syntax */
decorators?: boolean;
/** Allow decorators before export */
decoratorsBeforeExport?: boolean;
/** Enable export default from syntax */
exportDefaultFrom?: boolean;
/** Enable import assertions */
importAssertions?: boolean;
}type Program = Module | Script;
interface Module extends Node, HasSpan {
type: "Module";
body: ModuleItem[];
interpreter: string;
}
interface Script extends Node, HasSpan {
type: "Script";
body: Statement[];
interpreter: string;
}
type ModuleItem = ModuleDeclaration | Statement;interface Node {
type: string;
}
interface HasSpan {
span: Span;
}
interface Span {
start: number;
end: number;
ctxt: number;
}type Statement =
| BlockStatement
| EmptyStatement
| DebuggerStatement
| WithStatement
| ReturnStatement
| LabeledStatement
| BreakStatement
| ContinueStatement
| IfStatement
| SwitchStatement
| ThrowStatement
| TryStatement
| WhileStatement
| DoWhileStatement
| ForStatement
| ForInStatement
| ForOfStatement
| Declaration
| ExpressionStatement;type Expression =
| ThisExpression
| ArrayExpression
| ObjectExpression
| FunctionExpression
| UnaryExpression
| UpdateExpression
| BinaryExpression
| AssignmentExpression
| MemberExpression
| SuperPropExpression
| ConditionalExpression
| CallExpression
| NewExpression
| SequenceExpression
| Identifier
| Literal
| TemplateLiteral
| TaggedTemplateExpression
| ArrowFunctionExpression
| ClassExpression
| YieldExpression
| MetaProperty
| AwaitExpression
| ParenthesisExpression
| JSXElement
| JSXFragment
| TsTypeAssertion
| TsConstAssertion
| TsNonNullExpression
| TsAsExpression
| TsInstantiation
| PrivateName
| OptionalChainingExpression
| Invalid;Parse functions throw JavaScript Error objects for syntax errors with detailed position information:
ErrorExample Error Handling:
try {
const ast = await swc.parse("invalid syntax {}", {
syntax: "ecmascript"
});
} catch (error) {
console.error("Parse Error:", error.message);
// Output: "Syntax Error: Expected ';', '}' or <eof>"
}Install with Tessl CLI
npx tessl i tessl/npm-swc--wasm