Babel plugin
Rsbuild uses SWC transpilation by default. When existing functions cannot meet the requirements, and some Babel presets or plugins need to be added for additional processing, you can use Rsbuild's Babel Plugin.
Quick start
Install plugin
You can install the plugin using the following command:
Register plugin
You can register the plugin in the rsbuild.config.ts file:
Compilation cache
After using the Babel plugin, Rsbuild will perform the Babel transpilation in addition to the standard SWC transpilation, which adds additional compilation overhead. This can cause a noticeable decrease in build speed.
To reduce the overhead of Babel transpilation, the @rsbuild/plugin-babel enables Babel compilation cache by default. If you want to disable the cache, you can set performance.buildCache to false:
Options
babelLoaderOptions
These options are passed to babel-loader. For details, see the babel-loader documentation.
- Type:
Object | Function - Default:
Function type
When configuration is of type Function, the default Babel configuration will be passed as the first parameter. You can directly modify the configuration object or return an object as the final babel-loader configuration.
The second parameter of the function provides some more convenient utility functions. Please continue reading the documentation below.
The above example is just for reference, usually you do not need to manually configure babel-plugin-import, because the Rsbuild already provides a more general source.transformImport configuration.
Object type
When configuration's type is Object, the config will be shallow merged with default config by Object.assign.
Note that Object.assign is a shallow copy and will completely overwrite the built-in presets or plugins array, please use it with caution.
Util functions
When configuration is a Function, the tool functions available for the second parameter are as follows:
addPlugins
- Type:
(plugins: BabelPlugin[]) => void
Add some Babel plugins. For example:
addPresets
- Type:
(presets: BabelPlugin[]) => void
Add Babel preset configuration. (No need to add presets in most cases)
removePlugins
- Type:
(plugins: string | string[]) => void
To remove the Babel plugin, just pass in the name of the plugin to be removed, you can pass in a single string or an array of strings.
removePresets
- Type:
(presets: string | string[]) => void
To remove the Babel preset configuration, pass in the name of the preset to be removed, you can pass in a single string or an array of strings.
include
- Type:
string | RegExp | (string | RegExp)[] - Default:
undefined
Used to specify the files that need to be compiled by Babel.
Due to the performance overhead of Babel compilation, matching only certain files through include can reduce the number of modules compiled by Babel, thereby improving build performance.
For example, to only compile .custom.js files and ignore files under node_modules:
When you configure the include or exclude options, Rsbuild will create a separate Rspack rule to apply babel-loader and swc-loader.
This separate rule is completely independent of the SWC rule built into Rsbuild and is not affected by source.include and source.exclude.
exclude
- Type:
string | RegExp | (string | RegExp)[] - Default:
undefined
Used to specify the files that do not need to be compiled by Babel.
Due to the performance overhead of Babel compilation, excluding certain files through exclude can reduce the number of modules compiled by Babel, thereby improving build performance.
Execution order
After using @rsbuild/plugin-babel, Rsbuild will use both babel-loader and builtin:swc-loader to compile JavaScript files, with Babel running before SWC.
This means that if you are using some new ECMAScript features in your code, you may need to add Babel plugins to ensure that Babel can correctly compile these new features.
For example, add the @babel/plugin-transform-private-methods plugin to enable Babel to correctly compile private properties:
Debugging configs
After modifying the babel-loader configuration, you can view the final generated configuration in Rsbuild debug mode.
First, enable debug mode by using the DEBUG=rsbuild option:
Then open the generated rspack.config.web.mjs file and search for the babel-loader keyword to see the complete babel-loader configuration.
FAQ
Compilation freezes
After using the babel plugin, if the compilation progress bar is stuck, but there is no Error log on the terminal, it is usually because an exception occurred during the compilation. In some cases, when Error is caught by webpack or other modules, the error log cannot be output correctly. The most common scenario is that there is an exception in the Babel config, which is caught by webpack, and webpack swallows the Error in some cases.
Solution:
If this problem occurs after you modify the Babel config, it is recommended to check for the following incorrect usages:
- You have configured a plugin or preset that does not exist, maybe the name is misspelled, or it is not installed correctly.
- Whether multiple babel-plugin-imports are configured, but the name of each babel-plugin-import is not declared in the third item of the array.

