SukkaW / rollup-plugin-swc

Use SWC with Rollup to transform / minify ESNext and TypeScript code.
MIT License
154 stars 15 forks source link
esnext jsx minifier rollup rollup-plugin swc tsx typescript

rollup-plugin-swc

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 plugin rollup-preserve-directives, the previous preserveUseDirective named export is retained for the backward compatibility.

Comparison

sukkaw/rollup-plugin-swc mentaljam/rollup-plugin-swc nicholasxjy/rollup-plugin-swc2 @rollup/plugin-swc
minify your bundle in one pass[^1] โœ… ๐Ÿ›‘ ๐Ÿ›‘ ๐Ÿ›‘
Standalone swcMinify plugin โœ… ๐Ÿ›‘ ๐Ÿ›‘ ๐Ÿ›‘
Config Intellisense[^2] โœ… ๐Ÿ›‘ ๐Ÿ›‘ ๐Ÿ›‘
Reads your tsconfig.json and jsconfig.json โœ… ๐Ÿ›‘ ๐Ÿ›‘ ๐Ÿ›‘
ESM export โœ… ๐ŸŸก[^3] ๐Ÿ›‘ โœ…
TypeScript declarations โœ… โœ… โœ… โœ…
Has testing โœ… ๐Ÿ›‘ ๐Ÿ›‘ โœ…

[^1]: 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. [^2]: Autocompletion and type checking in your IDE [^3]: mentaljam/rollup-plugin-swc has both main and module fields in package.json, but has๐Ÿ›‘exports field.

Install

$ npm i @swc/core rollup-plugin-swc3
# If you prefer yarn
# yarn add @swc/core rollup-plugin-swc3
# If you prefer pnpm
# pnpm add @swc/core rollup-plugin-swc3

Usage

// rollup.config.js
import { swc } from 'rollup-plugin-swc3';

export default {
  input: 'xxxx',
  output: {},
  plugins: [
    swc({
      // All options are optional
      include: /\.[mc]?[jt]sx?$/, // default
      exclude: /node_modules/, // default
      tsconfig: 'tsconfig.json', // default
      // tsconfig: false, // You can also prevent `rollup-plugin-swc` from reading tsconfig.json, see below
      // And add your swc configuration here!
      // "filename" will be ignored since it is handled by rollup
      jsc: {}
    }),
  ];
}

If you want autocompletion in your IDE or type check:

import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3';

export default {
  input: 'xxxx',
  output: {},
  plugins: [
    swc(defineRollupSwcOption({
      // ... There goes the plugin's configuration
    })),
  ];
}

// or
/** @type {import('rollup-plugin-swc3').PluginOptions} */
const swcPluginConfig = {}

exclude

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should ignore.

extensions

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

A picomatch pattern, or array of patterns, which specifies the files in the build the plugin should operate on.

tsconfig

rollup-plugin-swc will read your tsconfig.json or jsconfig.json for default values if your doesn't provide corresponding swc options:

Standalone Minify Plugin

If you only want to use swc to minify your bundle:

import { minify } from 'rollup-plugin-swc3'

export default {
  plugins: [
    minify({
      // swc's minify option here
      // mangle: {}
      // compress: {}
    }),
  ],
}

If you want autocompletion in your IDE or type check:

import { minify, defineRollupSwcMinifyOption } from 'rollup-plugin-swc3'

export default {
  plugins: [
    minify(
      defineRollupSwcMinifyOption({
        // swc's minify option here
        // mangle: {}
        // compress: {}
      })
    ),
  ],
}

// or
/** @type {import('@swc/core').JsMinifyOptions} */
const 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:

import { defineConfig } from 'vite';
import { viteMinify } from 'rollup-plugin-swc3'

export default defineConfig({
  plugins: [
    viteMinify({
      // swc's minify option here
      // mangle: {}
      // compress: {}
    }),
  ],
})

React Server Component directives ('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 plugin rollup-preserve-directives, maintained by @huozhi and me. The previous preserveUseDirective named export is retained for the backward compatibility.

# npm
npm install -D rollup-preserve-directives
# yarn
yarn add -D rollup-preserve-directives
# pnpm
pnpm add -D rollup-preserve-directives
// rollup.config.js
import { swc } from 'rollup-plugin-swc3';
import preserveDirectives from 'rollup-preserve-directives';

export default {
  input: 'xxxx',
  output: {},
  plugins: [
    swc(),
    // And add `preserveDirectives` plugin after the `swc` plugin
    preserveDirectives()
  ];
}

And that is it!

preserveDirectives supports:

Write your Rollup config in TypeScript

You can write your Rollup config file in rollup.config.ts, and use the following command:

rollup --config rollup.config.ts --configPlugin swc3

TypeScript Declaration File

There are serveral ways to generate declaration file:

Use with Non-react JSX

You can either configure it in your tsconfig.json or in your rollup.config.js.

// Vue JSX
import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3';

export default {
  input: 'xxxx',
  output: {},
  plugins: [
    swc(defineRollupSwcOption({
      jsc: {
        experimental: {
          plugins: [['swc-plugin-vue-jsx', {}]] // npm i swc-plugin-vue-jsx
        }
      }
    })),
  ];
}
// Preact
import { swc, defineRollupSwcOption } from 'rollup-plugin-swc3';

export default {
  input: 'xxxx',
  output: {},
  plugins: [
    swc(defineRollupSwcOption({
      jsc: {
        transform:{
          react: {
          pragma: 'h',
          pragmaFrag: 'Fragment'
          // To use preact/jsx-runtime:
          // importSource: 'preact',
          // runtime: 'automatic'
          }
        }
      }
    })),
  ];
}

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