Vite

Vite is an excellent and powerful build tool. In some cases, you might need to migrate a Vite application to Rsbuild. You can refer to this guide to perform the migration.

Installing Dependencies

First, you need to replace the npm dependencies of Vite with Rsbuild's dependencies.

  • Remove Vite dependencies:
npm
yarn
pnpm
bun
npm remove vite
  • Install Rsbuild dependencies:
npm
yarn
pnpm
bun
npm add @rsbuild/core -D

Updating npm scripts

Next, you need to update the npm scripts in your package.json to use Rsbuild's CLI commands.

package.json
{
  "scripts": {
-   "dev": "vite",
-   "build": "vite build",
-   "preview": "vite preview"
+   "dev": "rsbuild dev",
+   "build": "rsbuild build",
+   "preview": "rsbuild preview"
  }
}

Create Configuration File

Create a Rsbuild configuration file rsbuild.config.ts in the same directory as package.json, and add the following content:

rsbuild.config.ts
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
  plugins: [],
});

Build Entry

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:

index.html
- <script type="module" src="/src/main.ts"></script>

Then add the following config.

rsbuild.config.ts
export default {
  html: {
    template: './index.html',
  },
  source: {
    entry: {
      index: './src/main.ts',
    },
  },
};

Rsbuild will automatically inject the <script> tags into the generated HTML files during the build process.

Migrating Plugins

Most common Vite plugins can be easily migrated to Rsbuild plugins, such as:

Vite Rsbuild
@vitejs/plugin-react @rsbuild/plugin-react
@vitejs/plugin-react-swc @rsbuild/plugin-react
@vitejs/plugin-vue @rsbuild/plugin-vue
@vitejs/plugin-vue2 @rsbuild/plugin-vue2
@vitejs/plugin-vue-jsx @rsbuild/plugin-vue-jsx
@vitejs/plugin-vue2-jsx @rsbuild/plugin-vue2-jsx
@vitejs/plugin-basic-ssl @rsbuild/plugin-basic-ssl
@vitejs/plugin-legacy No need to use, see Browser Compatibility for details
@sveltejs/vite-plugin-svelte @rsbuild/plugin-svelte
vite-plugin-svgr @rsbuild/plugin-svgr
vite-plugin-checker @rsbuild/plugin-type-check
vite-plugin-eslint @rsbuild/plugin-eslint
vite-plugin-static-copy output.copy
vite-plugin-node-polyfills @rsbuild/plugin-node-polyfill
vite-plugin-solid @rsbuild/plugin-solid

You can refer to Plugin List to learn more about available plugins.

Glob Import

Vite provides import.meta.glob() for importing multiple modules.

When migrating to Rsbuild, you can use the import.meta.webpackContext() function of Rspack instead:

  • Vite:
const modules = import.meta.glob('./dir/*.js');

for (const path in modules) {
  modules[path]().then((mod) => {
    console.log(path, mod);
  });
}
  • Rsbuild:
const context = import.meta.webpackContext('./dir', {
  // Whether to search subdirectories
  recursive: false,
  regExp: /\.js$/,
});

for (const path of context.keys()) {
  const mod = context(path);
  console.log(path, mod);
}

Validating Results

After completing the above steps, you have completed the basic migration from Vite to Rsbuild. You can now run the npm run serve 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.

Contents Supplement

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.