Rslib instance
This section describes all the properties and methods on the Rslib instance object.
rslib.build
Runs a production build, generating production outputs and writing them to the output directory.
- Type:
- Example:
Run specified library
You can specify the library to build using the lib option. If this option is not specified, all libraries will be built.
See lib.id to learn how to get or set the ID of the library.
Watch file changes
To watch file changes and re-build, set the watch option to true.
Close build
rslib.build() returns a close() method that stops the build process.
In watch mode, calling the close() method will stop watching:
In non-watch mode, also call the close() method to end the build, which triggers the onCloseBuild hook of Rsbuild for cleanup operations.
Stats object
In non-watch mode, rslib.build() returns an Rspack stats object.
For example, use the stats.toJson() method to get asset information:
rslib.startMFDevServer
Start the dev server for the Module Federation format library. This method will:
- Start a dev server to serve your application
- Watch for file changes and trigger recompilation
- Type:
- Example:
Start dev server:
startMFDevServer returns these parameters:
urls: URLs to access dev server.port: The actual listening port number.server: Server instance object.
Run specified library
You can specify the library to start dev server using the lib option.
See lib.id to learn how to get or set the ID of the library.
Close server
Call the close() method to close the dev server, trigger the onCloseDevServer hook of Rsbuild, and perform cleanup operations.
rslib.inspectConfig
Inspects and debugs Rslib's internal configurations. It provides access to:
- The resolved Rslib configuration
- The resolved Rsbuild configuration
- The environment-specific Rsbuild configurations
- The generated Rspack configurations
The method serializes these configurations to strings and optionally writes them to disk for inspection.
- Type:
To view the configurations during the build process, use debug mode, or obtain them through Rsbuild hooks such as onBeforeBuild, onBeforeCreateCompiler in Rsbuild plugins.
- Example:
Get the content of configs in string format:
Write the config content to disk:
Setting mode
By default, mode is inferred from process.env.NODE_ENV. When mode is 'production', rslib.inspectConfig() outputs production mode configs for all libraries. You can also set mode to 'development' to output development mode configs only for libraries with format set to mf:
The Rslib config is resolved before inspectConfig() is called, so mode does not affect how it is loaded. To load different Rslib configs based on NODE_ENV, set it before calling createRslib().
Run specified library
You can specify the library to inspect configurations using the lib option. If this option is not specified, all libraries will be inspected.
See lib.id to learn how to get or set the ID of the library.
Output path
You can set the output path using outputPath. When writeToDisk is true, the files are written to the .rsbuild directory under output.distPath.root by default.
If outputPath is a relative path, it will be resolved relative to output.distPath.root. You can also set outputPath to an absolute path, in which case the files will be written directly to that path. For example:
rslib.getRslibConfig
Get the Rslib config.
- Type:
- Example:
rslib.onAfterCreateRsbuild
Called after the internal Rsbuild instance is created. You can access or call the properties and methods of the Rsbuild instance through this method.
- Type:
- Example:
All properties and methods on the Rsbuild instance object can be viewed in the Rsbuild instance document.
