Swc

From G1Joshi/Agent-Skills · View interactive version

Swc is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert SWC assistance covering high-performance Rust-based JavaScript/TypeScript compilation, .swcrc configuration, Jest testing via @swc/jest, and minification. Use when replacing Babel with SWC for faster builds, accelerating test execution, or compiling modern ECMAScript features.

How to use this skill

Skill file

SWC

SWC (Speedy Web Compiler) is a Rust-based extensible platform. It is the compiler inside Next.js and Deno.

When to Use

Quick Start

1. Minimal .swcrc

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true
    },
    "transform": {
      "react": {
        "runtime": "automatic"
      }
    },
    "target": "es2022"
  },
  "minify": false
}

2. Compile via CLI

npx @swc/cli src -d dist

Core Concepts

Production SWC Configuration (`.swcrc`)

Configuring modern TypeScript, React 19 JSX runtime, and module transpilation:

{
  "$schema": "https://json.schemastore.org/swcrc",
  "jsc": {
    "parser": {
      "syntax": "typescript",
      "tsx": true,
      "decorators": true,
      "dynamicImport": true
    },
    "transform": {
      "react": {
        "runtime": "automatic",
        "refresh": true
      }
    },
    "target": "es2022",
    "loose": false,
    "externalHelpers": true,
    "keepClassNames": true,
    "minify": {
      "compress": {
        "unused": true,
        "drop_console": true
      },
      "mangle": true
    }
  },
  "module": {
    "type": "es6"
  },
  "minify": true,
  "sourceMaps": true
}

Programmatic Compilation with `@swc/core` API

Transpiling source files asynchronously in Node.js scripts:

import swc from "@swc/core";
import fs from "node:fs/promises";

async function compileTypeScript(filePath) {
  const code = await fs.readFile(filePath, "utf-8");

  const output = await swc.transform(code, {
    filename: filePath,
    sourceMaps: true,
    jsc": {
      parser: { syntax: "typescript", tsx: true },
      target: "es2022",
    },
    module: { type: "commonjs" },
  });

  console.log("Transpiled Code:\n", output.code);
  if (output.map) {
    console.log("Source Map Generated");
  }
}

compileTypeScript("./src/index.ts");

High-Speed CLI Transpilation

Compiling directory trees directly with the SWC command line:

# Transpile src/ to dist/ with source maps
npx swc src -d dist --copy-files --source-maps

# Transpile and bundle with swcpack
npx swc src/index.ts -o dist/bundle.js --minify

Common Patterns

Accelerating Jest Tests with @swc/jest

Problem: TypeScript transpilation with ts-jest makes test suites run very slowly.

Solution: Swap ts-jest for @swc/jest.

// jest.config.js
module.exports = {
  transform: {
    "^.+\.(t|j)sx?$": [
      "@swc/jest",
      {
        jsc: {
          transform: {
            react: { runtime: "automatic" },
          },
        },
      },
    ],
  },
};

SWC Bundling & Minification Pipeline

Problem: Need fast minification for production build scripts.

Solution: Enable minify options inside .swcrc.

{
  "minify": true,
  "jsc": {
    "minify": {
      "compress": {
        "drop_console": true
      },
      "mangle": true
    }
  }
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------- |

| failed to handle: error parsing typescript | TypeScript syntax (e.g. JSX or decorators) used without enabling flags in .swcrc | Ensure "syntax": "typescript", "tsx": true, and "decorators": true are present in jsc.parser. |

| Decorators metadata not emitting (Reflect.getMetadata returns undefined) | transform.legacyDecorator and transform.decoratorMetadata not configured | Set "legacyDecorator": true and "decoratorMetadata": true inside jsc.transform. |

| Cannot find module '@swc/core-darwin-arm64' | Native binary install failed during npm/yarn install | Run npm install --force @swc/core to trigger native binary fetch for target architecture. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.