Generator
generateSync generates source text from a program AST. It is exported by @swc-next/core, @swc-next/generator, and the initialized @swc-next/core-browser package.
Signature and inputs
Input nodes follow the AST declarations shared with the parser.
GenerateApiOptions
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 generator 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, untagged templates, template literal types, JSX attribute strings and text, comments, and hashbangs. Insert separators in JSX closing tags and 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 and regular expression bodies are preserved because rewriting their contents can change behavior. 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.
Printing infers JavaScript or TypeScript from the AST and reads source type from program.sourceType. It preserves TypeScript types and JSX syntax. Single type parameter arrow functions without a constraint receive a trailing comma, so their output is valid in both TS and TSX.
GenerateOutput
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 generator.