SWC (Speedy Web Compiler) is a transformer and minimizer for JavaScript and TypeScript based on Rust. SWC provides similar functionality to Babel and Terser, and it is 20x faster than Babel on a single thread and 70x faster on four cores.
Rsbuild enables the following SWC features by default:
The options for builtin:swc-loader
are the same as those for the JS version of swc-loader
. Rsbuild exposes some options to configure builtin:swc-loader
:
builtin:swc-loader
.Here are some examples:
tools.swc
can be used to register SWC's Wasm plugins, for example, registering @swc/plugin-styled-components:
You can check out the awesome-swc repository to see the SWC plugins available in the community.
Please note that the SWC plugin is still an experimental feature, and the SWC Wasm plugin is currently not backward compatible. The version of the SWC plugin is closely tied to the version of swc_core
that Rspack depends on.
This means that you must to choose an SWC plugin that matches the current version of swc_core
to ensure that it works properly. If the version of the SWC plugin you are using does not match the version of swc_core
that Rspack depends on, Rspack will throw an error during the build process. Please refer to Rspack FAQ - SWC Plugin Version Unmatched for more information.
Example of enabling the Emotion support using the builtin:swc-loader
:
For more options, please refer to @swc/plugin-emotion.
Example of enabling the Relay support using the builtin:swc-loader
:
For more options, please refer to @swc/plugin-relay.
Rsbuild provides the output.minify.js option to configure the SwcJsMinimizerRspackPlugin. Here are some examples:
You can exclude certain files from being minified using the exclude
option:
If the SwcJsMinimizerRspackPlugin
does not meet your needs, you can switch to other minimizers through the tools.bundlerChain option.
For example, to switch to Terser for minifying JavaScript, which is more stable but performs worse compared to SWC: