WebAssembly module for SWC compiler providing JavaScript/TypeScript parsing, transformation, and minification in browsers and Node.js
—
Convert Abstract Syntax Trees (AST) back to JavaScript source code with optional source map generation and configurable output formatting.
Converts a Program AST back to source code asynchronously with optional configuration.
/**
* Convert AST back to source code asynchronously
* @param m - Program AST (Module or Script)
* @param options - Code generation options
* @returns Promise resolving to generated code output
*/
function print(m: Program, options?: Options): Promise<Output>;Usage Examples:
import * as swc from "@swc/wasm";
// Parse then print code
const ast = await swc.parse(`
class Example {
constructor(value) {
this.value = value;
}
}
`, {
syntax: "ecmascript",
target: "es2020"
});
const output = await swc.print(ast, {
jsc: {
target: "es5"
},
sourceMaps: true
});
console.log(output.code);
// Output: ES5-compatible code
// Print with specific formatting
const formatted = await swc.print(ast, {
jsc: {
minify: {
format: {
comments: "some",
semicolons: true
}
}
}
});
// Print with source maps
const withSourceMap = await swc.print(ast, {
sourceMaps: "inline",
sourceFileName: "example.js"
});
console.log(withSourceMap.map); // Inline source mapConverts a Program AST back to source code synchronously with identical interface to async version.
/**
* Convert AST back to source code synchronously
* @param program - Program AST (Module or Script)
* @param options - Code generation options
* @returns Generated code output
*/
function printSync(program: Program, options?: Options): Output;Usage Examples:
import * as swc from "@swc/wasm";
// Synchronous code generation
const ast = swc.parseSync(`const x = 1;`, {
syntax: "ecmascript"
});
const output = swc.printSync(ast, {
jsc: {
target: "es3"
}
});
console.log(output.code);
// Print with minification
const minified = swc.printSync(ast, {
minify: true,
jsc: {
minify: {
compress: {
unused: true
},
mangle: {
toplevel: true
}
}
}
});The print functions accept the same Options interface as transform functions, allowing comprehensive control over code generation.
interface Options extends Config {
/** Source filename for source maps */
sourceFileName?: string;
/** Source root for source maps */
sourceRoot?: string;
/** Output path for source map fixing */
outputPath?: string;
/** Working directory for path resolution */
cwd?: string;
}
interface Config {
/** JavaScript compiler configuration */
jsc?: JscConfig;
/** Module format configuration */
module?: ModuleConfig;
/** Enable minification during generation */
minify?: boolean;
/** Source map generation */
sourceMaps?: boolean | "inline";
/** Include source content in source maps */
inlineSourcesContent?: boolean;
}Control code generation formatting through JscConfig:
interface JscConfig {
/** Compilation target affecting syntax */
target?: JscTarget;
/** Minification options for formatting */
minify?: JsMinifyOptions;
/** Preserve all comments */
preserveAllComments?: boolean;
/** Output character encoding */
output?: {
charset?: "utf8" | "ascii";
};
}
interface JsMinifyOptions {
/** Output formatting options */
format?: JsFormatOptions;
/** Compression settings */
compress?: boolean | TerserCompressOptions;
/** Name mangling settings */
mangle?: boolean | TerserMangleOptions;
}
interface JsFormatOptions {
/** Comment preservation strategy */
comments?: false | "some" | "all" | { regex: string };
/** Semicolon insertion */
semicolons?: boolean;
/** ASCII-only output */
asciiOnly?: boolean;
/** Beautify output */
beautify?: boolean;
/** Maximum line length */
maxLineLen?: number | false;
/** Indentation level */
indentLevel?: number;
}interface Output {
/** Generated source code */
code: string;
/** Source map (if enabled) */
map?: string;
/** Diagnostic messages */
diagnostics: string[];
}The diagnostics array typically contains warnings about code generation, such as:
const ast = await swc.parse(sourceCode, { syntax: "typescript" });
const output = await swc.print(ast, {
sourceMaps: true,
sourceFileName: "input.ts",
sourceRoot: "/src"
});
// output.map contains the source map stringconst output = await swc.print(ast, {
sourceMaps: "inline",
inlineSourcesContent: true
});
// Source map is embedded in output.code as data URLconst es5Output = await swc.print(ast, {
jsc: {
target: "es5"
}
});
// Transforms classes, arrow functions, etc. to ES5// Convert to CommonJS
const cjsOutput = await swc.print(ast, {
module: {
type: "commonjs"
}
});
// Convert to UMD
const umdOutput = await swc.print(ast, {
module: {
type: "umd",
globals: {
"react": "React",
"lodash": "_"
}
}
});const formatted = await swc.print(ast, {
jsc: {
minify: {
format: {
comments: "all",
beautify: true,
indentLevel: 2,
semicolons: true,
maxLineLen: 80
}
}
}
});const optimized = await swc.print(ast, {
minify: true,
jsc: {
target: "es2018",
minify: {
compress: {
dead_code: true,
drop_console: true,
unused: true
},
mangle: {
toplevel: true
},
format: {
comments: false
}
}
}
});Print functions may throw errors for:
try {
const output = await swc.print(ast, options);
} catch (error) {
console.error("Code generation error:", error.message);
}Diagnostic warnings are included in the diagnostics array rather than throwing exceptions.
Install with Tessl CLI
npx tessl i tessl/npm-swc--wasm