Transformer
transformSync parses, transforms, and prints JavaScript and TypeScript source in a single call. It is exported by @swc-next/core, @swc-next/transformer, and the initialized @swc-next/core-browser package.
Signature
source is the complete source text. Omitting options or passing null uses the defaults below. Transformation is synchronous.
Unlike parseSync, transformSync does not throw on syntax, semantic, configuration, or transform diagnostics; they are returned in output.diagnostics. Invalid option types or unknown option fields throw a TypeError.
Options encoding is not reentrant. Option getters and Proxy traps must not call transformSync again while it is encoding options.
TransformApiOptions
filename
Type: string
Used across the pipeline:
- Infers
parser.langwithlangFromPathwhenparser.langis omitted. - Infers
parser.sourceTypewithsourceTypeFromPathwhenparser.sourceTypeis omitted. - Sets the
sourcesentry in the generated source map whensourceMapistrue(defaults to"input.js"whenfilenameis omitted). - Supplies the source filename to transforms such as JSX development metadata unless overridden by
transformer.filename.
sourceMap
Type: boolean
Default: false
When true, output.map contains a JSON-encoded source map string. When false, output.map is omitted.
comments
Type: "all" | "none"
Default: "all"
Controls whether comments are emitted in the generated output. When parser.comments is omitted, this option also sets the parser comment mode, except when JSX, TypeScript-with-JSX, or AMD transforms are active: in those cases the parser still collects comments internally so directive pragmas can be read, while comments: "none" still strips comments from the output.
parser
Type: ParserConfig
Accepts the same fields as ParseApiOptions except errorRecovery: lang, sourceType, preserveParens, comments, and semanticErrors.
Explicit parser.lang and parser.sourceType values take precedence over values inferred from filename. When parser.semanticErrors is true, semantic early errors are appended to output.diagnostics; if any error diagnostics are reported during parsing or semantic checks, transformation and code generation are skipped.
transformer
Type: TransformerConfig
Configures syntax transforms. When omitted, TypeScript and JSX transforms are still enabled automatically based on the resolved lang.
generator
Type: GeneratorConfig
Accepts asciiOnly, inlineScript, and minify.
TransformerConfig
filename
Type: string
Overrides the top-level filename for transforms (such as JSX development __source metadata) without changing language inference or source map paths.
target
Type: "es2015" | ... | "es2026" | "esnext" | (string & {}) | string[] | Partial<Record<Engine, string>>
Selects the target environment for ECMAScript lowering:
- ES edition:
"es2015"through"es2026", or"esnext". - Browserslist query or query list: a single query string such as
"> 0.5%, not dead"or an array of queries such as["chrome 70", "chrome 120"]. - Engine version map: minimum versions keyed by engine name (
"chrome","edge","firefox","safari","ios","opera","samsung","node","deno","electron","ie","android","opera_mobile","op_mini","bb","baidu","kaios","and_qq","and_uc").
target currently lowers ES2016–ES2022 and supported RegExp syntax. ES2015 and older syntax is not lowered, and "es3" / "es5" or invalid Browserslist queries return an error diagnostic in output.diagnostics.
typescript
Type: boolean | TypeScriptConfig
Default: enabled when the resolved lang is "ts", "tsx", or "dts". Pass false to keep TypeScript syntax in the output.
jsx
Type: boolean | JsxConfig
Default: enabled with the "automatic" runtime when the resolved lang is "jsx" or "tsx". Pass false to keep JSX syntax in the output.
JSX options target Babel 8; useSpread and useBuiltIns are not supported.
decorators
Type: boolean | DecoratorConfig
Default: false
Pass true to enable "2023-11" decorators, or pass a configuration object:
module
Type: ModuleConfig
Default: "preserve"
Selects the module output format. Accepts a format shorthand string or an object discriminated by type:
Format-specific options:
Passing an option unsupported by the chosen type (such as interop on "preserve") throws a TypeError.
interop: interoperability strategy for lowered default and namespace imports ("babel","node", or"none").dynamicImport:"transform"lowers dynamicimport()calls for the target module format;"preserve"leaves them asimport().
features
Type: Partial<Record<Feature, boolean>>
Per-feature overrides applied after target resolution. true enables a transform; false disables it even if target would otherwise enable it.
Supported feature names:
assumptions
Type: Assumptions
Babel-compatible assumptions. All fields are optional booleans and default to false:
polyfill
Type: false | PolyfillConfig
Default: false
Injects core-js@3 polyfills based on the target environment. Only core-js@3 is supported (core-js@2 and regenerator-runtime are not supported).
To lower syntax with an ES edition while selecting polyfills by browser versions, specify polyfill.target separately:
JsOutput
See Diagnostics and positions for the Diagnostic and DiagnosticLabel shapes.