Rsbuild provides a lightweight yet powerful plugin system to implement most of its functionality and allows for user extension.
Plugins written by developers can modify the default behavior of Rsbuild and add various additional features, including but not limited to:
Plugins provide a function similar to (options?: PluginOptions) => RsbuildPlugin
as an entry point.
Registering the plugin:
Function-based plugins can accept an options object and return a plugin instance, managing internal state through closures.
The roles of each part are as follows:
name
property is used to label the plugin's name.setup
serves as the main entry point for the plugin logic.api
object contains various hooks and utility functions.The naming convention for plugins is as follows:
pluginXXX
and exported by name.name
of the plugin follows the format scope:foo-bar
or plugin-foo-bar
, adding scope:
can avoid naming conflicts with other plugins.Here is an example:
The name
of official Rsbuild plugins uniformly uses rsbuild:
as a prefix, for example, rsbuild:react
corresponds to @rsbuild/plugin-react
.
rsbuild-plugin-template is a minimal Rsbuild plugin template repository that you can use as a basis for developing your Rsbuild plugin.
Rsbuild uses lifetime planning work internally, and plugins can also register hooks to take part in any stage of the workflow and implement their own features.
The full list of Rsbuild's lifetime hooks can be found in the API References.
The Rsbuild does not take over the hooks of the underlying Rspack, whose documents can be found here: Rspack Plugin API。
Custom plugins can usually get config from function parameters, just define and use it at your pleasure.
But sometimes you may need to read and change the public config of the Rsbuild. To begin with, you should understand how the Rsbuild generates and uses its config:
api.modifyRsbuildConfig(...)
.Refer to this tiny example:
There are 3 ways to use Rsbuild config:
api.modifyRsbuildConfig(config => {})
to modify config.api.getRsbuildConfig()
to get Rsbuild config.api.getNormalizedConfig()
to get finally normalized config.When normalized, it will again merge the config object with the default values and make sure the optional properties exist. So for PluginUploadDist, part of its type looks like:
The return value type of getNormalizedConfig()
is slightly different from that of RsbuildConfig
and is narrowed compared to the types described elsewhere in the documentation.
You don't need to fill in the defaults when you use it.
Therefore, the best way to use configuration options is to
api.modifyRsbuildConfig(config => {})
api.getNormalizedConfig()
as the actual config used by the plugin in the further lifetime.The Rsbuild plugin can modify the configuration of Rspack in various ways.
api.modifyRspackConfig(config => {})
modifies the final Rspack configuration.api.modifyBundlerChain(chain => {})
modifies the bundler-chain
, usage is similar to webpack-chain.api.onAfterCreateCompiler(compiler => {})
directly operates on the Rspack compiler instance.Loaders can read and process different types of file modules, refer to concepts and loaders.
You can register Rspack plugins within Rsbuild plugins, such as registering eslint-webpack-plugin
: