CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-swc--types

Comprehensive TypeScript type definitions for the SWC JavaScript/TypeScript transformation and minification APIs.

Pending
Overview
Eval results
Files

minification.mddocs/

Minification

Terser-compatible interfaces for JavaScript minification and code optimization. These types define comprehensive configuration options for reducing bundle sizes and optimizing JavaScript code while maintaining compatibility with Terser configurations.

Capabilities

Main Minification Options

Core minification configuration interface compatible with Terser options.

/**
 * JavaScript minification configuration
 */
interface JsMinifyOptions {
  /** Compression optimization 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;
  /** Module mode detection */
  module?: boolean | "unknown";
  /** Safari 10 compatibility */
  safari10?: boolean;
  /** Top-level scope processing */
  toplevel?: boolean;
  /** Generate source maps */
  sourceMap?: boolean;
  /** Output file path for source maps */
  outputPath?: string;
  /** Include source content in source maps */
  inlineSourcesContent?: boolean;
}

type TerserEcmaVersion = 5 | 2015 | 2016 | string | number;

Compression Options

Detailed compression optimization settings for dead code elimination and code transformation.

/**
 * Terser-compatible compression options
 */
interface TerserCompressOptions {
  /** Transform arguments references */
  arguments?: boolean;
  /** Convert arrow functions */
  arrows?: boolean;
  /** Optimize boolean contexts */
  booleans?: boolean;
  /** Convert booleans to integers where possible */
  booleans_as_integers?: boolean;
  /** Collapse single-use variables */
  collapse_vars?: boolean;
  /** Optimize comparisons */
  comparisons?: boolean;
  /** Optimize computed property access */
  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 optimization */
  ecma?: TerserEcmaVersion;
  /** Evaluate constant expressions */
  evaluate?: boolean;
  /** Parse as expression context */
  expression?: boolean;
  /** Global definitions for replacement */
  global_defs?: any;
  /** Hoist function declarations */
  hoist_funs?: boolean;
  /** Hoist properties */
  hoist_props?: boolean;
  /** Hoist var declarations */
  hoist_vars?: boolean;
  /** IE8 compatibility */
  ie8?: boolean;
  /** Optimize if-return and if-continue */
  if_return?: boolean;
  /** Inline function calls (0=disabled, 1=simple, 2=with arguments, 3=always) */
  inline?: 0 | 1 | 2 | 3;
  /** Join consecutive var statements */
  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;
  /** Treat getters as pure functions */
  pure_getters?: any;
  /** List of pure function names */
  pure_funcs?: string[];
  /** Reduce function calls */
  reduce_funcs?: boolean;
  /** Reduce variables */
  reduce_vars?: boolean;
  /** Optimize sequences */
  sequences?: any;
  /** Remove side-effect-free statements */
  side_effects?: boolean;
  /** Optimize switch statements */
  switches?: boolean;
  /** Top-level retention */
  top_retain?: any;
  /** Top-level optimization */
  toplevel?: any;
  /** Optimize typeof expressions */
  typeofs?: boolean;
  /** Enable unsafe optimizations */
  unsafe?: boolean;
  /** Allow unsafe passes */
  unsafe_passes?: boolean;
  /** Optimize arrow functions unsafely */
  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;
  /** Convert const to let */
  const_to_let?: boolean;
  /** Module mode */
  module?: boolean;
}

Name Mangling Options

Configuration for shortening variable and property names to reduce file size.

/**
 * Name mangling configuration
 */
interface TerserMangleOptions {
  /** Property mangling options */
  props?: TerserManglePropertiesOptions;
  /** Mangle names in top-level scope */
  topLevel?: boolean;
  /** Alias for topLevel (Terser compatibility) */
  toplevel?: boolean;
  /** Keep class names */
  keepClassNames?: boolean;
  /** Alias for keepClassNames (Terser compatibility) */
  keep_classnames?: boolean;
  /** Keep function names */
  keepFnNames?: boolean;
  /** Alias for keepFnNames (Terser compatibility) */
  keep_fnames?: boolean;
  /** Keep private property names */
  keepPrivateProps?: boolean;
  /** Alias for keepPrivateProps (Terser compatibility) */
  keep_private_props?: boolean;
  /** IE8 compatibility */
  ie8?: boolean;
  /** Safari 10 compatibility */
  safari10?: boolean;
  /** Reserved names that should not be mangled */
  reserved?: string[];
}

/**
 * Property mangling options (currently empty but extensible)
 */
interface TerserManglePropertiesOptions { }

Output Formatting Options

Comprehensive output formatting configuration for controlling code style and presentation.

/**
 * Output formatting options (mostly Terser-compatible)
 * Many options are currently no-op but exist for configuration compatibility
 */
interface JsFormatOptions {
  /** Convert unicode to ASCII (currently noop) */
  asciiOnly?: boolean;
  /** Beautify output (currently noop) */
  beautify?: boolean;
  /** Force braces around statements (currently noop) */
  braces?: boolean;
  /** Comment preservation strategy */
  comments?: false | "some" | "all" | { regex: string };
  /** ECMAScript version for output (currently noop) */
  ecma?: TerserEcmaVersion;
  /** Indentation level (currently noop) */
  indentLevel?: number;
  /** Starting indentation (currently noop) */
  indentStart?: number;
  /** Inline script compatibility (currently noop) */
  inlineScript?: boolean;
  /** Keep number formatting (currently noop) */
  keepNumbers?: number;
  /** Keep quoted property names (currently noop) */
  keepQuotedProps?: boolean;
  /** Maximum line length (currently noop) */
  maxLineLen?: number | false;
  /** Output preamble (currently noop) */
  preamble?: string;
  /** Quote object keys (currently noop) */
  quoteKeys?: boolean;
  /** Quote style preference (currently noop) */
  quoteStyle?: boolean;
  /** Preserve annotations (currently noop) */
  preserveAnnotations?: boolean;
  /** Safari 10 compatibility (currently noop) */  
  safari10?: boolean;
  /** Use semicolons (currently noop) */
  semicolons?: boolean;
  /** Keep shebang (currently noop) */
  shebang?: boolean;
  /** WebKit compatibility (currently noop) */
  webkit?: boolean;
  /** Wrap immediately invoked function expressions (currently noop) */
  wrapIife?: boolean;
  /** Wrap function arguments (currently noop) */
  wrapFuncArgs?: boolean;
}

Utility Type Helpers

Type utilities for case conversion and Terser compatibility.

/**
 * Convert camelCase string to snake_case
 * @example ToSnakeCase<'indentLevel'> == 'indent_level'
 */
type ToSnakeCase<T extends string> = T extends `${infer A}${infer B}`
  ? `${A extends Lowercase<A> ? A : `_${Lowercase<A>}`}${ToSnakeCase<B>}`
  : T;

/**
 * Convert object properties from camelCase to snake_case
 * @example ToSnakeCaseProperties<{indentLevel: 3}> == {indent_level: 3}
 */
type ToSnakeCaseProperties<T> = {
  [K in keyof T as K extends string ? ToSnakeCase<K> : K]: T[K];
};

Usage Examples:

import type { JsMinifyOptions, TerserCompressOptions, TerserMangleOptions } from "@swc/types";

// Basic minification setup
const basicMinify: JsMinifyOptions = {
  compress: true,
  mangle: true
};

// Advanced compression configuration
const advancedCompress: TerserCompressOptions = {
  dead_code: true,
  drop_console: true,
  drop_debugger: true,
  evaluate: true,
  collapse_vars: true,
  reduce_vars: true,
  pure_funcs: ["console.log", "console.warn"],
  passes: 2
};

// Detailed minification with custom settings
const productionMinify: JsMinifyOptions = {
  compress: {
    dead_code: true,
    drop_console: true,
    drop_debugger: true,
    collapse_vars: true,
    reduce_vars: true,
    evaluate: true,
    inline: 2,
    passes: 3,
    pure_funcs: [
      "console.log",
      "console.info", 
      "console.debug",
      "console.warn"
    ]
  },
  mangle: {
    topLevel: true,
    keepClassNames: false,
    keepFnNames: false,
    reserved: ["$", "jQuery", "React", "ReactDOM"]
  },
  format: {
    comments: false,
    semicolons: true
  },
  ecma: 2020,
  module: true,
  sourceMap: true
};

// Development-friendly minification  
const devMinify: JsMinifyOptions = {
  compress: {
    dead_code: true,
    drop_debugger: true,
    // Keep console logs in development
    drop_console: false
  },
  mangle: false, // Don't mangle names for debugging
  format: {
    comments: "some",
    beautify: true
  },
  sourceMap: true
};

// Library-specific minification (preserve public API)
const libraryMinify: JsMinifyOptions = {
  compress: {
    dead_code: true,
    evaluate: true,
    collapse_vars: true
  },
  mangle: {
    // Don't mangle top-level names (public API)
    topLevel: false,
    keepClassNames: true,
    keepFnNames: true
  },
  format: {
    comments: /^!/  // Keep license comments
  }
};

Install with Tessl CLI

npx tessl i tessl/npm-swc--types

docs

assumptions.md

ast-nodes.md

configuration.md

index.md

jsx.md

minification.md

modules.md

parser.md

typescript.md

tile.json