Rsbuild core
Rsbuild offers these core methods.
createRsbuild
Create an Rsbuild instance.
- Type:
- Example:
Options
The first parameter of createRsbuild is an options object with the following properties:
cwd: The root path of the current build, defaults toprocess.cwd().callerName: The name of the framework or tool currently invoking Rsbuild, defaults to'rsbuild', see Specify caller name.environment: Build only specified environments. If not specified or an empty array is passed, all environments will be built.loadEnv:Whether to call the loadEnv method to load environment variables and define them as global variables via source.define.config: Rsbuild configuration object or the result returned byloadConfig. Refer to Configuration Overview for all available configuration options.restart: A function that handles restart requests for the current dev server or watch build. See restart.
config was introduced in Rsbuild 1.6. In earlier versions, use rsbuildConfig as an alternative.
Load configuration async
config can also be an async function for dynamically loading Rsbuild configuration and performing custom operations.
When the complete loadConfig result is passed, the absolute paths of the config file and its imported dependencies are stored in rsbuild.context.configFile and rsbuild.context.configFileDependencies, respectively. When persistent build cache is enabled, these files are also used as build dependencies. See Configuration file watching for how Rsbuild watches them for restart requests.
Load environment variables
The loadEnv option in createRsbuild calls the loadEnv method to load environment variables:
Setting loadEnv: true automatically completes these steps:
- Call the
loadEnvmethod to load environment variables. - Add source.define configuration, defining the
publicVarsreturned byloadEnvas global variables. - Watch the
.envfile for changes, restart the dev server when the file changes, and invalidate the build cache. - Automatically call the
cleanupmethod returned byloadEnvwhen closing the build or dev server.
You can also pass in the options of the loadEnv method, for example:
restart
- Version: Added in v2.1.7
The restart option allows the caller to control how Rsbuild restarts the current dev server or watch build. This is useful when the restart flow needs to be managed outside Rsbuild.
See Configuration file watching to learn what triggers a restart.
When a restart is requested, Rsbuild calls the onRestart hook, closes the current task resources, and then calls restart.
The function should return true when the replacement task starts successfully. If it returns false or throws an error, the restart watcher remains active so later file changes can retry the restart.
Specify caller name
You can specify the name of the framework or tool that is currently invoking Rsbuild, which can be accessed by Rsbuild plugins through context.callerName, and execute different logic based on this identifier.
loadConfig
Load Rsbuild configuration file.
- Type:
- Example:
If the Rsbuild config file does not exist in the cwd directory, the return value of the loadConfig method is { content: {}, filePath: null }.
When path is not specified, loadConfig searches for config files in the following order:
rsbuild.config.tsrsbuild.config.jsrsbuild.config.mtsrsbuild.config.mjsrsbuild.config.ctsrsbuild.config.cjs
If multiple config files exist at the same time, loadConfig uses the first matching file in this list.
Customize config file names
Use the configFileNames option to replace the default lookup list. The file names are resolved relative to cwd, and this option is ignored when path is specified.
Specify the configuration file
Use the path option to load the my-config.ts configuration file:
Specify the export name
By default, loadConfig reads the default export from the config file. Use exportName to read a named export instead:
Set exportName to false when the config file only needs to be executed and no export should be read. In this case, content is {}.
Passing meta object
Load the configuration file and pass in a custom meta object:
In the defineConfig configuration function, you can access the foo variable through the meta object:
Passing command
By default, loadConfig passes process.argv[2] as the command value to the config function. Use the command option to explicitly set this value.
loadEnv
Load the .env file and return all environment variables starting with the specified prefixes.
- Type:
- Example:
This method will also load files such as .env.local and .env.[mode], see Environment Variables for details.
- Rsbuild CLI will automatically call the
loadEnv()method. If you are using the Rsbuild CLI, you can set themodeparameter through the --env-mode option. - The
loadEnvoption in createRsbuild will help you call theloadEnv()method and handle related operations.
Specify the target object
By default, loadEnv uses the process.env object to store environment variables. You can specify a target object to store environment variables through the processEnv option:
mergeRsbuildConfig
Used to merge multiple Rsbuild configuration objects.
The mergeRsbuildConfig function takes multiple configuration objects as parameters. It deeply merges each configuration object, automatically combining multiple function values into an array of sequentially executed functions, and returns a new merged configuration object without modifying the input configuration objects.
- Type:
Basic example
This method will not modify the config object in the input parameter.
Merge rules
In addition to deep merging, the mergeRsbuildConfig function also handles some options in a special way.
For example, tools.rspack can be set as a function. When multiple configuration objects contain tools.rspack, mergeRsbuildConfig will not simply retain the last function. On the contrary, it will merge all tools.rspack functions or objects into an array.
In the above example, the merged configuration is in the following format. The array first contains an object { someOption: true }, followed by two functions in the order they were merged.
Each item in the array will be executed in sequence, and the output of the previous function will serve as the input to the next one, ultimately generating an Rspack configuration.
By this way, we can ensure that when merging multiple configuration objects, the same multiple tools.rspack fields can all be effective.
In Rsbuild, most options that support function values use this rule, such as tools.postcss, tools.less, tools.bundlerChain, etc.
createLogger
Creates an isolated logger instance. The created logger can also be passed to the customLogger config option.
See Logging for more details.
- Example:
logger
A global logger instance that can be used to output logs in a format consistent with Rsbuild.
See Logging for more details.
- Example:
rspack
If you need to access the API or plugins exported by @rspack/core, you can directly import the rspack object from @rsbuild/core without installing the @rspack/core package separately.
- Type:
Rspack - Example:
- Refer to Rspack plugins and Rspack JavaScript API to learn more about the available Rspack APIs.
- It's not recommended to manually install the
@rspack/corepackage, as it may conflict with the version that Rsbuild depends on.
version
The version of @rsbuild/core currently in use.
- Type:
string - Example:
ensureAssetPrefix
The ensureAssetPrefix function is used to prepend a given assetPrefix to a string that might be a URL. If the input string is already a complete URL, it returns the string directly.
- Type:
If assetPrefix is not passed, Rsbuild uses the default asset prefix /. If assetPrefix is 'auto' or a function, this helper returns the input URL unchanged.
- Example:

