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

configuration.mddocs/

Configuration

Core configuration interfaces for SWC compilation, parsing, and transformation. These types define the complete configuration API for integrating with SWC.

Capabilities

Main Options Interface

The primary programmatic options interface extending the base Config interface.

/**
 * Programmatic options for SWC compilation
 */
interface Options extends Config {
  /** If true, a file is parsed as a script instead of module */
  script?: boolean;
  /** Working directory for resolving relative paths */
  cwd?: string;
  /** Filename associated with the code being compiled */
  filename?: string;
  /** Project root directory for configuration resolution */
  root?: string;
  /** How SWC chooses its project root */
  rootMode?: "root" | "upward" | "upward-optional";
  /** Current active environment for configuration loading */
  envName?: string;
  /** Path to configuration file or boolean to disable */
  configFile?: string | boolean;
  /** Enable searching for .swcrc files */
  swcrc?: boolean;
  /** Packages considered as "root" for .swcrc loading */
  swcrcRoots?: boolean | MatchPattern | MatchPattern[];
  /** Input source map configuration */
  inputSourceMap?: boolean | string;
  /** Source file name for source maps */
  sourceFileName?: string;
  /** Source root for generated source maps */
  sourceRoot?: string;
  /** Transform plugin */
  plugin?: Plugin;
  /** Module type detection */
  isModule?: boolean | "unknown" | "commonjs";
  /** Output path for source map fixes */
  outputPath?: string;
}

interface CallerOptions {
  name: string;
  [key: string]: any;
}

/**
 * Pattern matching interface for file filtering
 */
interface MatchPattern { }

Base Configuration Interface

The core .swcrc configuration interface used for file-based configuration.

/**
 * .swcrc configuration interface
 */
interface Config {
  /** File matching patterns (regex syntax) */
  test?: string | string[];
  /** File exclusion patterns (regex syntax) */
  exclude?: string | string[];
  /** Environment-specific configuration */
  env?: EnvConfig;
  /** JavaScript compilation configuration */
  jsc?: JscConfig;
  /** Module system configuration */
  module?: ModuleConfig;
  /** Enable minification */
  minify?: boolean;
  /** Source map generation */
  sourceMaps?: boolean | "inline";
  /** Include source content in source maps */
  inlineSourcesContent?: boolean;
}

type Swcrc = Config | Config[];

JavaScript Compilation Configuration

JavaScript-specific compilation settings including parsing, transformation, and optimization.

interface JscConfig {
  /** Babel-compatible optimization assumptions */
  assumptions?: Assumptions;
  /** Loose transformation mode */
  loose?: boolean;
  /** Parser configuration */
  parser?: ParserConfig;
  /** Transform configuration */
  transform?: TransformConfig;
  /** Use @swc/helpers instead of inline helpers */
  externalHelpers?: boolean;
  /** ECMAScript target version */
  target?: JscTarget;
  /** Keep class names during compilation */
  keepClassNames?: boolean;
  /** Experimental features */
  experimental?: ExperimentalConfig;
  /** Base URL for module resolution */
  baseUrl?: string;
  /** Path mapping configuration */
  paths?: { [from: string]: string[] };
  /** Minification options */
  minify?: JsMinifyOptions;
  /** Preserve all comments */
  preserveAllComments?: boolean;
  /** Output character encoding */
  output?: { charset?: 'utf8' | 'ascii' };
}

interface ExperimentalConfig {
  /** Optimize hygiene */
  optimizeHygiene?: boolean;
  /** Keep import assertions */
  keepImportAssertions?: boolean;
  /** Emit assert for import attributes */
  emitAssertForImportAttributes?: boolean;
  /** Cache root directory */
  cacheRoot?: string;
  /** WebAssembly plugins */
  plugins?: WasmPlugin[];
  /** Run WASM plugins before TypeScript stripping */
  runPluginFirst?: boolean;
  /** Disable builtin transforms for testing */
  disableBuiltinTransformsForInternalTesting?: boolean;
  /** Emit isolated .d.ts files */
  emitIsolatedDts?: boolean;
  /** Disable all lint rules */
  disableAllLints?: boolean;
}

Environment Configuration

Babel-preset-env compatible configuration for environment-based transformations.

/**
 * Configuration ported from babel-preset-env
 */
interface EnvConfig {
  /** Polyfill mode */
  mode?: "usage" | "entry";
  /** Debug output */
  debug?: boolean;
  /** Enable dynamic import support */
  dynamicImport?: boolean;
  /** Loose transformation mode */
  loose?: boolean;
  /** Transpile broken syntax to modern syntax */
  bugfixes?: boolean;
  /** Skip ES features */
  skip?: string[];
  /** Include specific features */
  include?: string[];
  /** Exclude specific features */
  exclude?: string[];
  /** Core-js version */
  coreJs?: string;
  /** Target environments */
  targets?: any;
  /** Path to polyfills */
  path?: string;
  /** Enable shipped proposals */
  shippedProposals?: boolean;
  /** Force all transforms */
  forceAllTransforms?: boolean;
}

Transform Configuration

Configuration for various transformation passes including React, decorators, and TypeScript.

/**
 * Options for transform passes
 */
interface TransformConfig {
  /** React/JSX transformation */
  react?: ReactConfig;
  /** Constant modules configuration */
  constModules?: ConstModulesConfig;
  /** Optimizer configuration */
  optimizer?: OptimizerConfig;
  /** Legacy decorator transformation */
  legacyDecorator?: boolean;
  /** Decorator metadata emission */
  decoratorMetadata?: boolean;
  /** Decorator version */
  decoratorVersion?: "2021-12" | "2022-03";
  /** Treat const enum as enum */
  treatConstEnumAsEnum?: boolean;
  /** Use defineProperty for class fields */
  useDefineForClassFields?: boolean;
  /** Verbatim module syntax */
  verbatimModuleSyntax?: boolean;
  /** TypeScript enum mutability */
  tsEnumIsMutable?: boolean;
}

interface ReactConfig {
  /** JSX pragma function */
  pragma?: string;
  /** JSX fragment pragma */
  pragmaFrag?: string;
  /** Throw on XML namespaced tags */
  throwIfNamespace?: boolean;
  /** Development mode */
  development?: boolean;
  /** Use Object.assign instead of _extends */
  useBuiltins?: boolean;
  /** Fast refresh configuration */
  refresh?: boolean | RefreshConfig;
  /** JSX runtime */
  runtime?: "automatic" | "classic";
  /** Import source for jsx/jsxs factories */
  importSource?: string;
}

interface RefreshConfig {
  refreshReg?: string;
  refreshSig?: string;
  emitFullSignatures?: boolean;
}

Optimization Configuration

Configuration for SWC's optimization passes.

interface OptimizerConfig {
  /** Simplify expressions */
  simplify?: boolean;
  /** Global variable inlining */
  globals?: GlobalPassOption;
  /** JSON minification */
  jsonify?: { minCost: number };
}

/**
 * Options for inline-global pass
 */
interface GlobalPassOption {
  /** Global variables to inline with values */
  vars?: Record<string, string>;
  /** Environment variables to inline */
  envs?: string[] | Record<string, string>;
  /** typeof replacements */
  typeofs?: Record<string, string>;
}

/**
 * Constant modules configuration for Ember-style imports
 */
interface ConstModulesConfig {
  globals?: {
    [module: string]: {
      [name: string]: string;
    };
  };
}

Plugin Interface

Interface for SWC transform plugins.

/**
 * Transform plugin interface
 */
interface Plugin {
  (module: Program): Program;
}

type WasmPlugin = [wasmPackage: string, config: Record<string, any>];

interface WasmAnalysisOptions {
  parser?: ParserConfig;
  module?: true | false | "unknown";
  filename?: string;
  errorFormat?: "json" | "normal";
  cacheRoot?: string;
  plugins: WasmPlugin[];
}

Usage Examples:

import type { Options, JscConfig } from "@swc/types";

// Basic TypeScript compilation
const basicConfig: Options = {
  jsc: {
    parser: {
      syntax: "typescript",
      tsx: true
    },
    target: "es2020"
  },
  module: {
    type: "es6"
  }
};

// Advanced configuration with optimization
const advancedConfig: Options = {
  filename: "src/app.tsx",
  jsc: {
    parser: {
      syntax: "typescript",
      tsx: true,
      decorators: true
    },
    target: "es2020",
    transform: {
      react: {
        runtime: "automatic",
        development: process.env.NODE_ENV === "development"
      },
      optimizer: {
        simplify: true,
        globals: {
          vars: {
            __DEV__: "false"
          }
        }
      }
    },
    minify: {
      compress: {
        dead_code: true,
        drop_console: true
      },
      mangle: true
    }
  },
  module: {
    type: "es6"
  },
  sourceMaps: true
};

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