This chapter introduces how to migrate a Create React App (CRA) or CRACO project to Rsbuild.
If your project has already run the CRA eject
command, then most of the content in this document will no longer be applicable.
After ejecting a CRA project, it becomes more like a project directly using webpack, so you can refer to the webpack migration guide.
First, you need to replace the npm dependencies of CRA with Rsbuild's dependencies.
For projects using CRACO, you can also remove the @craco/craco dependency.
Next, you need to update the npm scripts in package.json to Rsbuild's CLI commands.
Rsbuild does not integrate testing frameworks, so it does not provide a command to replace react-scripts test
. You can directly use testing frameworks such as Jest or Vitest. You can refer to the Rsbuild react-jest example project for configuration.
Create a Rsbuild configuration file rsbuild.config.ts
in the same directory as package.json and add the following content:
CRA uses the public/index.html
file as the default HTML template. In Rsbuild, you can specify the HTML template through html.template:
In the HTML template, if you are using the %PUBLIC_URL%
variable from CRA, replace it with Rsbuild's assetPrefix variable and use a forward slash for concatenation:
This completes the basic migration from CRA to Rsbuild. You can now run the npm run start
command to try starting the dev server.
By default, CRA outputs to the build
directory, while Rsbuild's default output directory is dist
.
You can configure Rsbuild's output.distPath.root option to change the directory to build
, in line with CRA:
For more details, please refer to the Output Files section.
Rsbuild supports CSS preprocessors such as Sass and Less through plugins. Please refer to:
If you are using the "SVG to React Component" feature of CRA (i.e., SVGR), you also need to install the SVGR plugin for Rsbuild.
For example, if you are using the following usage:
You only need to install and register @rsbuild/plugin-svgr
:
Please refer to the SVGR plugin documentation to learn how to use SVGR in Rsbuild.
Here is the corresponding Rsbuild configuration for CRA configuration:
CRA | Rsbuild |
---|---|
HOST | server.host |
PORT | server.port |
HTTPS | server.https |
WDS_SOCKET_HOST | dev.client.host |
WDS_SOCKET_PATH | dev.client.path |
WDS_SOCKET_PORT | dev.client.port |
PUBLIC_URL | dev.assetPrefix / output.assetPrefix |
BUILD_PATH | output.distPath |
GENERATE_SOURCEMAP | output.sourceMap |
IMAGE_INLINE_SIZE_LIMIT | output.dataUriLimit |
FAST_REFRESH | dev.hmr |
TSC_COMPILE_ON_ERROR | Type Check Plugin |
Notes:
By default, CRA uses Babel to compile dependencies in node_modules, but Rsbuild does not, to avoid the performance overhead and potential compilation errors caused by secondary compilation.
If you need to handle syntax compatibility issues caused by dependencies in node_modules, you can use the source.include config to compile node_modules.
CRA injects environment variables starting with REACT_APP_
into the client code by default, while Rsbuild injects environment variables starting with PUBLIC_
by default (see public variables).
To be compatible with CRA's behavior, you can manually call Rsbuild's loadEnv method to read environment variables starting with REACT_APP_
, and inject them into the client code through the source.define config.
In CRA, if you import an asset that the build tool cannot recognize, CRA will by default output the file to the build/static/media
directory, for example, the document.pdf
file:
In Rsbuild, when importing unrecognized assets, Rsbuild will logs errors:
To resolve this error, you can use the following methods:
For example, you can add the following asset modules config to get the same output result as CRA:
CRA provides react-app-polyfill to manually inject polyfill code.
In the Rsbuild project, you can remove the dependency and code related to react-app-polyfill, as Rsbuild will automatically read the browserslist config and inject polyfill code as needed.
You can read Browser Compatibility to understand how Rsbuild handles polyfills.
In non-TypeScript projects, CRA supports reading the paths
field in jsconfig.json as the path alias.
If you want to use this feature in Rsbuild, you can set the source.tsconfigPath option, so that Rsbuild can recognize the paths
field in jsconfig.json.
If your project is using CRACO to override CRA configuration, you can refer to the table below for migration:
Here is an example of migrating from webpack.configure
to tools.rspack
:
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.