Rsbuild supports building outputs for multiple environments at the same time. You can use environments to build multiple environments in parallel and set a different Rsbuild config for each environment.
The environment
refers to the runtime environment for build output. Common environments include browsers, Node.js, and Workers. Rsbuild allows you to define any 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:
rsc
and ssr
environments, both targeting node
, used separately for React Server Components and SSR.desktop
and mobile
environments, both targeting web
, 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. Now, with the environments
configuration, you can complete the build for multiple outputs 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. Rsbuild plugin developers can customize the build process for a specified environment based on the environment
name, such as modifying Rsbuild or Rspack configurations, registering or removing plugins, adjusting Rspack rules, and viewing assets information.
Rsbuild supports defining different Rsbuild configs for each environment through environments.
For example, if your project wants to support the SSR function, you need to define different configs for client and SSR respectively. You can define a web and node environment respectively.
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.
environments.web
environments.node
Then, Rsbuild will use these environments configs to internally generate two Rspack configs and execute a single build using Rspack’s MultiCompiler.
When you execute the command npx rsbuild inspect
in the project root directory, you will find the following output:
When environments is not specified, Rsbuild will by default create an environment with the same name based on the currently target type (the value of output.target).
The above config is equivalent to a simplification of the following config:
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 environment via --environment <name>
.
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.
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.
By default, Rsbuild builds all environments in parallel. If an environment in your project depends on other environments to be compiled first, you can add the name of the environment you need to depend on to Rspack's dependencies configuration.
For example, the node environment depends on the web environment to be compiled first. You can add the following configuration: