Rsbuild supports directly modifying the Rspack configuration object, and also supports modifying the built-in Rspack configuration of Rsbuild through the bundler-chain.
You can use the tools.rspack
option of Rsbuild to modify the Rspack configuration object. Please refer to the tools.rspack documentation for complete usage.
Bundler chain is a subset of webpack chain, which contains part of the webpack chain API that you can use to modify both Rspack and webpack configuration.
Configurations modified via bundler chain will work on both Rspack and webpack builds. Note that the bundler chain only supports modifying the configuration of the non-differentiated parts of Rspack and webpack. For example, modifying the devtool configuration option (Rspack and webpack have the same devtool property value type), or adding an Rspack-compatible webpack plugin.
Compared to directly modifying the Rspack configuration object, bundler-chain not only supports chain calls but also allows modification of built-in rules or plugins based on their IDs.
Rsbuild provides the tools.bundlerChain
configuration option to modify the bundler chain.
The value of tools.bundlerChain
is a Function
type that accepts two parameters:
bundler-chain
instance, which can be used to modify the default bundler configuration.env
, isProd
, CHAIN_ID
, and more.Here's a basic example:
Before using the bundler chain to modify the Rspack configuration, it is recommended to familiarize yourself with some basics.
In short, the bundler chain requires users to set a unique ID for each rule, loader, plugin, and minimizer. With this ID, you can easily find the desired object from deeply nested objects.
Rsbuild exports all internally defined IDs through the CHAIN_ID
object, so you can quickly locate the loader or plugin you want to modify using these exported IDs, without the need for complex traversal in the Rspack configuration object.
For example, you can remove the built-in HTML plugin using CHAIN_ID.PLUGIN.HTML
:
The CHAIN_ID
object contains various IDs, which correspond to the following configurations:
CHAIN_ID Field | Corresponding Configuration | Description |
---|---|---|
CHAIN_ID.PLUGIN |
plugins[i] |
Corresponds to a plugin in the Rspack configuration |
CHAIN_ID.RULE |
module.rules[i] |
Corresponds to a rule in the Rspack configuration |
CHAIN_ID.USE |
module.rules[i].loader |
Corresponds to a loader in the Rspack configuration |
CHAIN_ID.MINIMIZER |
optimization.minimizer |
Corresponds to a minimizer in the Rspack configuration |
CHAIN_ID.RESOLVE_PLUGIN |
resolve.plugins[i] |
Corresponds to a resolve plugin in the Rspack configuration |
Here are examples of adding, modifying, and deleting Rspack loaders.
Here are examples of adding, modifying, and deleting Rspack plugins.
In the tools.bundlerChain
function, you can access various environment identifiers in the second parameter, such as development/production build, SSR build, Web Worker build, to achieve configuration modifications for different environments.
The above are some common configuration examples. For the complete bundler-chain API, please refer to the webpack-chain documentation.