Rslib's configuration is based on Rsbuild, which means that you can use all of Rsbuild configurations, as well as the lib
configuration specific to Rslib.
Rslib provides the lib
option to configure the library outputs. It is an array, and each object is used to describe a format of the output.
For example, output ESM and CJS formats, and use es2021
syntax:
You can set common Rsbuild configurations outside the lib
field, which will be inherited by each configuration object inside the lib
field.
For example, set the output.target of Rsbuild to web
, which will affect the output of all lib
configuration objects:
In the lib
field, you can set separate Rsbuild configurations for each output format, which will override the common Rsbuild configurations outside the lib
field.
For example, separately set the output.target of the ESM output to web
:
Rslib will generate the environments configuration of Rsbuild internally, please refer to Configuration Debug to view the final generated configuration.
You can also refer to the Configuration Overview page to view the detailed introduction of all configurations.
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:
Rslib allows you to use most of the Rsbuild configurations. Currently, the environments
config is not supported because it is generated internally by Rslib.
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.
Here is an example of a library that sets both CJS and ESM formats:
/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.