Gathering detailed insights and metrics for rollup-plugin-swc3
Gathering detailed insights and metrics for rollup-plugin-swc3
Gathering detailed insights and metrics for rollup-plugin-swc3
Gathering detailed insights and metrics for rollup-plugin-swc3
Use SWC with Rollup to transform / minify ESNext and TypeScript code.
npm install rollup-plugin-swc3
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
158 Stars
166 Commits
15 Forks
3 Watching
12 Branches
13 Contributors
Updated on 25 Nov 2024
TypeScript (99.45%)
JavaScript (0.55%)
Cumulative downloads
Total Downloads
Last day
-4.7%
7,272
Compared to previous day
Last week
0.2%
38,771
Compared to previous week
Last month
9.6%
159,642
Compared to previous month
Last year
209.5%
1,601,610
Compared to previous year
5
26
SWC is an extensible Rust-based platform for the next generation of fast developer tools. This plugin is designed to replace rollup-plugin-typescript2
, @rollup/plugin-typescript
, @rollup/plugin-babel
and rollup-plugin-terser
for you.
New: Building library for React Server Component support is added in 0.9.0
! 'use client'
and 'use server'
directives now are handled properly, without triggering rollup warnings. Start using 'use client'
and 'use server'
in your library by adding two lines in your rollup.config.js
Since
0.9.1
the support for'use client'
and'use server'
has been separated into a standalone rollup pluginrollup-preserve-directives
, the previouspreserveUseDirective
named export is retained for the backward compatibility.
sukkaw/rollup-plugin-swc | mentaljam/rollup-plugin-swc | nicholasxjy/rollup-plugin-swc2 | @rollup/plugin-swc | |
---|---|---|---|---|
minify your bundle in one pass1 | ✅ | 🛑 | 🛑 | 🛑 |
Standalone swcMinify plugin | ✅ | 🛑 | 🛑 | 🛑 |
Config Intellisense2 | ✅ | 🛑 | 🛑 | 🛑 |
Reads your tsconfig.json and jsconfig.json | ✅ | 🛑 | 🛑 | 🛑 |
ESM export | ✅ | 🟡3 | 🛑 | ✅ |
TypeScript declarations | ✅ | ✅ | ✅ | ✅ |
Has testing | ✅ | 🛑 | 🛑 | ✅ |
1$ npm i @swc/core rollup-plugin-swc3 2# If you prefer yarn 3# yarn add @swc/core rollup-plugin-swc3 4# If you prefer pnpm 5# pnpm add @swc/core rollup-plugin-swc3
1// rollup.config.js 2import { swc } from 'rollup-plugin-swc3'; 3 4export default { 5 input: 'xxxx', 6 output: {}, 7 plugins: [ 8 swc({ 9 // All options are optional 10 include: /\.[mc]?[jt]sx?$/, // default 11 exclude: /node_modules/, // default 12 tsconfig: 'tsconfig.json', // default 13 // tsconfig: false, // You can also prevent `rollup-plugin-swc` from reading tsconfig.json, see below 14 // And add your swc configuration here! 15 // "filename" will be ignored since it is handled by rollup 16 jsc: {} 17 }), 18 ]; 19}
If you want autocompletion in your IDE or type check:
1import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3'; 2 3export default { 4 input: 'xxxx', 5 output: {}, 6 plugins: [ 7 swc(defineRollupSwcOption({ 8 // ... There goes the plugin's configuration 9 })), 10 ]; 11} 12 13// or 14/** @type {import('rollup-plugin-swc3').PluginOptions} */ 15const swcPluginConfig = {}
exclude
string | RegExp | Array<String | RegExp>
/node_modules/
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore.
extensions
string[]
['.ts', '.tsx', '.mjs', '.js', '.cjs', '.jsx']
Specifies the files in the build the plugin should operate on. Also, the plugin will search and resolve files for extensions in the order specified for extensionless imports.
include
string | RegExp | Array<String | RegExp>
/\.[mc]?[jt]sx?$/
A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on.
tsconfig
string | false | undefined
'tsconfig.json'
rollup-plugin-swc
will read your tsconfig.json
or jsconfig.json
for default values if your doesn't provide corresponding swc options:
rollup-plugin-swc
will always have the highest priority (higher than tsconfig.json
/jsconfig.json
).rollup-plugin-swc
uses get-tsconfig
to find the tsconfig.json
/jsconfig.json
for the file currently being transpiled.
tsconfig.rollup.json
, jsconfig.compile.json
) to tsconfig
option, and rollup-plugin-swc
will find and resolve the nearest file with that filename./path/to/your/tsconfig.json
) to tsconfig
option, and rollup-plugin-swc
will only use the provided path as a single source of truth.rollup-plugin-swc
from reading tsconfig.json
/jsconfig.json
by setting tsconfig
option to false
.jsconfig.json
will be ignored if tsconfig.json
and jsconfig.json
both exist.extends
of tsconfig.json
/jsconfig.json
is compilerOptions.target
will be passed to swc's jsc.target
.compilerOptions.jsxImportSource
, compilerOptions.jsxFactory
, and compilerOptions.jsxFragmentFactory
will be passed to swc's jsc.transform.react.importSource
, jsc.transform.react.pragma
and jsc.transform.react.pragmaFrag
.compilerOptions.jsx
is either react-jsx
or react-jsxdev
, swc's jsc.transform.react.runtime
will be automatic
, otherwise it will be classic
.
compilerOptions.jsx: react-jsxdev
will also set swc's jsc.transform.react.development
to true
.compilerOptions.jsx: preserve
will be ignored. swc will always transpile your jsx into javascript code.compilerOptions.baseUrl
and compilerOptions.paths
will be passed to swc's jsc.baseUrl
and jsc.paths
directly. They won't affect how rollup resolve your imports. If you have encounted any issue during bundling, please use other plugins to resolve your imports' aliases (e.g., add rollup-plugin-typescript-paths or rollup-plugin-tsconfig-paths before @rollup/plugin-node-resolve
).compilerOptions.importHelpers
will be passed to swc's jsc.externalHelpers
. You will have to have @swc/helpers
avaliable in your project when enabled.compilerOptions.experimentalDecorators
and compilerOptions.emitDecoratorMetadata
will be passed to swc's jsc.parser.decorators
and jsc.transform.decoratorMetadata
.compilerOptions.esModuleInterop
will always be ignored, as swc requires module.type
to exist when module.noInterop
is given.If you only want to use swc
to minify your bundle:
1import { minify } from 'rollup-plugin-swc3' 2 3export default { 4 plugins: [ 5 minify({ 6 // swc's minify option here 7 // mangle: {} 8 // compress: {} 9 }), 10 ], 11}
If you want autocompletion in your IDE or type check:
1import { minify, defineRollupSwcMinifyOption } from 'rollup-plugin-swc3' 2 3export default { 4 plugins: [ 5 minify( 6 defineRollupSwcMinifyOption({ 7 // swc's minify option here 8 // mangle: {} 9 // compress: {} 10 }) 11 ), 12 ], 13} 14 15// or 16/** @type {import('@swc/core').JsMinifyOptions} */ 17const swcMinifyConfig = {}
If you are are using Vite and you do not want to use terser
or esbuild
for minification, rollup-plugin-swc3
also provides a standalone minify plugin designed for Vite:
1import { defineConfig } from 'vite'; 2import { viteMinify } from 'rollup-plugin-swc3' 3 4export default defineConfig({ 5 plugins: [ 6 viteMinify({ 7 // swc's minify option here 8 // mangle: {} 9 // compress: {} 10 }), 11 ], 12})
'use client'
and 'use server'
)Since version 0.9.0
, the support for 'use client'
and 'use server'
has been added:
The support for
'use client'
and'use server'
has been separated into a standalone rollup pluginrollup-preserve-directives
, maintained by @huozhi and me. The previouspreserveUseDirective
named export is retained for the backward compatibility.
1# npm 2npm install -D rollup-preserve-directives 3# yarn 4yarn add -D rollup-preserve-directives 5# pnpm 6pnpm add -D rollup-preserve-directives
1// rollup.config.js 2import { swc } from 'rollup-plugin-swc3'; 3import preserveDirectives from 'rollup-preserve-directives'; 4 5export default { 6 input: 'xxxx', 7 output: {}, 8 plugins: [ 9 swc(), 10 // And add `preserveDirectives` plugin after the `swc` plugin 11 preserveDirectives() 12 ]; 13}
And that is it!
preserveDirectives
supports:
Merging duplicated directives in the output bundles
1// src/foo.js 2'use sukka'; 3'use foxtail'; 4 5export const foo = 'foo'; 6 7// src/bar.js 8'use sukka'; 9export const bar = 'bar'; 10 11// src/index.js 12export { foo } from './foo'; 13export { bar } from './bar'; 14 15// rollup.config.js 16export default { 17 input: 'src/index.js', 18 output: { file: 'dist/index.js' } 19 plugins: [swc(), preserveDirectives()] 20} 21 22// dist/index.js 23'use sukka'; 24'use foxtail'; 25 26const foo = 'foo'; 27const bar = 'bar'; 28 29export { foo, bar };
When bundle React Client Component and React Server Component separately, mutiple entries will have their own separated and correct directives:
1// src/client.js 2'use client'; 3import { useState } from 'react'; 4export const Foo = () => { useState('client-only code') }; 5 6// src/server.js 7'use server'; 8import 'fs'; 9export const bar = 'server only code'; 10 11// rollup.config.js 12export default { 13 // let rollup bundle client and server separately by adding two entries 14 input: { 15 client: 'src/client.js', 16 server: 'src/server.js' 17 }, 18 // output both client bundle and server bundle in the "dist/" directory 19 output: { dir: 'dist/', entryFileName: '[name].js' } 20 plugins: [swc(), preserveDirectives()] 21} 22 23// dist/client.js 24'use client'; 25import { useState } from 'react'; 26const Foo = () => { useState('client-only code') }; 27export { Foo }; 28 29// dist/server.js 30'use server'; 31import 'fs'; 32const bar = 'server only code'; 33export { bar };
You can write your Rollup config file in rollup.config.ts
, and use the following command:
1rollup --config rollup.config.ts --configPlugin swc3
There are serveral ways to generate declaration file:
tsc
with emitDeclarationOnly
, the slowest way but you get type checking, it doesn't bundle the .d.ts
files.rollup-plugin-dts
which generates and bundle .d.ts
, also does type checking. It is used by this plugin as well.You can either configure it in your tsconfig.json
or in your rollup.config.js
.
1// Vue JSX 2import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3'; 3 4export default { 5 input: 'xxxx', 6 output: {}, 7 plugins: [ 8 swc(defineRollupSwcOption({ 9 jsc: { 10 experimental: { 11 plugins: [['swc-plugin-vue-jsx', {}]] // npm i swc-plugin-vue-jsx 12 } 13 } 14 })), 15 ]; 16}
1// Preact 2import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3'; 3 4export default { 5 input: 'xxxx', 6 output: {}, 7 plugins: [ 8 swc(defineRollupSwcOption({ 9 jsc: { 10 transform:{ 11 react: { 12 pragma: 'h', 13 pragmaFrag: 'Fragment' 14 // To use preact/jsx-runtime: 15 // importSource: 'preact', 16 // runtime: 'automatic' 17 } 18 } 19 } 20 })), 21 ]; 22}
rollup-plugin-swc © Sukka, Released under the MIT License.
Inspired by egoist's rollup-plugin-esbuild.
Authored and maintained by Sukka with help from contributors (list).
Personal Website · Blog · GitHub @SukkaW · Telegram Channel @SukkaChannel · Mastodon @sukka@acg.mn · Twitter @isukkaw · Keybase @sukka
If minify is called in Rollup's transform
phase, every individual module processed will result in a minify call. However, if minify is called in Rollup's renderChunk
phase, the minify will only be called once in one whole pass before Rollup generates bundle, results in a faster build. ↩
Autocompletion and type checking in your IDE ↩
mentaljam/rollup-plugin-swc
has both main
and module
fields in package.json
, but has🛑exports
field. ↩
No vulnerabilities found.
No security vulnerabilities found.