tsconfig-swc
v1.0.0
Published
Translate a tsconfig JSON object into a swcrc JSON object.
Maintainers
Readme
tsconfig-swc
Translate a tsconfig JSON object into a swcrc JSON object with comprehensive mapping support.
- Comprehensive mapping: Supports most TypeScript compiler options
- JSX support: Full React JSX transformation configuration
- Decorators: Experimental decorators and metadata support
- Path mapping: baseUrl and paths configuration
- Source maps: Complete source map configuration
- Module systems: CommonJS, ES6, AMD, UMD support
- Type-safe: Full TypeScript type definitions
Installation
npm install tsconfig-swcUsage
import convert from "tsconfig-swc";
const tsconfig = {
compilerOptions: {
target: "ES2020",
module: "CommonJS",
jsx: "ReactJSX",
experimentalDecorators: true,
sourceMap: true,
},
};
const swcConfig = convert(tsconfig);
console.log(swcConfig); // SWC configuration objectConfiguration Mapping
This table shows how TypeScript compiler options map to SWC configuration options:
Core Compilation Options
| TSConfig Option | SWC Equivalent | Notes |
| --------------- | ------------------- | ---------------------------------------------------------- |
| target | jsc.target | Maps ES versions (ES5 → es5, ES2020 → es2020, etc.) |
| module | module.type | Maps module systems (CommonJS → commonjs, ES6 → es6, etc.) |
| lib | N/A | Handle via environment/polyfills, not SWC |
| allowJs | jsc.parser.syntax | Influences syntax detection |
| checkJs | N/A | TypeScript-only feature |
JSX Configuration
| TSConfig Option | SWC Equivalent | Notes |
| -------------------- | ----------------------------------------------- | ------------------------------------------------------ |
| jsx | jsc.parser.jsx, jsc.transform.react.runtime | Maps JSX modes (React → classic, ReactJSX → automatic) |
| jsxFactory | jsc.transform.react.pragma | React pragma function |
| jsxFragmentFactory | jsc.transform.react.pragmaFrag | React fragment pragma |
| jsxImportSource | jsc.transform.react.importSource | Import source for automatic runtime |
Decorators & Experimental Features
| TSConfig Option | SWC Equivalent | Notes |
| ------------------------- | -------------------------------------------------------- | ----------------------------------- |
| experimentalDecorators | jsc.parser.decorators, jsc.transform.legacyDecorator | Enables decorator support |
| emitDecoratorMetadata | jsc.transform.decoratorMetadata | Decorator metadata emission |
| useDefineForClassFields | jsc.transform.useDefineForClassFields | Class field initialization behavior |
Module Resolution & Paths
| TSConfig Option | SWC Equivalent | Notes |
| ------------------ | -------------- | ------------------------------ |
| baseUrl | jsc.baseUrl | Base URL for path resolution |
| paths | jsc.paths | Path mapping configuration |
| moduleResolution | N/A | Handled by bundler/environment |
| typeRoots | N/A | TypeScript-only feature |
| types | N/A | TypeScript-only feature |
Module Interop & Imports
| TSConfig Option | SWC Equivalent | Notes |
| ------------------------------ | ------------------ | ----------------------------------------------------------- |
| esModuleInterop | module.noInterop | Inverted mapping (esModuleInterop: false → noInterop: true) |
| allowSyntheticDefaultImports | N/A | Handled by module system |
| resolveJsonModule | N/A | Handled by bundler |
| preserveSymlinks | N/A | Handled by module resolver |
Source Maps & Output
| TSConfig Option | SWC Equivalent | Notes |
| ----------------- | ------------------------- | -------------------------------------------------------------------- |
| sourceMap | sourceMaps: true | External source maps |
| inlineSourceMap | sourceMaps: 'inline' | Inline source maps |
| inlineSources | inlineSourcesContent | Include source content in maps |
| removeComments | jsc.preserveAllComments | Inverted mapping (removeComments: false → preserveAllComments: true) |
Helpers & Optimization
| TSConfig Option | SWC Equivalent | Notes |
| -------------------- | ------------------------------------ | ----------------------------------- |
| importHelpers | jsc.externalHelpers | Use external helper functions |
| downlevelIteration | N/A | SWC handles iteration automatically |
| preserveConstEnums | jsc.transform.treatConstEnumAsEnum | Const enum behavior |
TypeScript-Only Options
These options are TypeScript compiler-specific and have no SWC equivalent:
| TSConfig Option | Notes |
| ---------------------------------- | --------------------------------------------------- |
| declaration | Type declaration generation (use tsc for this) |
| declarationMap | Declaration source maps (use tsc for this) |
| emitDeclarationOnly | Declaration-only compilation (use tsc for this) |
| outDir | Output directory (handled by build tools) |
| outFile | Single file output (use bundler) |
| rootDir | Root directory (handled by build tools) |
| noEmit | No emit mode (use tsc --noEmit for type checking) |
| strict | Type checking option, not compilation |
| noImplicitAny | Type checking option, not compilation |
| strictNullChecks | Type checking option, not compilation |
| strictFunctionTypes | Type checking option, not compilation |
| noImplicitReturns | Type checking option, not compilation |
| noFallthroughCasesInSwitch | Type checking option, not compilation |
| skipLibCheck | TypeScript-only optimization |
| forceConsistentCasingInFileNames | TypeScript-only file system check |
Examples
Node.js Project
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "CommonJS",
"strict": true,
"esModuleInterop": true,
"sourceMap": true,
"outDir": "dist"
}
}
// Generated .swcrc
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": { "syntax": "typescript" },
"target": "es2020"
},
"module": {
"type": "commonjs",
"strict": true
},
"sourceMaps": true
}React Application
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"jsx": "ReactJSX",
"jsxImportSource": "@emotion/react",
"strict": true,
"moduleResolution": "node"
}
}
// Generated .swcrc
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": {
"syntax": "typescript",
"jsx": true,
"tsx": true
},
"target": "es2020",
"transform": {
"react": {
"runtime": "automatic",
"importSource": "@emotion/react"
}
}
},
"module": {
"type": "es6"
}
}Library with Decorators
// tsconfig.json
{
"compilerOptions": {
"target": "ES2017",
"module": "UMD",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"useDefineForClassFields": false,
"declaration": true
}
}
// Generated .swcrc
{
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": {
"syntax": "typescript",
"decorators": true
},
"target": "es2017",
"transform": {
"legacyDecorator": true,
"decoratorMetadata": true,
"useDefineForClassFields": false
}
},
"module": {
"type": "umd"
}
}