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 BuildOptions = {
/**
* Specify library id
*/
lib?: string[];
/**
* Whether to watch for file changes and rebuild.
*
* @default false
*/
watch?: boolean;
};
function Build(options?: BuildOptions): Promise<{
/**
* Rspack's [stats](https://rspack.rs/api/javascript-api/stats) object.
*/
stats?: Rspack.Stats | Rspack.MultiStats;
/**
* Close the build and call the `onCloseBuild` hook.
* In watch mode, this method will stop watching.
*/
close: () => Promise<void>;
}>;
// Example 1: run build
await rslib.build();
// Example 2: run build of a specified library id
await rslib.build({
lib: ['esm'],
});
// Example 3: build and get all assets
const { stats } = await rslib.build();
if (stats) {
const { assets } = stats.toJson({
// exclude unused fields to improve performance
all: false,
assets: true,
});
console.log(assets);
}
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.
await rslib.build({
lib: ['cjs', 'esm'],
});
Watch file changes
To watch file changes and re-build, set the watch option to true.
await rslib.build({
watch: true,
});
Close build
rslib.build() returns a close() method that stops the build process.
In watch mode, calling the close() method will stop watching:
const buildResult = await rslib.build({
watch: true,
});
await buildResult.close();
In non-watch mode, also call the close() method to end the build, which triggers the onCloseBuild hook of Rsbuild for cleanup operations.
const buildResult = await rslib.build();
await buildResult.close();
Stats object
In non-watch mode, rslib.build() returns an Rspack stats object.
For example, use the stats.toJson() method to get asset information:
const result = await rslib.build();
const { stats } = result;
if (stats) {
const { assets } = stats.toJson({
// exclude unused fields to improve performance
all: false,
assets: true,
});
console.log(assets);
}
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 StartMFDevServerOptions = {
/**
* Specify library id
*/
lib?: string[];
};
type StartServerResult = {
/**
* The URLs that server is listening on.
*/
urls: string[];
/**
* The actual port used by the server.
*/
port: number;
server: {
/**
* Close the server.
* In development mode, this will call the `onCloseDevServer` hook.
*/
close: () => Promise<void>;
};
};
function startMFDevServer(
options?: StartMFDevServerOptions,
): Promise<StartServerResult>;
Start dev server:
// Start dev server
await rslib.startMFDevServer();
// Start dev server of a specified library id
await rslib.startMFDevServer({
lib: ['entry1'],
});
startMFDevServer returns these parameters:
urls: URLs to access dev server.
port: The actual listening port number.
server: Server instance object.
const { port } = await rslib.startMFDevServer();
console.log(port); // 3000
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.
await rslib.startMFDevServer({
lib: ['entry1'],
});
Close server
Call the close() method to close the dev server, trigger the onCloseDevServer hook of Rsbuild, and perform cleanup operations.
const { server } = await rslib.startMFDevServer();
await server.close();
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 InspectConfigOptions = {
/**
* Specify library id
*/
lib?: string[];
/**
* Inspect the config in the specified mode.
* Available options: 'development' or 'production'.
* @default 'production'
*/
mode?: RsbuildMode;
/**
* Enables verbose mode to display the complete function
* content in the configuration.
* @default false
*/
verbose?: boolean;
/**
* Specify the output path for inspection results.
* @default 'output.distPath.root'
*/
outputPath?: string;
/**
* Whether to write the inspection results to disk.
* @default false
*/
writeToDisk?: boolean;
};
function inspectConfig(options?: InspectConfigOptions): Promise<{
rslibConfig: string;
rsbuildConfig: string;
bundlerConfigs: string[];
environmentConfigs: string[];
origin: {
rsbuildConfig: RsbuildConfig;
environmentConfigs: Record<string, EnvironmentConfig>;
bundlerConfigs: Rspack.Configuration[];
};
}>;
Get the content of configs in string format:
const { rslibConfig, rsbuildConfig, bundlerConfigs } =
await rslib.inspectConfig();
console.log(rslibConfig, rsbuildConfig, bundlerConfigs);
Write the config content to disk:
await rslib.inspectConfig({
writeToDisk: true,
});
可以通过 lib 参数指定需要查看配置的库 id。如果不指定该参数,则会输出所有配置。
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.
await rslib.inspectConfig({
lib: ['cjs', 'esm'],
});
Output path
You can set the output path using outputPath. The default value is output.distPath.root.
If outputPath is a relative path, it will be concatenated relative to the value of 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:
import path from 'node:path';
await rslib.inspectConfig({
writeToDisk: true,
outputPath: path.join(__dirname, 'custom-dir'),
});
rslib.getRslibConfig
Get the Rslib config.
function getRslibConfig(): Readonly<RslibConfig>;
import { createRslib } from '@rslib/core';
const rslib = await createRslib();
const config = rslib.getRslibConfig();
console.log(config.lib);
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 OnAfterCreateRsbuildFn = (params: {
rsbuild: RsbuildInstance;
}) => void | Promise<void>;
function onAfterCreateRsbuild(callback: OnAfterCreateRsbuildFn): void;
const rslib = await createRslib();
rslib.onAfterCreateRsbuild(({ rsbuild }) => {
rsbuild.onAfterBuild(() => {
console.log('build done');
});
});
await rslib.build();
All properties and methods on the Rsbuild instance object can be viewed in the Rsbuild instance document.