Codegen
printSync generates source text from a program AST. It is exported by @swc-next/core, @swc-next/codegen, and the initialized @swc-next/core-browser package.
Signature and inputs
Input nodes follow the AST declarations shared with the parser.
CodegenOptions
Omitted fields use their defaults. Non-boolean option values, such as minify: 1, throw a TypeError.
asciiOnly
Type: boolean
Default: false
Use escape sequences for non-ASCII characters in identifiers, strings, and supported template text. With false, the printer can emit Unicode characters directly.
Escaping is context-dependent. Regular expression bodies and tagged template raw text are preserved, so this option does not guarantee that the entire output contains only ASCII. It does not change string values at runtime.
inlineScript
Type: boolean
Default: false
Escape case-insensitive </script sequences in strings and untagged templates, and insert separators when neighboring tokens would form such a sequence. This helps prevent generated JavaScript text from closing an HTML <script> element early.
The escape preserves the string's runtime value. Tagged template raw text is preserved because changing it would be observable by the tag function. This option therefore does not guarantee that arbitrary generated output can be embedded directly in HTML.
minify
Type: boolean
Default: false
Use compact printing: remove optional whitespace, indentation, and line breaks, and choose shorter spellings where supported. Required separators remain so that adjacent tokens are parsed correctly. With false, print readable, indented code.
minify does not evaluate 1 + 2, remove unused declarations, or shorten variable names. Compression and name mangling belong to the separate Minifier component. Printing preserves TypeScript and JSX syntax with either setting.
Metadata
Pass parseSync(...).meta to preserve the input language and source type. Explicit metadata takes precedence; when omitted, source type comes from the program and language is inferred from the tree. Printing does not remove TypeScript types or lower JSX.
CodegenOutput
The JavaScript interface returns generated code only. It does not expose source maps or comment emission options; the parser's separate comment list is not passed to the printer.