WebAssembly module for SWC compiler providing JavaScript/TypeScript parsing, transformation, and minification in browsers and Node.js
—
Optimize JavaScript code for production deployment with configurable compression, name mangling, and output formatting options based on Terser compatibility.
Minifies JavaScript source code asynchronously with comprehensive optimization options.
/**
* Minify JavaScript source code asynchronously
* @param src - JavaScript source code string
* @param opts - Minification configuration options
* @returns Promise resolving to minified code output
*/
function minify(src: string, opts?: JsMinifyOptions): Promise<Output>;Usage Examples:
import * as swc from "@swc/wasm";
// Basic minification
const result = await swc.minify(`
function calculateTotal(items) {
let total = 0;
for (const item of items) {
total += item.price * item.quantity;
}
return total;
}
const items = [
{ price: 10, quantity: 2 },
{ price: 5, quantity: 4 }
];
console.log("Total:", calculateTotal(items));
`, {
module: false
});
console.log(result.code);
// Output: Minified code with shortened variable names
// Advanced minification with custom options
const optimized = await swc.minify(sourceCode, {
compress: {
dead_code: true,
drop_console: true,
drop_debugger: true,
unused: true
},
mangle: {
toplevel: true,
keep_fnames: false
},
format: {
comments: false,
semicolons: true
},
module: true,
sourceMap: true
});
// Minify with source maps
const withMaps = await swc.minify(code, {
sourceMap: true,
outputPath: "dist/bundle.js"
});
console.log(withMaps.map); // Source map stringMinifies JavaScript source code synchronously with identical interface to async version.
/**
* Minify JavaScript source code synchronously
* @param code - JavaScript source code string
* @param opts - Minification configuration options
* @returns Minified code output
*/
function minifySync(code: string, opts?: JsMinifyOptions): Output;Usage Examples:
import * as swc from "@swc/wasm";
// Synchronous minification
const result = swc.minifySync(`
const message = "Hello World";
console.log(message);
`, {
module: false,
compress: {
unused: true
}
});
console.log(result.code);
// Output: Compressed code
// Minify with custom formatting
const formatted = swc.minifySync(code, {
format: {
beautify: true,
comments: "some",
indentLevel: 2
}
});interface JsMinifyOptions {
/** Compression options */
compress?: TerserCompressOptions | boolean;
/** Output formatting options */
format?: JsFormatOptions & ToSnakeCaseProperties<JsFormatOptions>;
/** Name mangling options */
mangle?: TerserMangleOptions | boolean;
/** ECMAScript version target */
ecma?: TerserEcmaVersion;
/** Keep class names during minification */
keep_classnames?: boolean;
/** Keep function names during minification */
keep_fnames?: boolean;
/** Treat input as ES module */
module?: boolean;
/** Safari 10 compatibility mode */
safari10?: boolean;
/** Enable top-level minification */
toplevel?: boolean;
/** Generate source map */
sourceMap?: boolean;
/** Output path for source map URL */
outputPath?: string;
/** Inline sources content in source map */
inlineSourcesContent?: boolean;
}
type TerserEcmaVersion = 5 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | string | number;interface TerserCompressOptions {
/** Optimize function arguments */
arguments?: boolean;
/** Optimize arrow functions */
arrows?: boolean;
/** Optimize boolean expressions */
booleans?: boolean;
/** Convert booleans to integers */
booleans_as_integers?: boolean;
/** Collapse single-use variables */
collapse_vars?: boolean;
/** Optimize comparisons */
comparisons?: boolean;
/** Optimize computed properties */
computed_props?: boolean;
/** Optimize conditional expressions */
conditionals?: boolean;
/** Remove unreachable code */
dead_code?: boolean;
/** Apply default optimizations */
defaults?: boolean;
/** Remove directive prologues */
directives?: boolean;
/** Remove console.* calls */
drop_console?: boolean;
/** Remove debugger statements */
drop_debugger?: boolean;
/** ECMAScript version for optimizations */
ecma?: TerserEcmaVersion;
/** Evaluate constant expressions */
evaluate?: boolean;
/** Optimize expression sequences */
expression?: boolean;
/** Global variable definitions */
global_defs?: any;
/** Hoist function declarations */
hoist_funs?: boolean;
/** Hoist property accesses */
hoist_props?: boolean;
/** Hoist variable declarations */
hoist_vars?: boolean;
/** IE8 compatibility */
ie8?: boolean;
/** Optimize if-return patterns */
if_return?: boolean;
/** Function inlining level */
inline?: 0 | 1 | 2 | 3;
/** Join consecutive variable declarations */
join_vars?: boolean;
/** Keep class names */
keep_classnames?: boolean;
/** Keep function arguments */
keep_fargs?: boolean;
/** Keep function names */
keep_fnames?: boolean;
/** Keep Infinity literal */
keep_infinity?: boolean;
/** Optimize loops */
loops?: boolean;
/** Negate immediately invoked function expressions */
negate_iife?: boolean;
/** Number of optimization passes */
passes?: number;
/** Optimize property access */
properties?: boolean;
/** Assume getter functions are pure */
pure_getters?: any;
/** List of pure function names */
pure_funcs?: string[];
/** Reduce function calls */
reduce_funcs?: boolean;
/** Reduce variable assignments */
reduce_vars?: boolean;
/** Optimize sequences */
sequences?: any;
/** Remove code without side effects */
side_effects?: boolean;
/** Optimize switch statements */
switches?: boolean;
/** Variables to retain at top level */
top_retain?: any;
/** Enable top-level optimizations */
toplevel?: any;
/** Optimize typeof expressions */
typeofs?: boolean;
/** Enable unsafe optimizations */
unsafe?: boolean;
/** Unsafe optimization passes */
unsafe_passes?: boolean;
/** Unsafe arrow function optimizations */
unsafe_arrows?: boolean;
/** Unsafe comparison optimizations */
unsafe_comps?: boolean;
/** Unsafe function optimizations */
unsafe_function?: boolean;
/** Unsafe math optimizations */
unsafe_math?: boolean;
/** Unsafe symbol optimizations */
unsafe_symbols?: boolean;
/** Unsafe method optimizations */
unsafe_methods?: boolean;
/** Unsafe prototype optimizations */
unsafe_proto?: boolean;
/** Unsafe regex optimizations */
unsafe_regexp?: boolean;
/** Unsafe undefined optimizations */
unsafe_undefined?: boolean;
/** Remove unused variables */
unused?: boolean;
/** Treat as ES module */
module?: boolean;
}interface TerserMangleOptions {
/** Mangle property names */
props?: TerserManglePropertiesOptions;
/** Enable top-level mangling */
toplevel?: boolean;
/** Keep class names */
keep_classnames?: boolean;
/** Keep function names */
keep_fnames?: boolean;
/** Keep private property names */
keep_private_props?: boolean;
/** IE8 compatibility */
ie8?: boolean;
/** Safari 10 compatibility */
safari10?: boolean;
/** Reserved names to not mangle */
reserved?: string[];
}
interface TerserManglePropertiesOptions {
// Property mangling options (implementation-specific)
}interface JsFormatOptions {
/** ASCII-only output */
asciiOnly?: boolean;
/** Beautify output (format code) */
beautify?: boolean;
/** Force braces around single statements */
braces?: boolean;
/** Comment preservation strategy */
comments?: false | "some" | "all" | { regex: string };
/** ECMAScript version for output */
ecma?: TerserEcmaVersion;
/** Indentation level */
indentLevel?: number;
/** Starting indentation */
indentStart?: number;
/** Handle inline scripts */
inlineScript?: number;
/** Keep numeric precision */
keepNumbers?: number;
/** Keep quoted property names */
keepQuotedProps?: boolean;
/** Maximum line length */
maxLineLen?: number | false;
/** Code preamble */
preamble?: string;
/** Quote object keys */
quoteKeys?: boolean;
/** Quote style preference */
quoteStyle?: boolean;
/** Preserve function annotations */
preserveAnnotations?: boolean;
/** Safari 10 compatibility */
safari10?: boolean;
/** Force semicolons */
semicolons?: boolean;
/** Preserve shebang */
shebang?: boolean;
/** WebKit compatibility */
webkit?: boolean;
/** Wrap immediately invoked function expressions */
wrapIife?: boolean;
/** Wrap function arguments */
wrapFuncArgs?: boolean;
}interface Output {
/** Minified code */
code: string;
/** Source map (if enabled) */
map?: string;
/** Diagnostic messages */
diagnostics: string[];
}const production = await swc.minify(sourceCode, {
compress: {
// Remove unused code
dead_code: true,
unused: true,
// Remove development aids
drop_console: true,
drop_debugger: true,
// Aggressive optimizations
collapse_vars: true,
reduce_vars: true,
evaluate: true,
// Multiple passes for better results
passes: 2
},
mangle: {
// Mangle all names including top-level
toplevel: true,
// Keep some function names for debugging
keep_fnames: false,
keep_classnames: false
},
format: {
// Minimal output
comments: false,
semicolons: true,
beautify: false
},
ecma: 2018, // Modern browser target
module: true
});const development = await swc.minify(sourceCode, {
compress: {
// Basic optimizations only
dead_code: true,
unused: false, // Keep for debugging
// Keep development aids
drop_console: false,
drop_debugger: false
},
mangle: {
// Limited mangling
toplevel: false,
keep_fnames: true,
keep_classnames: true
},
format: {
// Readable output
comments: "some",
beautify: true,
indentLevel: 2
},
sourceMap: true // Enable source maps
});const library = await swc.minify(libraryCode, {
compress: {
// Conservative optimizations
dead_code: true,
unused: true,
reduce_vars: false, // May break external references
// Keep globals that may be used externally
toplevel: false
},
mangle: {
// Don't mangle top-level names (public API)
toplevel: false,
keep_fnames: true,
// Reserve public API names
reserved: ["MyLibrary", "publicMethod", "VERSION"]
},
format: {
comments: /^!/, // Keep license comments
semicolons: true
}
});Minification functions may fail for:
try {
const result = await swc.minify(code, options);
} catch (error) {
console.error("Minification error:", error.message);
}
// Check diagnostics for warnings
if (result.diagnostics.length > 0) {
console.warn("Minification warnings:", result.diagnostics);
}Install with Tessl CLI
npx tessl i tessl/npm-swc--wasm