Comprehensive TypeScript type definitions for the SWC JavaScript/TypeScript transformation and minification APIs.
—
Type definitions for various JavaScript module systems and their configuration options. These types support ES6 modules, CommonJS, UMD, AMD, Node.js, and SystemJS formats with comprehensive interoperability settings.
Core module system configuration supporting multiple output formats.
/**
* All supported module system configurations
*/
type ModuleConfig =
| Es6Config | CommonJsConfig | UmdConfig
| AmdConfig | NodeNextConfig | SystemjsConfig;
/**
* Base configuration shared across module systems
*/
interface BaseModuleConfig {
/** Non-enumerable __esModule property control */
strict?: boolean;
/** Emit 'use strict' directive */
strictMode?: boolean;
/** Lazy evaluation of imports */
lazy?: boolean | string[];
/** Import interoperability mode */
importInterop?: "swc" | "babel" | "node" | "none";
/** Output file extension */
outFileExtension?: "js" | "mjs" | "cjs";
/** Emit cjs-module-lexer annotations */
exportInteropAnnotation?: boolean;
/** Preserve dynamic imports */
ignoreDynamic?: boolean;
/** Allow top-level this */
allowTopLevelThis?: boolean;
/** Preserve import.meta */
preserveImportMeta?: boolean;
/** Resolve .mjs fully */
resolveFully?: boolean;
/** Deprecated: use importInterop instead */
noInterop?: boolean;
}ECMAScript 2015+ module configuration for modern JavaScript environments.
/**
* ES6/ES2015+ module configuration
*/
interface Es6Config extends BaseModuleConfig {
type: "es6";
}Usage Examples:
import type { Es6Config } from "@swc/types";
// Modern ES6 module output
const es6Config: Es6Config = {
type: "es6",
strict: true,
strictMode: true
};
// ES6 with lazy loading for better performance
const es6LazyConfig: Es6Config = {
type: "es6",
lazy: true, // Lazy-load dependencies, not local imports
outFileExtension: "js"
};CommonJS module system configuration for Node.js environments.
/**
* CommonJS module configuration
*/
interface CommonJsConfig extends BaseModuleConfig {
type: "commonjs";
}Usage Examples:
import type { CommonJsConfig } from "@swc/types";
// Standard CommonJS output
const cjsConfig: CommonJsConfig = {
type: "commonjs",
importInterop: "swc", // Use SWC's interop helpers
strictMode: true
};
// Node.js native interop
const nodeConfig: CommonJsConfig = {
type: "commonjs",
importInterop: "node", // Use Node.js native import behavior
outFileExtension: "cjs",
exportInteropAnnotation: true // Emit cjs-module-lexer annotations
};
// Legacy compatibility
const legacyConfig: CommonJsConfig = {
type: "commonjs",
importInterop: "none", // No interop helpers
strict: false
};UMD configuration for libraries that need to work in multiple environments.
/**
* UMD module configuration
*/
interface UmdConfig extends BaseModuleConfig {
type: "umd";
/** Global variable mappings for dependencies */
globals?: { [key: string]: string };
}Usage Examples:
import type { UmdConfig } from "@swc/types";
// Library UMD build
const umdConfig: UmdConfig = {
type: "umd",
globals: {
"react": "React",
"react-dom": "ReactDOM",
"lodash": "_"
},
strictMode: true
};
// UMD with no external dependencies
const standaloneUmd: UmdConfig = {
type: "umd",
strict: true
};AMD module configuration for RequireJS and similar loaders.
/**
* AMD module configuration
*/
interface AmdConfig extends BaseModuleConfig {
type: "amd";
/** Module ID for the AMD module */
moduleId?: string;
}Usage Examples:
import type { AmdConfig } from "@swc/types";
// AMD with explicit module ID
const amdConfig: AmdConfig = {
type: "amd",
moduleId: "my-library",
strictMode: true
};
// Anonymous AMD module
const anonymousAmd: AmdConfig = {
type: "amd"
};Node.js module configuration with modern Node.js features.
/**
* Node.js modern module configuration
*/
interface NodeNextConfig extends BaseModuleConfig {
type: "nodenext";
}Usage Examples:
import type { NodeNextConfig } from "@swc/types";
// Modern Node.js with ES modules
const nodeNextConfig: NodeNextConfig = {
type: "nodenext",
outFileExtension: "mjs",
preserveImportMeta: true,
allowTopLevelThis: false
};SystemJS module configuration for dynamic module loading.
/**
* SystemJS module configuration
*/
interface SystemjsConfig {
type: "systemjs";
/** Allow top-level this in modules */
allowTopLevelThis?: boolean;
}Usage Examples:
import type { SystemjsConfig } from "@swc/types";
// SystemJS for dynamic loading
const systemConfig: SystemjsConfig = {
type: "systemjs",
allowTopLevelThis: true
};Detailed configuration for how different module systems interact.
/**
* Import interoperability strategies
*/
type ImportInterop = "swc" | "babel" | "node" | "none";SWC/Babel Interop ("swc" | "babel"):
__esModule property to exports_interop_require_default helper for default importsNode.js Interop ("node"):
module.exports value__esModule property checkingNo Interop ("none"):
Lazy Loading Configuration:
// Lazy loading examples
const lazyConfig = {
// Don't lazy-load any imports
lazy: false,
// Lazy-load dependencies but not local imports
lazy: true,
// Lazy-load specific modules
lazy: ["lodash", "moment", "react-router"]
};Complete Module Configuration Examples:
import type { ModuleConfig, CommonJsConfig, Es6Config } from "@swc/types";
// Production ES6 build
const productionEs6: Es6Config = {
type: "es6",
strict: true,
strictMode: true,
outFileExtension: "js",
lazy: ["lodash", "moment"] // Lazy-load heavy dependencies
};
// Node.js library build
const nodeLibrary: CommonJsConfig = {
type: "commonjs",
importInterop: "node",
outFileExtension: "cjs",
exportInteropAnnotation: true,
strictMode: true,
allowTopLevelThis: false
};
// Universal library build
const universalLibrary: UmdConfig = {
type: "umd",
globals: {
"react": "React",
"@babel/runtime": "babelRuntime"
},
strict: true,
strictMode: true
};
// Development build with better debugging
const development: Es6Config = {
type: "es6",
strict: false, // More lenient for development
preserveImportMeta: true,
ignoreDynamic: true // Don't transform dynamic imports
};Install with Tessl CLI
npx tessl i tessl/npm-swc--types