This chapter introduces how to migrate a Vite plugin to Rsbuild plugin.
Before migrating a Vite plugin, it is recommended to check if there is a corresponding plugin in the Rsbuild ecosystem. You can find the plugins through the following pages:
Rsbuild plugin is defined in a way similar to Vite, usually a function that accepts plugin options as a parameter and returns a plugin description object.
The main difference is that Vite's hooks are defined directly on the plugin description object, while Rsbuild's hooks are accessed and called through the api object. This allows you to control the timing of plugin API calls more flexibly.
Rsbuild's plugin API covers most of the Vite and Rollup plugin hooks, for example:
Vite plugin hooks | Rsbuild plugin API |
---|---|
resolveId | resolve |
transform | transform |
config | modifyRsbuildConfig |
configResolved | getNormalizedConfig |
configEnvironment | modifyEnvironmentConfig |
configureServer | onBeforeStartDevServer |
buildStart | onBeforeBuild, onBeforeStartDevServer |
buildEnd | onAfterBuild, onCloseDevServer |
closeBundle | onCloseBuild, onCloseDevServer |
transformIndexHtml | modifyHTML, modifyHTMLTags |
See Plugin system for more details.
config
hookRsbuild provides the modifyRsbuildConfig hook to modify Rsbuild configuration. Since Rsbuild and Vite have different configuration structures, you'll need to adjust your configuration when migrating Vite plugins.
For example, you should replace Vite's define
option with Rsbuild's source.define option.
See Config migration to learn how to migrate Vite configurations to Rsbuild.
configEnvironment
hookRsbuild provides the modifyEnvironmentConfig hook to modify the configuration of a specific environment.
configResolved
hookRsbuild provides the api.getNormalizedConfig method to get the resolved configuration. This method serves a similar purpose to Vite's configResolved
hook.
transformIndexHtml
hookVite's transformIndexHtml
hook corresponds to two hooks in Rsbuild:
Here's an example of replacing the HTML title.
configureServer
hookRsbuild provides the onBeforeStartDevServer
hook to replace Vite's configureServer
hook, which allows you to get the dev server instance and add custom middleware.
apply
propertyRsbuild plugin provides the same apply property as Vite plugins.