> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# CLI

Rslib comes with a lightweight CLI that includes commands such as [rslib](#rslib) and [rslib inspect](#rslib-inspect).

## All commands

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

```bash
npx rslib -h
```

The output is shown below:

```bash
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:

| Flag                       | Description                                                                                                                                                    |
| -------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `-c, --config <config>`    | Specify the configuration file, can be a relative or absolute path, see [Specify config file](/guide/basic/configure-rslib.md#specify-config-file)             |
| `--config-loader <loader>` | Set the config file loader (`auto` \| `jiti` \| `native`), see [Specify config loader](/guide/basic/configure-rslib.md#specify-config-loader)                  |
| `--env-dir <dir>`          | Specify the directory to load `.env` files, see [Rsbuild - Env directory](https://rsbuild.rs/guide/advanced/env-vars#env-directory)                            |
| `--env-mode <mode>`        | Specify the env mode to load the `.env.[mode]` file, see [Rsbuild - Env mode](https://rsbuild.rs/guide/advanced/env-vars#env-mode)                             |
| `-h, --help`               | Display help for command                                                                                                                                       |
| `--lib <id>`               | Specify the library to run commands (repeatable, e.g. `--lib esm --lib cjs`), see [lib.id](/config/lib/id.md) 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](/config/rsbuild/log-level.md)                                                      |
| `--no-env`                 | Disable loading of `.env` files                                                                                                                                |
| `-r, --root <root>`        | Specify the project root directory, can be an absolute path or a path relative to cwd                                                                          |

## rslib

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

```bash
Usage:
  $ rslib

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](/guide/basic/configure-rslib.md#configuration-file) is not present in your project, the CLI will automatically use the default configuration containing only a single [lib](/config/lib/index.md) 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.
:::

### Watch mode

Use `rslib --watch` or `rslib -w` to enable watch mode. Rslib watches for source file changes and automatically rebuilds when they change.

If you need to adjust the configuration based on whether watch mode is enabled, check `process.argv`:

```ts title="rslib.config.ts"
import { defineConfig } from '@rslib/core';

const isWatch = process.argv.some((arg) => arg === '--watch' || arg === '-w');

export default defineConfig({
  source: {
    alias: {
      '@request': isWatch ? './src/request.dev.js' : './src/request.prod.js',
    },
  },
});
```

## rslib inspect

The `rslib inspect` command inspects a project's Rslib config and the corresponding Rsbuild and Rspack configs.

```bash
Usage:
  $ rslib inspect

Options:
  --mode <mode>         set the build mode (development | production)
  --output <output>     set the output path for inspection results (default: ".rsbuild")
  --verbose             show complete 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.esm.mjs`: Represents the Rspack configuration used during the build.
- `rslib.config.mjs`: Represents the resolved Rslib configuration.

```text
➜ npx rslib inspect

config inspection completed, generated files:

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

### Setting mode

By default, `mode` is inferred from `process.env.NODE_ENV`: it is `'development'` when `NODE_ENV` is `'development'`, and `'production'` otherwise.

When `mode` is `'production'`, the inspect command outputs production mode configurations for all libraries. You can add the `--mode development` option to output development mode configurations for libraries with [format](/config/lib/format.md) set to `mf`:

```bash
rslib inspect --mode development
```

### Verbose content

By default, the inspect command omits function bodies in the configuration object. To output complete function content, add the `--verbose` option:

```bash
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.

```text
➜ npx rslib inspect

config inspection completed, generated files:

  - 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](/guide/advanced/module-federation.md) format.

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