--- url: /docs/getting-started.md --- # Getting Started ## Installation The easiest way to try SWC is using the [Playground](/playground/index.md). Otherwise, run the following to download pre-built binaries: ```sh [npm] npm install -D @swc/cli @swc/core ``` ```sh [yarn] yarn add -D @swc/cli @swc/core ``` ```sh [pnpm] pnpm add -D @swc/cli @swc/core ``` ```sh [bun] bun add -D @swc/cli @swc/core ``` ```sh [deno] deno add -D npm:@swc/cli npm:@swc/core ``` Then, you can transpile your first file and emit to `stdout`: ```bash npx swc ./file.js ``` ## Supported Binaries SWC can be downloaded and used as a pre-built binary, or built from source. Currently, the following binaries are provided: - Mac (Apple Silicon) - Mac (x64) - Linux (x86\_64) - Linux (aarch64) - Linux (armv7) - Alpine Linux (also install `@swc/core-linux-musl`) - Android (aarch64) - Windows (win32-x64) - Windows (ia32) --- url: /docs/usage/cli.md --- # @swc/cli ## Usage Run the following to download pre-built binaries: ```sh [npm] npm install -D @swc/cli @swc/core ``` ```sh [yarn] yarn add -D @swc/cli @swc/core ``` ```sh [pnpm] pnpm add -D @swc/cli @swc/core ``` ```sh [bun] bun add -D @swc/cli @swc/core ``` ```sh [deno] deno add -D npm:@swc/cli npm:@swc/core ``` Then, you can transpile your files: ```sh # Transpile one file and emit to stdout npx swc ./file.js # Transpile one file and emit to `output.js` npx swc ./file.js -o output.js # Transpile and write to /output dir npx swc ./my-dir -d output ``` ## Flow input To compile Flow code with the CLI, point `swc` at a `.swcrc` file that sets `jsc.parser.syntax` to `"flow"`: ```json title=".swcrc" { "jsc": { "parser": { "syntax": "flow" } } } ``` ```sh npx swc input.js --config-file .swcrc -o output.js ``` For JSX-flavored Flow files, set `jsx: true` and compile `.jsx` inputs with the same config. See the [Flow guide](/docs/usage/flow.md) for the full list of parser options. ## Options ### `--filename` (-f) Filename to use when reading from stdin. This will be used in source maps and errors. ```sh npx swc -f input.js ``` ### `--config-file` Path to a `.swcrc` file to use. ```sh npx swc input.js --config-file .swcrc ``` ### `--env-name` The name of the 'env' to use when loading configs and plugins. Defaults to the value of `SWC_ENV`, or else `NODE_ENV`, or else `development`. ```sh npx swc input.js --env-name='test' ``` ### `--no-swcrc` Whether or not to look up `.swcrc` files. ```sh npx swc input.js --no-swcrc ``` ### `--ignore` List of glob paths to **not** compile. ```sh npx swc src --ignore **/*.test.js ``` ### `--only` List of glob paths to **only** compile Example: ```sh npx swc src --only **/*.js ``` ### `--watch` (-w) To automatically recompile files on changes, install `chokidar`: ```sh [npm] npm install -D chokidar ``` ```sh [yarn] yarn add -D chokidar ``` ```sh [pnpm] pnpm add -D chokidar ``` ```sh [bun] bun add -D chokidar ``` ```sh [deno] deno add -D npm:chokidar ``` Then, add the `-w` flag: ```sh npx swc input.js -w ``` ### `--quiet` (-q) Suppress compilation output. ```sh npx swc input.js -q ``` ### `--source-maps` (-s) Values: `true|false|inline|both` ```sh npx swc input.js -s ``` ### `--source-map-target` Define the `file` for the source map. ```sh npx swc input.js -s --source-map-target input.map.js ``` ### `--source-file-name` Set `sources[0]` on returned source map ### `--source-root` The root from which all sources are relative. ### `--out-file` (-o) Compile all input files into a single file. ```sh npx swc input.js -o output.js ``` ### `--out-dir` (-d) Compile an input directory of modules into an output directory. ```sh npx swc src -d dist ``` ### `--copy-files` (-D) When compiling a directory, copy over non-compilable files. ```sh npx swc src --copy-files ``` ### `--include-dotfiles` Include dotfiles when compiling and copying non-compilable files. ```sh npx swc src --include-dotfiles ``` ### `--config` (-C) Override a config from `.swcrc` file. ```sh npx swc src -C module.type=amd -C module.moduleId=hello ``` ### `--sync` Invoke swc synchronously. Useful for debugging. ```sh npx swc src --sync ``` ### `--log-watch-compilation` Log a message when a watched file is successfully compiled. ```sh npx swc input.js --log-watch-compilation ``` ### `--extensions` Use specific extensions. ### `--strip-leading-paths` Remove the leading directory (including all parent relative paths) when building the final output path. As an example it compiles all modules under `src` folder to `dist` folder, without create the `src` folder inside of `dist`. ```sh npx swc src -d dist --strip-leading-paths ``` ## use in nodejs script ```js const { swcDir } = require('@swc/cli'); const swcOptions = { jsc: { target: 'esnext', externalHelpers: true, }, module: { type: 'commonjs', }, sourceMaps: true, }; swcDir({ cliOptions: { outDir: './dist', watch: true, filenames: ['./src'], extensions: ['.ts'], stripLeadingPaths: true, }, swcOptions, callbacks: { onSuccess: e => { console.log(e); }, onFail: e => { console.log(e); }, onWatchReady: () => {}, }, }); ``` Please note that when using `callbacks`, the `--quiet (-q)` will always be true. ### `--out-file-extension` Use a specific extension for the output files. As an example, if you want to render your es6 output to `.mjs` file extensions, you could call: ```shell npx swc input.js --out-file-extension mjs ``` Please note that if you are compiling multiple files, you will also need to make sure that your imports are resolved with the appropriate extension. You can do so by making use of the `resolveFully` and `outFileExtension` module options: ```json { "module": { "resolveFully": true, "outFileExtension": "mjs" } } ``` --- url: /docs/usage/core.md --- # @swc/core These are the core SWC APIs mainly useful for build tool authors. ## transform `@swc/core` provides appropriate `.d.ts` file, so you may not need this. Returns `Promise<{ code: string, map?: string }>` ```ts twoslash import * as swc from "@swc/core"; swc .transform("source code", { // Some options cannot be specified in .swcrc filename: "input.js", sourceMaps: true, // Input files are treated as module by default. isModule: false, // All options below can be configured via .swcrc jsc: { parser: { syntax: "ecmascript", }, transform: {}, }, }) .then((output) => { output.code; // transformed code output.map; // source map (in string) }); ``` ### Flow input To transform Flow code, set `jsc.parser.syntax` to `"flow"`. SWC will parse Flow syntax and strip type-only constructs as part of the transform pipeline. ```ts twoslash import * as swc from "@swc/core"; swc.transform("const value: number = 1;", { jsc: { parser: { syntax: "flow", }, }, }); ``` See the [Flow guide](/docs/usage/flow.md) for `.swcrc`, CLI, and parser options. ### transformSync Returns `{ code: string, map?: string }` ### transformFile Returns `Promise<{ code: string, map?: string }>` ### transformFileSync Returns `{ code: string, map?: string }` ## parse Returns `Promise