This section describes some low-level tools configurations in Rsbuild.
Object | Function
You can modify the config of autoprefixer by tools.autoprefixer
.
When tools.autoprefixer
is configured as Object
type, it is merged with the default config through Object.assign. For example:
When tools.autoprefixer
is a Function, the default config is passed as the first parameter and can be directly modified or returned as the final result. For example:
Function | undefined
undefined
You can modify the webpack and Rspack configuration by configuring tools.bundlerChain
which is type of Function
. The function receives two parameters, the first is the original bundler chain object, and the second is an object containing some utils.
Bundler chain is a subset of webpack chain, which contains part of the webpack chain API that you can use to modify both webpack and Rspack configuration.
Configurations modified via bundler chain will work on both webpack and Rspack builds. Note that the bundler chain only supports modifying the configuration of the non-differentiated parts of webpack and Rspack. For example, modifying the devtool configuration option (webpack and Rspack have the same devtool property value type), or adding an Rspack-compatible webpack plugin.
tools.bundlerChain
is executed earlier than tools.webpackChain / tools.webpack / tools.rspack and thus will be overridden by changes in others.
'development' | 'production' | 'test'
The env
parameter can be used to determine whether the current environment is development, production or test. For example:
boolean
The isProd
parameter can be used to determine whether the current environment is production. For example:
'web' | 'node' | 'web-worker'
The target
parameter can be used to determine the current environment. For example:
boolean
Determines whether the target environment is node
, equivalent to target === 'node'
.
boolean
Determines whether the target environment is web-worker
, equivalent to target === 'web-worker'
.
typeof import('html-webpack-plugin')
The instance of html-webpack-plugin
:
Some common Chain IDs are predefined in the Rsbuild, and you can use these IDs to locate the built-in Rule or Plugin.
Please note that some of the rules or plugins listed below are not available by default. They will only be included in the Rspack or webpack configuration when you enable specific options or register certain plugins.
For example, the RULE.STYLUS
rule exists only when the Stylus plugin is registered.
ID | Description |
---|---|
RULE.MJS |
Rule for mjs |
RULE.CSS |
Rule for css |
RULE.LESS |
Rule for less |
RULE.SASS |
Rule for sass |
RULE.STYLUS |
Rule for stylus |
RULE.TOML |
Rule for toml |
RULE.YAML |
Rule for yaml |
RULE.WASM |
Rule for WASM |
RULE.NODE |
Rule for node |
ONE_OF.XXX
can match a certain type of rule in the rule array.
ID | Description |
---|---|
ONE_OF.SVG |
Rules for SVG, automatic choice between data URI and separate file |
ONE_OF.SVG_URL |
Rules for SVG, output as a separate file |
ONE_OF.SVG_INLINE |
Rules for SVG, inlined into bundles as data URIs |
ONE_OF.SVG_ASSETS |
Rules for SVG, automatic choice between data URI and separate file |
USE.XXX
can match a certain loader.
ID | Description |
---|---|
USE.LESS |
correspond to less-loader |
USE.SASS |
correspond to sass-loader |
USE.STYLUS |
correspond to stylus-loader |
USE.VUE |
correspond to vue-loader |
USE.TOML |
correspond to toml-loader |
USE.YAML |
correspond to yaml-loader |
USE.NODE |
correspond to node-loader |
USE.SVGR |
correspond to @svgr/webpack |
USE.POSTCSS |
correspond to postcss-loader |
USE.RESOLVE_URL_LOADER_FOR_SASS |
correspond to resolve-url-loader |
PLUGIN.XXX
can match a certain webpack plugin.
ID | Description |
---|---|
PLUGIN.HTML |
correspond to HtmlPlugin , you need to splice the entry name when using: ${PLUGIN.HTML}-${entryName} |
PLUGIN.APP_ICON |
correspond to AppIconPlugin |
PLUGIN.INLINE_HTML |
correspond to InlineChunkHtmlPlugin |
PLUGIN.BUNDLE_ANALYZER |
correspond to WebpackBundleAnalyzer |
PLUGIN.VUE_LOADER_PLUGIN |
correspond to VueLoaderPlugin |
PLUGIN.AUTO_SET_ROOT_SIZE |
correspond to automatically set root font size plugin in Rsbuild |
MINIMIZER.XXX
can match a certain minimizer.
ID | Description |
---|---|
MINIMIZER.JS |
correspond to TerserWebpackPlugin or SwcJsMinimizerRspackPlugin |
MINIMIZER.CSS |
correspond to CssMinimizerWebpackPlugin |
For usage examples, please refer to: WebpackChain usage examples.
Object | Function
undefined
The config of css-loader can be modified through tools.cssLoader
. The default config is as follows:
When using Rspack as the bundler, this configuration is only supported when set disableCssExtract is true.
To modify CSS Modules configuration, it is recommended to use the output.cssModules configuration.
When this value is an Object, it is merged with the default config via deep merge. For example:
When the value is a Function, the default config is passed in as the first parameter. You can modify the config object directly, or return an object as the final config. For example:
false | Object | Function
The configs of html-webpack-plugin can be modified through tools.htmlPlugin
.
When tools.htmlPlugin
is Object
type, the value will be merged with the default config via Object.assign
.
When tools.htmlPlugin
is a Function:
The built-in html-webpack-plugin
plugins can be disabled by set tools.htmlPlugin
to false
.
By default, Rsbuild will compresses JavaScript/CSS code inside HTML during the production build to improve the page performance. This ability is often helpful when using custom templates or inserting custom scripts.
However, when output.enableInlineScripts
or output.enableInlineStyles
is turned on, inline JavaScript/CSS code will be repeatedly compressed, which will have a certain impact on build performance. You can modify the default minify behavior by modifying the tools.htmlPlugin.minify
configuration.
Object | Function
You can modify the config of less-loader via tools.less
.
When tools.less
is configured as Object
type, it is merged with the default config through Object.assign in a shallow way. It should be noted that lessOptions
is merged through deepMerge in a deep way. For example:
When tools.less
is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For example:
In some scenarios, if you need to use a specific version of Less instead of the built-in Less v4 in Rsbuild, you can install the desired Less version in your project and set it up using the implementation
option of the less-loader
.
(excludes: RegExp | RegExp[]) => void
Used to specify which files less-loader
does not compile, You can pass in one or more regular expressions to match the path of less files, for example:
Object | Function
Rsbuild integrates PostCSS by default, you can configure postcss-loader through tools.postcss
.
When the value is a Function, the internal default config is passed as the first parameter, and the config object can be modified directly without returning, or an object can be returned as the final result; the second parameter is a set of tool functions for modifying the postcss-loader config.
For example, you need to add a PostCSS plugin on the basis of the original plugin, and push a new plugin to the postcssOptions.plugins array:
When you need to pass parameters to the PostCSS plugin, you can pass them in by function parameters:
tools.postcss
can return a config object and completely replace the default config:
When this value is an Object, it is merged with the default config via 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.
(plugins: PostCSSPlugin | PostCSSPlugin[]) => void
For adding additional PostCSS plugins, You can pass in a single PostCSS plugin, or an array of PostCSS plugins.
Rsbuild uses the PostCSS v8 version. When you use third-party PostCSS plugins, please pay attention to whether the PostCSS version is compatible. Some legacy plugins may not work in PostCSS v8.
Object | Function
You can modify the config of sass-loader via tools.sass
.
When tools.sass
is Object
type, it is merged with the default config through Object.assign. It should be noted that sassOptions
is merged through deepMerge in a deep way.
For example:
When tools.sass
is a Function, the default config is passed as the first parameter, which can be directly modified or returned as the final result. The second parameter provides some utility functions that can be called directly. For Example:
In some scenarios, if you need to use a specific version of Sass instead of the built-in Dart Sass v1 in Rsbuild, you can install the desired Sass version in your project and set it up using the implementation
option of the sass-loader
.
(excludes: RegExp | RegExp[]) => void
Used to specify which files sass-loader
does not compile, You can pass in one or more regular expressions to match the path of sass files, for example:
Object | Function
{}
The config of style-loader can be set through tools.styleLoader
.
It is worth noting that Rsbuild does not enable style-loader
by default. You can use output.disableCssExtract
config to enable it.
When this value is an Object, it is merged with the default config via Object.assign. For example:
When the value is a Function, the default config is passed in as the first parameter. You can modify the config object directly, or return an object as the final config. For example:
Object | Function | undefined
undefined
only support Rspack
tools.rspack
is used to configure Rspack.
tools.rspack
can be configured as an object to be deep merged with the built-in Rspack configuration through webpack-merge.
For example, add resolve.alias
configuration:
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.
The second parameter of this function is an object, which contains some utility functions and properties, as follows:
'development' | 'production' | 'test'
The env
parameter can be used to determine whether the current environment is development, production or test. For example:
boolean
The isProd
parameter can be used to determine whether the current environment is production. For example:
'web' | 'node' | 'web-worker'
The target
parameter can be used to determine the current target. For example:
boolean
Determines whether the target environment is node
, equivalent to target === 'node'
.
boolean
Determines whether the target environment is web-worker
, equivalent to target === 'web-worker'
.
typeof import('@rspack/core')
The Rspack instance. For example:
(rules: RuleSetRule | RuleSetRule[]) => void
Add additional Rspack rules.
For example:
(plugins: RspackPluginInstance | RspackPluginInstance[]) => void
Add additional plugins to the head of the internal Rspack plugins array, and the plugin will be executed first.
(plugins: RspackPluginInstance | RspackPluginInstance[]) => void
Add additional plugins at the end of the internal Rspack plugins array, the plugin will be executed last.
(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:
(...configs: RspackConfig[]) => RspackConfig
Used to merge multiple Rspack configs, same as webpack-merge.
(name: string) => string
Get the path to the Rsbuild built-in dependencies, such as:
This method is usually used when you need to reuse the same dependency with the rsbuild.
Rsbuild built-in dependencies are subject to change with version iterations, e.g. generate large version break changes. Please avoid using this API if it is not necessary.