Rsbuild instance
This section describes all the properties and methods on the Rsbuild instance object.
rsbuild.context
rsbuild.context is a read-only object that provides some context information, which can be accessed in two ways:
- Access through the
contextproperty of the Rsbuild instance:
- Access through the api.context of the Rsbuild plugin:
context.version
The version of @rsbuild/core currently in use.
- Type:
context.rootPath
The root path of the current build, corresponding to the cwd option of the createRsbuild method.
- Type:
context.distPath
The absolute path of the output directory, corresponding to the output.distPath.root config in RsbuildConfig.
When multiple environments exist, Rsbuild attempts to find the parent distPath of all environments as context.distPath.
To get the absolute path to a specific environment's output directory, use environment.distPath.
- Type:
context.cachePath
The absolute path of the build cache files.
- Type:
context.callerName
The name of the framework or tool that is currently invoking Rsbuild, the same as the callerName option in the createRsbuild method.
- Type:
string - Default:
'rsbuild' - Example:
Here are some tools based on Rsbuild that have already set the callerName value:
context.devServer
Dev server information when running in dev mode. Available after the dev server has been created.
- Type:
- Example:
context.action
The current action type.
- Type:
context.action is set when running CLI commands or calling Rsbuild instance methods:
dev: set when running rsbuild dev or rsbuild.startDevServer()build: set when running rsbuild build or rsbuild.build()preview: set when running rsbuild preview or rsbuild.preview()
For example:
context.bundlerType
The bundler type for the current build.
- Type:
Rsbuild internally supports switching to webpack for comparative testing, so this field is provided for differentiation. Usually, you do not need to use this field.
rsbuild.build
Runs a production build, generating optimized production bundles and writing them to the output directory.
- Type:
- Example:
Monitor file changes
To watch file changes and re-build, set the watch option to true.
Close build
rsbuild.build() returns a close() method that stops the build process.
In watch mode, calling the close() method will stop watching:
In non-watch mode, also call the close() method to end the build, which triggers the onCloseBuild hook for cleanup operations.
Stats object
In non-watch mode, rsbuild.build() returns an Rspack stats object.
For example, use the stats.toJson() method to get asset information:
rsbuild.startDevServer
Starts the local dev server. This method will:
- Start a development server to serve your application
- Watch for file changes and trigger recompilation
- Type:
- Example:
Start dev server:
Once the dev server starts successfully, these logs appear:
startDevServer returns these parameters:
urls: URLs to access dev server.port: The actual listening port number.server: Server instance object.
Close server
Call the close() method to close the dev server, trigger the onCloseDevServer hook, and perform cleanup operations.
Get port silently
When the default startup port is occupied, Rsbuild automatically increments the port number until it finds an available one. This process outputs a prompt log. To suppress this log, set getPortSilently to true.
rsbuild.createDevServer
Rsbuild includes a built-in dev server designed to improve the development experience. When you run the rsbuild dev command, the server starts automatically and provides features such as page preview, routing, and hot module reloading.
- To integrate the Rsbuild dev server into a custom server, you can use the
createDevServermethod to create a dev server instance. Please refer to Dev server API for all available APIs. - To use Rsbuild dev server to start the project directly, you can use the rsbuild.startDevServer method directly.
rsbuild.startDevServeris actually syntactic sugar for the following code:
rsbuild.preview
Starts a server to preview the production build locally. This method should be executed after rsbuild.build.
- Type:
- Example:
Start the server:
preview returns the following parameters:
urls: URLs to access server.port: The actual listening port number.server: Server instance object.
Close server
Calling the close() method will close the preview server.
rsbuild.createCompiler
Creates an Rspack Compiler instance. If there are multiple environments for this build, the return value is MultiCompiler.
- Type:
- Example:
You do not need to use this API unless you need to custom the dev server or other advanced scenarios.
rsbuild.addPlugins
Registers one or more Rsbuild plugins, which can be called multiple times.
This method needs to be called before compiling. If it is called after compiling, it will not affect the compilation result.
- Type:
- Example:
rsbuild.getPlugins
Gets all the Rsbuild plugins registered in the current Rsbuild instance.
- Type:
- Example:
rsbuild.removePlugins
Removes one or more Rsbuild plugins, which can be called multiple times.
This method needs to be called before compiling. If it is called after compiling, it will not affect the compilation result.
- Type:
- Example:
rsbuild.isPluginExists
Determines if a plugin has been registered in the current Rsbuild instance.
-
If the
environmentparameter is not specified, it checks if the plugin exists in the globally registered plugins. -
If the
environmentparameter is specified, it checks if the plugin exists in the specified environment. -
Type:
- Example:
Or check if a plugin exists in a specified environment:
rsbuild.initConfigs
Initialize and return the internal Rspack configurations used by Rsbuild. This method processes all plugins and configurations to generate the final Rspack configs.
Note: You typically do not need to call this method directly since it is automatically invoked by methods like rsbuild.build and rsbuild.startDevServer.
- Type:
- Example:
rsbuild.inspectConfig
Inspects and debugs Rsbuild's internal configurations. It provides access to:
- The resolved Rsbuild configuration
- The environment-specific Rsbuild configurations
- The generated Rspack configurations
The method serializes these configurations to strings and optionally writes them to disk for inspection.
- Type:
To view the Rsbuild and Rspack configurations during the build process, use debug mode, or obtain them through hooks such as onBeforeBuild, onBeforeCreateCompile.
Example
Get the content of configs in string format:
Write the config content to disk:
Output path
You can set the output path using outputPath. The default value is output.distPath.root.
If outputPath is a relative path, it will be concatenated relative to the value of output.distPath.root. You can also set outputPath to an absolute path, in which case the files will be written directly to that path. For example:
rsbuild.onBeforeCreateCompiler
Provides the same functionality as the onBeforeCreateCompiler plugin hook.
A callback function that is triggered after the Compiler instance has been created, but before the build process begins. This hook is called when you run rsbuild.startDevServer, rsbuild.build, or rsbuild.createCompiler.
You can access the Rspack configuration array through the bundlerConfigs parameter. The array may contain one or more Rspack configurations. It depends on whether multiple environments are configured.
- Type:
- Example:
rsbuild.onAfterCreateCompiler
Provides the same functionality as the onAfterCreateCompiler plugin hook.
A callback function that is triggered after the compiler instance has been created, but before the build process. This hook is called when you run rsbuild.startDevServer, rsbuild.build, or rsbuild.createCompiler.
You can access the Compiler instance through the compiler parameter:
- Type:
- Example:
rsbuild.onBeforeBuild
Provides the same functionality as the onBeforeBuild plugin hook.
A callback function that is triggered before the production build is executed.
You can access the Rspack configuration array through the bundlerConfigs parameter. The array may contain one or more Rspack configurations. It depends on whether multiple environments are configured.
Moreover, you can use isWatch to determine whether it is watch mode, and use isFirstCompile to determine whether it is the first build on watch mode.
- Type:
- Example:
rsbuild.onAfterBuild
Provides the same functionality as the onAfterBuild plugin hook.
A callback function that is triggered after running the production build. You can access the build result information via the stats parameter.
Moreover, you can use isWatch to determine whether it is watch mode, and use isFirstCompile to determine whether it is the first build on watch mode.
- Type:
- Example:
rsbuild.onCloseBuild
Provides the same functionality as the onCloseBuild plugin hook.
Called when closing the build instance. Can be used to perform cleanup operations when the building is closed.
Rsbuild CLI will automatically call this hook after running rsbuild build, while users of the JavaScript API need to manually call the build.close() method to trigger this hook.
- Type:
- Example:
rsbuild.onBeforeStartDevServer
Provides the same functionality as the onBeforeStartDevServer plugin hook.
Called before starting the dev server.
Use the server parameter to get the dev server instance, see Dev server API for more information.
- Type:
- Example:
See Plugin hooks - onBeforeStartDevServer for more details.
rsbuild.onAfterStartDevServer
Provides the same functionality as the onAfterStartDevServer plugin hook.
Called after starting the dev server, you can get the port number with the port parameter, and the page routes info with the routes parameter.
- Type:
- Example:
rsbuild.onCloseDevServer
Provides the same functionality as the onCloseDevServer plugin hook.
Called when closing the dev server. Can be used to perform cleanup operations when the dev server is closed.
Rsbuild CLI will automatically call this hook at the appropriate time, while users of the JavaScript API need to manually call the server.close() method to trigger this hook.
- Type:
- Example:
rsbuild.onBeforeStartProdServer
Provides the same functionality as the onBeforeStartProdServer plugin hook.
Called before starting the production preview server.
- Type:
- Example:
rsbuild.onAfterStartProdServer
Provides the same functionality as the onAfterStartProdServer plugin hook.
Called after starting the production preview server, you can get the port number with the port parameter, and the page routes info with the routes parameter.
- Type:
- Example:
rsbuild.onBeforeDevCompile
Provides the same functionality as the onBeforeDevCompile plugin hook.
A callback function that is triggered before the dev compile is executed.
You can access the Rspack configuration array through the bundlerConfigs parameter. The array may contain one or more Rspack configurations. It depends on whether multiple environments are configured.
Moreover, you can use isFirstCompile to determine whether it is the first compile.
- Type:
- Version: Added in v1.5.0
- Example:
rsbuild.onAfterDevCompile
Provides the same functionality as the onAfterDevCompile plugin hook.
Called after each development mode build, you can use isFirstCompile to determine whether it is the first build.
- Type:
The onAfterDevCompile hook was added in Rsbuild v1.5.0. For earlier versions, you can use the functionally identical onDevCompileDone hook.
- Example:
rsbuild.onBeforeEnvironmentCompile
Provides the same functionality as the onBeforeEnvironmentCompile plugin hook.
- Version: Added in v1.5.7
- Example:
rsbuild.onAfterEnvironmentCompile
Provides the same functionality as the onAfterEnvironmentCompile plugin hook.
- Version: Added in v1.5.7
- Example:
rsbuild.onExit
Provides the same functionality as the onExit plugin hook.
Called when the process is going to exit, this hook can only execute synchronous code.
- Type:
- Example:
rsbuild.getRsbuildConfig
Provides the same functionality as the getRsbuildConfig plugin API.
Get the Rsbuild config, this method must be called after the modifyRsbuildConfig hook is executed.
- Type:
- Parameters:
You can specify the type of Rsbuild config to read by using the type parameter:
- Example:
rsbuild.getNormalizedConfig
Provides the same functionality as the getNormalizedConfig plugin API.
Get the normalized Rsbuild config for all environments or a specific one. Call this method only after the modifyRsbuildConfig hook has executed.
Compared with the api.getRsbuildConfig method, the config returned by this method has been normalized, and the type definition of the config will be narrowed. For example, the undefined type of config.html will be removed.
We recommend using this method to get the Rsbuild config.
- Type:
- Example:
rsbuild.expose
Provides the same functionality as the expose plugin API.
- Version: Added in v1.5.0
- Example:
rsbuild.modifyRsbuildConfig
Provides the same functionality as the modifyRsbuildConfig plugin API.
- Version: Added in v1.5.0
- Example:
rsbuild.modifyEnvironmentConfig
Provides the same functionality as the modifyEnvironmentConfig plugin API.
- Version: Added in v1.5.0
- Example:

