Plugin hooks
This page outlines the plugin hooks available for Rsbuild plugins.
Overview
Common hooks
- modifyRsbuildConfig: Modify the configuration passed to Rsbuild.
- modifyEnvironmentConfig: Modify the Rsbuild configuration of a specific environment.
- modifyRspackConfig: Modify the configuration passed to Rspack.
- modifyBundlerChain: Modify the configuration of Rspack through the chain API.
- modifyHTMLTags: Modify the tags that are injected into the HTML.
- modifyHTML: Modify the final HTML content.
- onBeforeCreateCompiler: Called before creating a compiler instance.
- onAfterCreateCompiler: Called after creating a compiler instance and before building.
- onBeforeEnvironmentCompile: Called before the compilation of a single environment.
- onAfterEnvironmentCompile: Called after the compilation of a single environment. You can get the build result information.
- onExit: Called when the process is about to exit.
Dev hooks
Called when running the rsbuild dev command or the rsbuild.startDevServer() method:
- onBeforeStartDevServer: Called before starting the dev server.
- onAfterStartDevServer: Called after starting the dev server.
- onBeforeDevCompile: Called before each build in development mode.
- onAfterDevCompile: Called after each build in development mode.
- onCloseDevServer: Called when the dev server is closed.
Build hooks
Called when running the rsbuild build command or the rsbuild.build() method:
- onBeforeBuild: Called before running the production build.
- onAfterBuild: Called after running the production build. You can get the build result information.
- onCloseBuild: Called when the build is closed.
Preview hooks
Called when running the rsbuild preview command or the rsbuild.preview() method:
- onBeforeStartProdServer: Called before starting the production server.
- onAfterStartProdServer: Called after starting the production server.
Hooks order
Dev hooks
When the rsbuild dev command or rsbuild.startDevServer() method is executed, Rsbuild will execute the following hooks in order:
- modifyRsbuildConfig
- modifyEnvironmentConfig
- onBeforeStartDevServer
- modifyBundlerChain
- modifyRspackConfig
- onBeforeCreateCompiler
- onAfterCreateCompiler
- onBeforeDevCompile
- onBeforeEnvironmentCompile
- onAfterStartDevServer
- modifyHTMLTags
- modifyHTML
- onAfterEnvironmentCompile
- onAfterDevCompile
- onCloseDevServer
- onExit
When rebuilding, the following hooks will be triggered again:
- onBeforeDevCompile
- onBeforeEnvironmentCompile
- modifyHTMLTags
- modifyHTML
- onAfterEnvironmentCompile
- onAfterDevCompile
Build hooks
When the rsbuild build command or rsbuild.build() method is executed, Rsbuild will execute the following hooks in order:
- modifyRsbuildConfig
- modifyEnvironmentConfig
- modifyBundlerChain
- modifyRspackConfig
- onBeforeCreateCompiler
- onAfterCreateCompiler
- onBeforeBuild
- onBeforeEnvironmentCompile
- modifyHTMLTags
- modifyHTML
- onAfterEnvironmentCompile
- onAfterBuild
- onCloseBuild
- onExit
When rebuilding, the following hooks will be triggered again:
- onBeforeBuild
- onBeforeEnvironmentCompile
- modifyHTMLTags
- modifyHTML
- onAfterEnvironmentCompile
- onAfterBuild
Preview hooks
When executing the rsbuild preview command or rsbuild.preview() method, Rsbuild will execute the following hooks in order:
Global hooks vs environment hooks
In Rsbuild, some plugin hooks are global. These hooks relate to Rsbuild's startup process or other shared logic and run across all environments. For example:
modifyRsbuildConfigis used to modify the basic configuration of Rsbuild. The basic configuration will eventually be merged with the environment configuration;onBeforeStartDevServerandonAfterStartDevServerare related to the Rsbuild dev server startup process, all environments share Rsbuild's dev server, middleware, and WebSocket.
Correspondingly, there are some plugin hooks that are related to the current environment. These hooks are executed with a specific environment context and are triggered multiple times depending on the environment.
Global hooks
- modifyRsbuildConfig
- onBeforeStartDevServer
- onBeforeCreateCompiler
- onAfterCreateCompiler
- onAfterStartDevServer
- onBeforeDevCompile
- onAfterDevCompile
- onCloseDevServer
- onBeforeBuild
- onAfterBuild
- onCloseBuild
- onBeforeStartProdServer
- onAfterStartProdServer
- onExit
Environment hooks
- modifyEnvironmentConfig
- modifyBundlerChain
- modifyRspackConfig
- modifyHTMLTags
- modifyHTML
- onBeforeEnvironmentCompile
- onAfterEnvironmentCompile
Callback order
Default behavior
If multiple plugins register the same hook, the callback functions of the hook will execute in the order in which they were registered.
In the following example, the console will output '1' and '2' in sequence:
order Field
When registering a hook, you can declare the order of hook through the order field.
In the following example, the console will sequentially output '2' and '1', because order was set to pre when plugin2 called modifyRsbuildConfig.
Common hooks
modifyRsbuildConfig
Modify the config passed to the Rsbuild, you can directly modify the config object, or return a new object to replace the previous object.
modifyRsbuildConfig is a global hook. To add support for your plugin as an environment-specific plugin, you should use modifyEnvironmentConfig instead of modifyRsbuildConfig.
- Type:
- Example: Setting a default value for a specific config option:
- Example: Using
mergeRsbuildConfigto merge config objects, and return the merged object.
modifyRsbuildConfig cannot be used to register additional Rsbuild plugins. This is because at the time modifyRsbuildConfig is executed, Rsbuild has already initialized all plugins and started executing the callbacks of the hooks.
For details, please refer to Plugin registration phase.
modifyEnvironmentConfig
Modify the Rsbuild configuration of a specific environment.
In the callback function, the config object in the parameters has already been merged with the common Rsbuild configuration. You can directly modify this config object, or you can return a new object to replace it.
- Type:
- Example: Set a default value for the Rsbuild config of a specified environment:
- Example: Using
mergeEnvironmentConfigto merge config objects, and return the merged object.
modifyRspackConfig
To modify the Rspack config, you can directly modify the config object, or return a new object to replace the previous object.
modifyRspackConfig is executed earlier than tools.rspack. Therefore, the modifications made by tools.rspack cannot be obtained in modifyRspackConfig.
- Type:
- Example:
The second parameter utils of the callback function is an object, which contains some utility functions and properties, see tools.rspack - Utils for more details.
modifyBundlerChain
rspack-chain is a utility library for configuring Rspack. It provides a chaining API, making the configuration of Rspack more flexible. By using rspack-chain, you can more easily modify and extend Rspack configurations without directly manipulating the complex configuration object.
modifyBundlerChain allows you to modify the Rspack configuration using the rspack-chain API, providing the same functionality as tools.bundlerChain.
- Type:
- Example:
The second parameter utils of the callback function is an object, which contains some utility functions and properties, see tools.bundlerChain - Utils for more details.
modifyHTML
Modify the final HTML content. The hook receives an HTML string and a context object, and you can return a new HTML string to replace the original one.
This hook is triggered after the modifyHTMLTags hook.
- Type:
- Version: Added in v1.3.15
- Example:
Modify HTML content based on filename:
Instead of directly manipulating the HTML string, you can use cheerio or htmlparser2 to modify the HTML content more conveniently.
For example, cheerio provides a jQuery-like API for HTML manipulation:
modifyHTMLTags
Modify the tags that are injected into the HTML.
- Type:
- Example:
See html.tags for more details on how to define tags.
When using modifyHTML, modifyHTMLTags, and html.tags options together, the execution order is as follows:
onBeforeCreateCompiler
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:
onAfterCreateCompiler
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:
onBeforeEnvironmentCompile
A callback function that is triggered before the compilation of a single environment.
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 dev or build watch mode, and use isFirstCompile to determine whether it is the first build.
- Type:
- Example:
onAfterEnvironmentCompile
A callback function that is triggered after the compilation of a single environment. You can access the build result information via the stats parameter.
Moreover, you can use isWatch to determine whether it is dev or build watch mode, and use isFirstCompile to determine whether it is the first build.
- Type:
- Example:
Build hooks
onBeforeBuild
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:
onAfterBuild
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:
onCloseBuild
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:
Dev hooks
onBeforeStartDevServer
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:
Register middleware
A common usage scenario is to register custom middleware in onBeforeStartDevServer:
When onBeforeStartDevServer is called, the default middleware of Rsbuild are not registered yet, so the middleware you add will run before the default middleware.
onBeforeStartDevServer allows you to return a callback function, which will be called when the default middleware of Rsbuild are registered. The middleware you register in the callback function will run after the default middleware.
Store server instance
If you need to access server in other hooks, you can store the server instance through api.onBeforeStartDevServer, and then access it in the hooks executed later. Note that you cannot access server in hooks that are executed earlier than onBeforeStartDevServer.
onAfterStartDevServer
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:
onBeforeDevCompile
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:
onAfterDevCompile
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:
onCloseDevServer
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:
Preview hooks
onBeforeStartProdServer
Called before starting the production preview server.
- Type:
- Example:
onAfterStartProdServer
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:
Other hooks
onExit
Called when the process is going to exit, this hook can only execute synchronous code.
- Type:
- Example:

