Rslib provides a wide range of configuration options and sets a common default value for each option, which can meet the requirements of most use cases. Therefore, in most cases, you can use Rslib out of the box with few configurations.
If you need to customize build behaviors, you can read the following sections to learn about the configuration options and its usage.
The configuration structure of Rslib looks like this:
The configuration file exports a default object that includes a lib
array and shared Rsbuild configuration options. You can find detailed descriptions of all configs on the Configure Overview page.
lib
array contains multiple objects, each representing a set of independent configurations that will generate different outputs. Each object within the lib
array can specify unique configurations, which is a superset of Rsbuild
configurations along with Rslib's specific configurations.lib
array, there are shared Rsbuild configuration options that will be deep merged with independent Rsbuild configuration of each lib
configuration object.Rslib generates corresponding Rsbuild environments configurations based on the multiple build configurations of different output formats. You can view the final generated configurations through the configuration debug documentation.
When you use the CLI of Rslib, Rslib will automatically read the configuration file in the root directory of the current project and resolve it in the following order:
rslib.config.mjs
rslib.config.ts
rslib.config.js
rslib.config.cjs
rslib.config.mts
rslib.config.cts
We recommend using the .mjs
or .ts
format for the configuration file and importing the defineConfig
utility function from @rslib/core
. It provides friendly TypeScript type hints and autocompletion, which can help you avoid errors in the configuration.
For example, in rslib.config.ts
, you can define the Rslib syntax configuration and the Rsbuild output.target configuration:
If you are developing a non-TypeScript project, you can use the .mjs
format for the configuration file.
When you use the .ts
, .mts
, and .cts
extensions, Rslib will use jiti to load configuration files, providing interoperability between ESM and CommonJS. The behavior of module resolution differs slightly from the native behavior of Node.js.
Rslib CLI uses the --config
option to specify the config file, which can be set to a relative path or an absolute path.
For example, if you need to use the rslib.prod.config.mjs
file when running build
, you can add the following scripts to package.json
:
You can also abbreviate the --config
option to -c
:
In the configuration file, you can use Node.js environment variables such as process.env.NODE_ENV
to dynamically set different configurations:
In Rslib project, you can configure Rsbuild related configurations in both lib
configuration object and shared configuration section.
Common-used Rsbuild configurations in library development are listed at Rsbuild Configuration.
You can also refer to the Rsbuild Configuration for checking the complete configuration items.
Rslib is built on top of Rsbuild and Rsbuild supports directly modifying the Rspack configuration object and also supports modifying the built-in Rspack configuration of Rsbuild through rspack-chain
. This means you can configure Rspack related configurations in an Rslib project as well.
For more details, refer to Configure Rspack.
You can enable Rslib's debug mode by adding the DEBUG=rsbuild
environment variable when executing a build. It will display the final Rsbuild/Rspack configuration after processing by Rslib.
In debug mode, Rslib will write the Rsbuild / Rspack config to the dist directory, which is convenient for developers to view and debug.
Below is an example of a configuration for Rslib that builds both CJS and ESM output.
/dist/.rsbuild/rsbuild.config.esm.mjs
file to see the complete content of the Rsbuild config./dist/.rsbuild/rspack.config.esm.mjs
file to see the complete content of the Rspack config.