$ show swc-project/swc-node
swc-node is a monorepo of tools that enable fast TypeScript/JavaScript transformation and execution using the SWC compiler. It provides several packages: `@swc-node/core` wraps SWC's native transform functions with sensible defaults and configuration helpers; `@swc-node/register` hooks into Node.js's module system (both CommonJS via pirates and ESM via loader hooks) to transpile TypeScript on the fly; `@swc-node/jest` is a high-performance Jest transformer; `@swc-node/loader` is a Webpack loader; and `@swc-node/sourcemap-support` provides runtime source map resolution. The codebase is well-architected with clear separation of concerns, extensive caching (in-memory and on-disk), and careful handling of source maps. It is actively maintained with frequent releases and a large user base (1982 stars).
The LLM identified 4 files worth highlighting for reuse:
Provides `transformSync`, `transform`, and `transformJest` functions that normalize TypeScript compiler options into SWC's configuration. The `transformOption` function builds SWC options with sensible defaults (ES2018 target, CommonJS module, inline sourcemaps for Jest, etc.) while allowing overrides. This is a clean, reusable abstraction for any tool that needs to drive SWC transforms from TypeScript config.
Implements a two-level transform cache (in-memory LRU + on-disk JSON files) keyed by source content, filename, compiler options, and runtime salt. Uses xxh64 for fast hashing, atomic writes via temp-file-and-rename to avoid partial cache entries, and configurable memory limits. Exposes `getCachedTransform`, `setCachedTransform`, `createCacheKey`, and utility functions to clear or inspect cache. This pattern is directly reusable in any build tool or loader that wants to cache expensive transform results across processes.
Reads a `tsconfig.json` file (with `extends` support), parses it with TypeScript's API, and converts the compiler options into SWC-compatible options. Handles target mapping (e.g., ES2024 -> 'es2024'), module kind mapping, JSX config, decorators, paths with baseUrl, and source map modes. The `tsCompilerOptionsToSwcConfig` function is a self-contained, well-tested utility that can be extracted and used by any project that needs to bridge TypeScript configuration to SWC.
Shared logic for the synchronous and asynchronous ESM loader hooks. Defines `planResolve` and `planTransform` functions that classify a module resolution or load request into an action plan (result, next, entrypoint, resolve, passthrough, compile). This separation of classification from IO allows the synchronous and asynchronous hooks to share the same routing logic. Also provides helpers for package.json traversal, format detection, and builtin resolution. The plan-based architecture is a clean, testable pattern for custom loaders.