CLI

Rslib comes with a lightweight CLI that includes commands such as rslib build and rslib inspect.

All commands

To view all available CLI commands, run the following command in the project directory:

npx rslib -h

The output is shown below:

Usage:
  $ rslib <command> [options]

Commands:
  build    build the library for production
  inspect  inspect the Rsbuild / Rspack configs of Rslib projects
  mf-dev   start Rsbuild dev server of Module Federation format

Common flags

Rslib CLI provides several common flags that can be used with all commands:

FlagDescription
-c, --config <config>Specify the configuration file, can be a relative or absolute path, see Specify config file
--env-dir <dir>Specify the directory to load .env files, see Rsbuild - Env directory
--env-mode <mode>Specify the env mode to load the .env.[mode] file, see Rsbuild - Env mode
-h, --helpDisplay help for command
--lib <id>Specify the library to run commands (repeatable, e.g. --lib esm --lib cjs), see lib.id to learn how to get or set the ID of the library
-r, --root <root>Specify the project root directory, can be an absolute path or a path relative to cwd

rslib build

The rslib build command will build the outputs for production in the dist/ directory by default.

Usage:
  $ rslib build

Options:
  -w --watch            turn on watch mode, watch for changes and rebuild

Environment variables

Rslib supports injecting env variables or expressions into the code during build, which is helpful for distinguishing the running environment or replacing constants.

You can see more details in Rsbuild - Environment variables.

NOTE
  • If format is esm or cjs, process.env.NODE_ENV will be preserved in the build output.
  • If format is mf or umd, process.env.NODE_ENV will be replaced to ensure that the output can run in the browser.

rslib inspect

The rslib inspect command is used to view the Rsbuild config and Rspack config of the Rslib project.

Usage:
  $ rslib inspect

Options:
  --output <output>     specify inspect content output path (default: ".rsbuild")
  --verbose             show full function definitions in output

When you run the command npx rslib inspect in the project root directory, the following files will be generated in the dist/.rsbuild directory of the project:

  • rsbuild.config.mjs: Represents the Rsbuild configuration used during the build.
  • rspack.config.web.mjs: Represents the Rspack configuration used during the build.
➜ npx rslib inspect

Inspect config succeed, open following files to view the content:

  - Rsbuild Config: /project/dist/.rsbuild/rsbuild.config.mjs
  - Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs

Verbose content

By default, the inspect command omits the content of functions in the configuration object. You can add the --verbose option to output the complete content of functions:

rslib inspect --verbose

Multiple output formats

If the current project has multiple output formats, such as ESM artifact and CJS artifact simultaneously, multiple Rspack configuration files will be generated in the dist/.rsbuild directory.

➜ npx rslib inspect

Inspect config succeed, open following files to view the content:

  - Rsbuild Config (esm): /project/dist/.rsbuild/rsbuild.config.esm.mjs
  - Rsbuild Config (cjs): /project/dist/.rsbuild/rsbuild.config.cjs.mjs
  - Rspack Config (esm): /project/dist/.rsbuild/rspack.config.esm.mjs
  - Rspack Config (cjs): /project/dist/.rsbuild/rspack.config.cjs.mjs

rslib mf-dev

The rslib mf-dev command is utilized to start Rsbuild dev server for the Module Federation format.

This enables you to develop and debug your mf format module within the host app.