Environment API
Here you can find all environment APIs.
See Multi-environment builds for more details.
Environment context
Environment context is a read-only object that provides context information about the current environment.
- Type:
You can get the environment context in the following ways:
- In Rsbuild's Plugin hooks, you can get the environment context object through the
environmentorenvironmentsparameter. - In the Environment API, it is available via
environments.context.
name
The unique name of the current environment, used to distinguish and locate the environment. Corresponds to the key in the environments configuration.
- Type:
string - Example:
browserslist
The browserslist configuration of the current environment. See Browserslist for more details.
- Type:
string[] - Example:
config
The normalized Rsbuild config for the current environment.
- Type:
- Example:
distPath
The absolute path of the output directory, corresponding to the output.distPath.root config of Rsbuild.
- Type:
string
entry
The entry object from the source.entry option.
- Type:
- Example:
htmlPaths
The path information for all HTML assets.
This value is an object, the key is the entry name and the value is the relative path of the HTML file in the dist directory.
- Type:
- Example:
tsconfigPath
The absolute path of the tsconfig.json file, or undefined if the tsconfig.json file does not exist in current project.
- Type:
- Example:
manifest
The manifest data. Only available when the output.manifest config is enabled.
- Type:
Record<string, unknown> | ManifestData | undefined - Example:
The manifest data is only available after the build has completed, you can access it in the following hooks:
webSocketToken
WebSocket authentication token, used to authenticate WebSocket connections and prevent unauthorized access.
Only available in development mode, and is an empty string in production mode.
- Type:
string - Version: Added in v1.4.4
When you need to establish a WebSocket connection with the Rsbuild dev server in the browser, you need to use this token as a query parameter.
Environment API
Environment API provides some APIs related to the multi-environment build.
You can use environment API via rsbuild.createDevServer() or dev.setupMiddlewares, which allows you to get the build outputs information for a specific environment in the server side.
context
You can get context information related to the current environment through the Environment API.
- Type: EnvironmentContext
- Example:
getStats
Get the build stats of current environment.
- Type:
- Example:
loadBundle
Loads and executes the compiled bundle on the server. This method returns the exported content of the specified entry module and is typically used to run bundles generated by Rsbuild in a server-side environment.
loadBundle resolves only after the build process has finished and the onAfterDevCompile hook has completed. As a result, it cannot be called within the onAfterDevCompile hook.
- Type:
- Example:
getTransformedHtml
Get the HTML template content after compilation and transformation.
- Type:
- Example:
This method returns the complete HTML string, including all resources and content injected through HTML plugins.

