API reference/Core

Core

The core packages provide synchronous parsing and code generation with the same function signatures and option types in Node.js and browsers.

Packages

PackageRuntimeInitialization
@swc-next/coreNode.jsReady after import
@swc-next/core-browserBrowser WebAssemblyAwait the default init() export

Exports

ExportPurposeReference
parseSync(source, options?)Parse source text into a program, comments, diagnostics, and metadataParser
printSync(program, options?, meta?)Generate a { code } result from a programCodegen
walk(root, hooks, state?)Traverse and edit an AST synchronously; exported by the Node.js packageWalk
langFromPath(path)Infer the parser language from a file pathParser helpers
sourceTypeFromPath(path)Infer the source type from a file pathParser helpers

For browser traversal, import walk from @swc-next/walker. That package is pure JavaScript and requires no initialization. createWalker and visitorKeys are also available from the walker package directly.

Browser WebAssembly

The browser package includes the WebAssembly binary and its JavaScript bindings. Its default export initializes the binding:

export default function init(): Promise<void>;

Await init() before calling parseSync or printSync. It loads the bundled .wasm file; after successful initialization, subsequent calls are idempotent no-ops. Serve the package with a bundler that resolves its WebAssembly asset.

Parsing and printing are synchronous after initialization. Run substantial workloads in a worker to keep the interface responsive.

Type imports

Import types alongside the functions they configure:

import { parseSync, type ParserOptions } from "@swc-next/core";

const options = {
  lang: "ts",
  comments: "flat",
} satisfies ParserOptions;

const result = parseSync("const answer: number = 42;", options);

satisfies checks option names and values while preserving their inferred types. The browser package exports the same types from @swc-next/core-browser. Both packages also re-export AST node types, such as Program and Node.