Multi-environment builds
Rsbuild can build outputs for multiple environments in a single run. Use environments to build them in parallel and set a separate Rsbuild config for each one.
What is an environment
The environment refers to the runtime environment for build output. Common environments include browsers, Node.js, and Workers. Rsbuild allows you to define custom environment names and set build options for each environment individually.
A typical scenario is server-side rendering (SSR). You can define two environments, web and node, where the build targets (output.target) are web and node. These are used for client-side rendering (CSR) and server-side rendering (SSR) scenarios.
You can also define different environments for the same build target, for example:
- Define
rscandssrenvironments, both targetingnode, used separately for React Server Components and SSR. - Define
desktopandmobileenvironments, both targetingweb, used separately for desktop and mobile browsers.
Without the environments configuration, you would need to define multiple configurations for these scenarios and run multiple independent Rsbuild builds. With environments, you can build every output in a single Rsbuild run (Rsbuild achieves this using Rspack's MultiCompiler).
In Rsbuild, each environment is associated with an Rsbuild configuration, an Rspack configuration, and a set of build outputs. Plugin authors can tailor the build for a specific environment—modifying configs, registering or removing plugins, adjusting Rspack rules, or inspecting asset information—based on the environment name.
Environment configs
Rsbuild supports defining different Rsbuild configurations for each environment through environments.
For example, if your project needs SSR support, you need to define different configurations for the client and server. You can define web and node environments.
Config merging
If you configure environments, Rsbuild will merge the config in environments with the outer base config. When merging, the config in environments has higher priority.
In the example above, after merging the configs, Rsbuild generates two standalone environment configs for building web and node environments.
- web environments config: Generated by merging base config with
environments.web - node environments config: Generated by merging base config with
environments.node
Then, Rsbuild will use these environment configurations to internally generate two Rspack configs and execute a single build using Rspack’s MultiCompiler.
Debug config
When you execute the command npx rsbuild inspect in the project root directory, you will see the following output:
rsbuild.config.[name].mjs: The Rsbuild config used for a certain environment during build.rspack.config.[name].mjs: The Rspack config corresponding to a certain environment when building.
Default environment
When environments is not specified, Rsbuild creates an environment by default with the same name as the current target type (the value of output.target).
The above config is equivalent to a simplification of the following config:
Build a specific environment
By default, Rsbuild will build all environments in the Rsbuild configuration when you execute rsbuild dev or rsbuild build. You can build only the specified environments with --environment <name>.
Add plugins for specified environment
Plugins configured through the plugins field support running in all environments. If you want a plugin to run only in a specified environment, you can configure the plugin in the specified environment.
For example, enable the React plugin only in the web environment:
If you are a plugin developer, you can view Developing environment plugins for details.
Configuring output directories
When building for multiple environments, it's recommended to configure different output directories for each environment to prevent dist files with the same name from overwriting each other.
You can use output.distPath.root to set independent output root directories for each environment.
For example, output the web bundles to the default dist directory, and the node bundles to dist/server:
Plugin API
Update environment config
Rsbuild supports modifying or adding environment config through the modifyRsbuildConfig hook.
Configuring a specific environment
Rsbuild supports modifying the Rsbuild config of a specific environment through the modifyEnvironmentConfig hook.
Environment context
Environment context is a read-only object that provides some context infos about the current environment. Rsbuild supports obtaining environment context information in plugin hooks.
For some plugin hooks related to the build environment (such as modifyRspackConfig and modifyBundlerChain), Rsbuild supports obtaining the current environment context through the environment parameter.
For some global plugin hooks (such as onAfterDevCompile, onBeforeStartDevServer, etc.), Rsbuild supports obtaining the context of all environments through the environments parameter.
Environment API
Rsbuild server provides a series of APIs related to the build environment. Users can operate the build artifacts in a specific environment on the server side through the Rsbuild environment API.
You can use the environment API in Rsbuild DevMiddleware or Custom Server.
For example, you can quickly implement an SSR function through the Rsbuild environment API in development mode:
For detailed usage, please refer to: SSR + Express Example.
Build order
By default, Rsbuild builds all environments in parallel.
To control the build order between different environments, you can set build dependencies through Rspack's dependencies configuration.
For example, if you need to build the web environment first, then build the node environment, you can add the following configuration:
We can use a simple plugin to test the build order of multiple environments:

