For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /guide/basic/cli.md.
close
  • English
  • CLI

    Rslib comes with a lightweight CLI that includes commands such as rslib 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
    --no-envDisable 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.

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

    Detecting watch mode

    The rslib --watch command builds in watch mode. If you need to use different configuration in watch mode, check whether --watch is present in process.argv:

    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',
        },
      },
    });

    rslib inspect

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

    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.
    ➜ 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 set to mf:

    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:

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

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