This chapter introduces how to migrate a Vite project to Rsbuild.
First, you need to replace the npm dependencies of Vite with Rsbuild's dependencies.
Next, you need to update the npm scripts in your package.json to use Rsbuild's CLI commands.
Create a Rsbuild configuration file rsbuild.config.ts
in the same directory as package.json, and add the following content:
The default build entry points for Rsbuild and Vite are different. Vite uses index.html
as the default entry, while Rsbuild uses src/index.js
.
When migrating from Vite to Rsbuild, you can use Rsbuild's source.entry to set the build entry and html.template to set the template.
Using a newly created Vite project as an example, first delete the <script>
tags from index.html
:
Then add the following config.
Rsbuild will automatically inject the <script>
tags into the generated HTML files during the build process.
Most common Vite plugins can be easily migrated to Rsbuild plugins, such as:
You can refer to Plugin List to learn more about available plugins.
Here is the corresponding Rsbuild configuration for Vite configuration:
Vite | Rsbuild |
---|---|
root | root |
mode | mode |
base | server.base |
define | source.define |
plugins | plugins |
appType | server.historyApiFallback |
envDir | Env Directory |
publicDir | server.publicDir |
assetsInclude | source.assetsInclude |
resolve.alias | resolve.alias |
resolve.dedupe | resolve.dedupe |
resolve.extensions | resolve.extensions |
resolve.conditions | tools.rspack.resolve.conditionNames |
resolve.mainFields | tools.rspack.resolve.mainFields |
resolve.preserveSymlinks | tools.rspack.resolve.symlinks |
html.cspNonce | security.nonce |
css.modules | output.cssModules |
css.postcss | tools.postcss |
css.preprocessorOptions.sass | pluginSass |
css.preprocessorOptions.less | pluginLess |
css.preprocessorOptions.stylus | pluginStylus |
css.devSourcemap | output.sourceMap |
css.lightningcss | tools.lightningcssLoader |
server.host, preview.host | server.host |
server.port, preview.port | server.port |
server.cors, preview.cors | server.cors |
server.strictPort, preview.strictPort | server.strictPort |
server.https, preview.https | server.https |
server.open, preview.open | server.open |
server.proxy, preview.proxy | server.proxy |
server.headers, preview.headers | server.headers |
server.hmr | dev.hmr, dev.client |
build.target, build.cssTarget | Browserslist |
build.outDir, build.assetsDir | output.distPath |
build.assetsInlineLimit | output.dataUriLimit |
build.cssMinify | output.minify |
build.sourcemap | output.sourceMap |
build.lib | Use Rslib |
build.manifest | output.manifest |
build.ssrEmitAssets | output.emitAssets |
build.minify, build.terserOptions | output.minify |
build.emptyOutDir | output.cleanDistPath |
build.copyPublicDir | server.publicDir |
build.reportCompressedSize | performance.printFileSize |
ssr, worker | environments |
Notes:
Vite injects environment variables starting with VITE_
into the client code by default, while Rsbuild injects environment variables starting with PUBLIC_
by default (see public variables).
To be compatible with Vite's behavior, you can manually call Rsbuild's loadEnv method to read environment variables starting with VITE_
, and inject them into the client code through the source.define config.
Rsbuild injects the following environment variables by default:
import.meta.env.MODE
import.meta.env.BASE_URL
import.meta.env.PROD
import.meta.env.DEV
For import.meta.env.SSR
, you can set it through the environments and source.define configuration options:
Vite provides some preset type definitions through the vite-env.d.ts
file. When migrating to Rsbuild, you can use the preset types provided by @rsbuild/core
:
Vite provides import.meta.glob()
for importing multiple modules.
When migrating to Rsbuild, you can use the import.meta.webpackContext() function of Rspack instead:
After completing the above steps, you have completed the basic migration from Vite to Rsbuild. You can now run the npm run dev
command to try starting the dev server.
If you encounter any issues during the build process, please debug according to the error log, or check the Vite configuration to see if there are any necessary configurations that have not been migrated to Rsbuild.
The current document only covers part of the migration process. If you find suitable content to add, feel free to contribute to the documentation via pull request 🤝.
The documentation for rsbuild can be found in the rsbuild/website directory.