tools.rspack
- Type:
Rspack.Configuration | Function | undefined - Default:
undefined
tools.rspack configures Rspack.
The built-in Rspack config in Rsbuild may change with iterations, and these changes will not be reflected in semver. Therefore, your custom config may become invalid when you upgrade Rsbuild.
Object type
tools.rspack accepts an object that gets deep merged with the built-in Rspack configuration through webpack-merge.
For example, add resolve.alias configuration:
When merging configurations, webpack-merge will automatically concatenate arrays such as plugins, module.rules, resolve.extensions, etc.
If you need to override a configuration rather than merge it with the default value, you can use the function type of tools.rspack.
Function type
tools.rspack can be configured as a function. The first parameter of this function is the built-in Rspack configuration object, you can modify this object, and then return it. For example:
The object returned by the tools.rspack function is used directly as the final Rspack configuration and is not merged with the built-in Rspack configuration.
tools.rspack can also be an async function:
Utils
The second parameter of this function is an object, which contains some utility functions and properties, as follows:
env
- Type:
'development' | 'production' | 'test'
The env parameter determines whether the current environment is development, production or test. For example:
isDev
- Type:
boolean
A boolean value indicating whether this is a development build. Set to true when the mode is development.
isProd
- Type:
boolean
A boolean value indicating whether this is a production build. Set to true when the mode is production.
target
- Type:
'web' | 'node' | 'web-worker'
The current build target.
You can set different Rspack configurations for different build targets, for example:
isServer
- Type:
boolean
A boolean value indicating whether the build target is node, equivalent to target === 'node'.
isWebWorker
- Type:
boolean
A boolean value indicating whether the build target is web-worker, equivalent to target === 'web-worker'.
rspack
- Type:
Rspack
The Rspack instance, the same as import { rspack } from '@rsbuild/core'.
environment
- Type: EnvironmentContext
Context information for the current environment.
environments
- Type:
Record<string, EnvironmentContext>
Context information for all environments.
HtmlPlugin
- Type:
typeof import('html-rspack-plugin')
The default export of html-rspack-plugin.
addRules
- Type:
(rules: RuleSetRule | RuleSetRule[]) => void
Add additional Rspack rules to the head of the internal Rspack module rules array.
It should be noted that Rspack loaders will be executed in right-to-left order. If you want the loader you added to be executed before other loaders (Normal Phase), you should use appendRules to add the rule to the end.
For example:
appendRules
- Type:
(rules: RuleSetRule | RuleSetRule[]) => void
Add additional Rspack rules to the end of the internal Rspack module rules array.
For example:
prependPlugins
- Type:
(plugins: BundlerPluginInstance | BundlerPluginInstance[]) => void
Add additional plugins to the head of the internal Rspack plugins array, and the plugin will be executed first.
appendPlugins
- Type:
(plugins: BundlerPluginInstance | BundlerPluginInstance[]) => void
Add additional plugins at the end of the internal Rspack plugins array, the plugin will be executed last.
removePlugin
- Type:
(name: string) => void
Remove the internal Rspack plugin, the parameter is the constructor.name of the plugin.
For example, remove the internal webpack-bundle-analyzer:
mergeConfig
- Type:
(...configs:Rspack.Configuration[]) =>Rspack.Configuration
Used to merge multiple Rspack configs, the same as webpack-merge.
The mergeConfig method will create a new config object without modifying the original config object, so you need to return the result of mergeConfig.

