Communitygithub.com

G1Joshi/Agent-Skills

SWC 專家協助:高效能 Rust 驅動的 JavaScript/TypeScript 編譯、.swcrc 配置、透過 @swc/jest 進行 Jest 測試以及程式碼壓縮。適合用於以 SWC 取代 Babel 以加速建置、加快測試執行,或編譯現代 ECMAScript 功能。

Agent-Skills 是什麼?

此技能提供 SWC(快速網頁編譯器)的深入協助,這是一個基於 Rust 的高效能編譯平台,也是 Next.js 和 Deno 內部的編譯器。使用者可以在需要快速取代 Babel、加速 TypeScript/JSX 轉譯、透過 @swc/jest 加速 Jest 測試、使用 @swc/core API 進行程式化 AST 操作,或進行極速的 JS/CSS 壓縮時呼叫此技能。技能涵蓋了下列平台與工具:CLI(@swc/cli)、Node.js API(@swc/core)、測試框架(@swc/jest),以及支援這些工具的開發環境。其核心價值在於透過 Rust 編譯器提供高達 20 倍的轉譯速度提升,同時支援現代 ECMAScript 功能、React JSX 轉換、裝飾器語法,並可與 Webpack/Turbopack 等打包工具整合。技能中也包含了生產環境的 .swcrc 配置範例、常見的疑難排解情境(如解析錯誤、原生二進制安裝失敗),以及 2026 年的最佳實務建議,例如啟用 externalHelpers 減少執行時期 Helper 重複、與 tsc --noEmit 搭配以完成型別檢查、以及務必在壓縮時產生 Source Map 以利除錯。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/G1Joshi/Agent-Skills/tree/HEAD/skills/tools/swc

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

SWC

SWC(Speedy Web Compiler,快速網頁編譯器)是一個基於 Rust 的可擴展平台。它是 Next.js 和 Deno 內部的編譯器。

使用時機

  • 高效能 JavaScript/TypeScript 轉譯:以 Rust 編譯器取代 Babel,速度最高可提升 20 倍。
  • Next.js 與現代打包工具管線:轉譯 React JSX、TSX、現代 ECMAScript 功能以及 Webpack/Turbopack 的資產。
  • 程式化 AST 操作與程式碼生成:使用 @swc/core Node.js API 建構快速的客製化建置工具。
  • 生產環境資產壓縮:使用 @swc/core 壓縮器以極快速度壓縮 JS 和 CSS 套件。

快速入門

1. 最小化 .swcrc

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

2. 透過 CLI 編譯

npx @swc/cli src -d dist

核心概念

生產環境 SWC 配置(.swcrc)

配置現代 TypeScript、React 19 JSX 執行時期以及模組轉譯:

{
  "$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
}

使用 @swc/core API 進行程式化編譯

在 Node.js 腳本中非同步轉譯原始碼檔案:

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("轉譯後的程式碼:\n", output.code);
  if (output.map) {
    console.log("已產生 Source Map");
  }
}

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

高速 CLI 轉譯

直接使用 SWC 命令列編譯目錄樹:

# 將 src/ 轉譯至 dist/,同時產生 Source Map
npx swc src -d dist --copy-files --source-maps

# 使用 swcpack 轉譯並打包
npx swc src/index.ts -o dist/bundle.js --minify

常見模式

使用 @swc/jest 加速 Jest 測試

問題:使用 ts-jest 進行 TypeScript 轉譯會導致測試套件執行速度極慢。
解決方案:將 ts-jest 替換為 @swc/jest。

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

SWC 打包與壓縮管線

問題:需要為生產建置腳本提供快速的壓縮功能。
解決方案:在 .swcrc 中啟用 minify 選項。

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

最佳實務(2026 年)

  • 務必 安裝 @swc/helpers 並啟用 externalHelpers: true,以避免重複轉譯器執行時期 Helper 樁的程式碼。
  • 務必 在 CI/CD 管線中將 SWC 與 tsc --noEmit 搭配使用,因為 SWC 在轉譯程式碼時不會執行型別檢查。
  • 務必 將 target 配置為 "es2022" 或更新版本,以充分利用原生的現代瀏覽器功能。
  • 務必 使用 @swc/jest 或 @swc/register 大幅加快單元測試的執行速度。
  • 切勿 在非必要的情況下依賴 Babel 外掛;請檢查是否有原生 SWC 外掛(Wasm 外掛)可用。
  • 切勿 將 .swc 建置快取目錄提交至 Git 儲存庫。
  • 切勿 在沒有產生 Source Map 的情況下執行 SWC 壓縮,以便進行生產環境除錯。

疑難排解

錯誤 / 症狀原因解決方案
failed to handle: error parsing typescript使用 TypeScript 語法(例如 JSX 或裝飾器)但未在 .swcrc 中啟用對應標記確保 jsc.parser 中存在 "syntax": "typescript"、"tsx": true 和 "decorators": true。
裝飾器元資料未發出(Reflect.getMetadata 傳回 undefined)transform.legacyDecorator 和 transform.decoratorMetadata 未配置在 jsc.transform 中設定 "legacyDecorator": true 和 "decoratorMetadata": true。
Cannot find module '@swc/core-darwin-arm64'在 npm/yarn 安裝過程中,原生二進制檔案安裝失敗執行 npm install --force @swc/core 以觸發針對目標架構的原生二進制檔案下載。

參考資料

Individual skills in this repo

This repo contains 11 individual skills — each has its own dedicated page.

G1Joshi/Agent-Skills

Expert Dask distributed computing assistance covering Dask DataFrames, Arrays, Futures, and cluster scaling. Use when analyzing datasets too large for pandas on a single machine or multi-node cluster.

G1Joshi/Agent-Skills

Expert dbt (data build tool) assistance covering SQL modeling, Jinja macros, tests, documentation, and semantic layer. Use when building analytics engineering pipelines on BigQuery, Snowflake, or PostgreSQL.

G1Joshi/Agent-Skills

Expert JAX assistance covering Autograd, XLA compilation (`jit`), vectorization (`vmap`), and parallelization (`pmap`). Use when building high-performance numerical computing and cutting-edge deep learning research.

G1Joshi/Agent-Skills

Expert Ray distributed computing assistance covering Ray Core (actors, tasks), Ray Train, Ray Tune, and Ray Serve. Use when scaling Python compute and ML training across multi-node clusters.

G1Joshi/Agent-Skills

Expert Git version control assistance covering branching, interactive rebase, cherry-pick, submodules, worktrees, and conflict resolution. Use when managing source code history and collaboration workflows.

G1Joshi/Agent-Skills

Expert K9s CLI assistance covering terminal Kubernetes cluster navigation, real-time log streaming, port forwarding, and pod debugging. Use when managing and troubleshooting Kubernetes clusters with speed.

G1Joshi/Agent-Skills

Expert Tig assistance covering text-mode interface for Git, interactive staging, commit graph visualization, diff exploration, and blame navigation. Use when navigating Git commit history in the terminal, staging hunks interactively, browsing file changes, or reviewing revisions.

G1Joshi/Agent-Skills

Expert Vim assistance covering modal editing, .vimrc configuration, registers, macros, search/replace, and plugin management via vim-plug. Use when editing text efficiently in terminal environments, writing Vimscript, recording macros, or configuring core Vim settings.

G1Joshi/Agent-Skills

Expert Zed editor assistance covering high-performance Rust-based text editing, multi-buffer editing, language server protocols (LSP), and AI assistant integrations. Use when configuring Zed settings.json, setting up language extensions, collaborating in real-time channels, or optimizing editor startup speed.

G1Joshi/Agent-Skills

Expert Zsh assistance covering shell customization, Oh My Zsh plugins, Zinit plugin manager, prompt engineering (Starship/Powerlevel10k), and shell scripting. Use when configuring .zshrc, writing Zsh automation scripts, optimizing shell startup time, or configuring tab-completion.

G1Joshi/Agent-Skills

Expert [skill-name] assistance covering [feature 1], [feature 2], and [feature 3]. Use when [working with X], [debugging Y], or [implementing Z].

相關技能