close
logo
Rslib
Guide
Config
Blog
English
简体中文
Guide
Config
Blog
English
简体中文
logo
Rslib

Getting Started

Introduction
Quick start
Glossary
Packages

Solution

Node.js
React
Vue

Basic

CLI
Configure Rslib
Use TypeScript
Output format
Output structure
Upgrade Rslib

Advanced

Handle third-party dependencies
Output compatibility
Declaration files
Import static assets
Import SVGR
Import JSON files
Module Federation
Use Storybook

Migration

Modern.js Module
tsup

FAQ

Features FAQ
📝 Edit this page on GitHub
Previous PageVue
Next PageConfigure Rslib

#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
--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

#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.