swc
Compile JavaScript/TypeScript code to more compatible targets
TLDR
Transpile a specified input file and output to stdout
Transpile the input file every time it is changed
Transpile a specified input file and output to a specific file
Transpile a specified input directory and output to a specific directory
Transpile a specified input directory using a specific configuration file
Ignore files in a directory specified using glob path
SYNOPSIS
swc [options] <files...>
swc [options] <input-file> -o <output-file>
swc --help
PARAMETERS
-d, --out-dir <dir>
Compile all input files and emit them into the specified output directory, preserving the directory structure.
-o, --out-file <file>
Compile a single input file and emit the result to the specified output file. Not used with multiple input files.
-s, --source-maps [type]
Enable source map generation. 'type' can be true, inline, or both.
-w, --watch
Watch input files and directories for changes and recompile automatically.
--config <path>
Specify a path to an swc configuration file (e.g., .swcrc) to override the default lookup.
--minify
Minify the output code, reducing its size by removing whitespace, shortening variable names, etc.
--env-name <name>
Specify the environment name (e.g., 'development', 'production') to load environment-specific configurations from .swcrc.
--copy-files
When using --out-dir, copy non-compilable files (e.g., CSS, images) to the output directory.
--version
Display the current swc CLI version.
--help
Show usage instructions and available options.
DESCRIPTION
swc (Speedy Web Compiler) is an extremely fast, Rust-based toolkit for JavaScript and TypeScript. It provides core functionalities essential for modern web development, including transpilation (converting modern ECMAScript/TypeScript code to older versions compatible with various browsers or environments), minification (reducing code size for faster loading), and bundling.
Designed with performance in mind, swc is often used as a high-speed alternative to tools like Babel for transpilation or Terser for minification, significantly accelerating build times in large-scale projects. It processes code through a series of configurable plugins and can be extensively customized via a .swcrc configuration file. While primarily a library, its command-line interface via @swc/cli allows direct invocation for various development and build tasks.
CAVEATS
swc is not a standard Linux system command and must be installed, typically via npm or yarn as part of a Node.js project (e.g., npm install @swc/cli). It's primarily used in web development workflows for JavaScript and TypeScript compilation. Its configuration often relies on a .swcrc file located in the project root, which defines compilation targets, plugins, and other transformations.
CONFIGURATION FILE (.SWCRC)
swc heavily relies on a .swcrc JSON configuration file, usually located in the project root. This file specifies all compilation options, including presets (like @swc/preset-env for targeted environments) and plugins for various transformations (e.g., React JSX, TypeScript). Most complex configurations are managed through this file rather than command-line arguments.
INTEGRATION WITH BUILD TOOLS
While swc provides a CLI, it's very commonly integrated into larger build systems and frameworks. For instance, Next.js uses swc internally for fast compilation, and bundlers like Webpack or Rollup can use swc loaders/plugins to accelerate their JavaScript processing steps. This integration makes it a powerful backend for many modern web development toolchains.
HISTORY
swc was created by DongYoon Kang and is developed in Rust, a programming language known for its performance and memory safety. It emerged as a response to the need for faster build tools in the JavaScript ecosystem, aiming to provide a high-performance alternative to existing solutions like Babel, which are traditionally written in JavaScript. Its development focused on leveraging Rust's capabilities to deliver significantly faster compilation and minification times, quickly gaining traction in the modern web development community.