close

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 (default if no command is given)
  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
--config-loader <loader>Set the config file loader (auto | jiti | native), see Specify config loader
--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
--log-level <level>Set the log level (info | warn | error | silent), see logLevel
-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
  --entry <entry>        set entry file or pattern (repeatable)
  --dist-path <dir>      set output directory
  --bundle               enable bundle mode (use --no-bundle to disable)
  --format <format>      specify the output format (esm | cjs | umd | mf | iife)
  --syntax <syntax>      set build syntax target (repeatable)
  --target <target>      set runtime target (web | node)
  --dts                  emit declaration files (use --no-dts to disable)
  --externals <pkg>      add package to externals (repeatable)
  --minify               minify output (use --no-minify to disable)
  --clean                clean output directory before build (use --no-clean to disable)
  --auto-extension       control automatic extension redirect (use --no-auto-extension to disable)
  --auto-external        control automatic dependency externalization (use --no-auto-external to disable)
  --tsconfig <path>      use specific tsconfig (relative to project root)
Note

If the Rslib configuration file is not present in your project, the CLI will automatically use the default configuration containing only a single lib and apply all build options from the command line. You can add a configuration file once you need a more complex configuration or want to build outputs in multiple formats.

Environment variables

Rslib supports injecting environment variables or expressions into the code during the build, which is helpful for distinguishing running environments or replacing constants. You can see more details in Rsbuild - Environment variables.

By default, Rslib sets the process.env.NODE_ENV environment variable, which is always 'production' during the build. If you need to distinguish watch mode to dynamically set different configurations, you can set as follows:

rslib.config.ts
import { defineConfig } from '@rslib/core';

const isWatch = process.argv.includes('--watch');

export default defineConfig({
  lib: [
    {
      format: 'esm',
    },
  ],
  source: {
    alias: {
      '@request': isWatch ? './src/request.dev.js' : './src/request.prod.js',
    },
  },
});
Note
  • If format is esm or cjs, process.env.NODE_ENV in source code will be preserved in the build output.
  • If format is mf or umd, process.env.NODE_ENV in source code 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.
  • rslib.config.mjs: Represents the final Rslib configuration after normalization.
➜ 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
  - Rslib Config: /project/dist/.rsbuild/rslib.config.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 Config: /project/dist/.rsbuild/rslib.config.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.