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

# 使用 Rsdoctor

[Rsdoctor](https://rsdoctor.rs/zh/) 是为 Rspack 生态打造的构建分析工具，通过可视化呈现构建过程与产物，帮助开发者排查构建问题、定位性能瓶颈；同时为 Agent 提供构建数据，支持对 JavaScript 程序的自动诊断与优化。

## 快速上手

在基于 Rslib 的项目中，你可以通过以下步骤开启 Rsdoctor 分析：

1. 安装 Rsdoctor 插件：


```sh [npm]
npm add @rsdoctor/core@latest -D
```

```sh [yarn]
yarn add @rsdoctor/core@latest -D
```

```sh [pnpm]
pnpm add @rsdoctor/core@latest -D
```

```sh [bun]
bun add @rsdoctor/core@latest -D
```

```sh [deno]
deno add npm:@rsdoctor/core@latest -D
```

从 Rsdoctor 1.x 升级，请参考 [Rsdoctor 2.0 迁移指南](https://rsdoctor.rs/zh/guide/migration/migration-v2)。

2. 在 CLI 命令前添加 `RSDOCTOR=true` 环境变量：

```json title="package.json"
{
  "scripts": {
    "build:rsdoctor": "RSDOCTOR=true rslib"
  }
}
```

由于 Windows 不支持上述用法，你也可以使用 [cross-env](https://npmjs.com/package/cross-env) 来设置环境变量，这可以确保在不同的操作系统中都能正常使用：

```json title="package.json"
{
  "scripts": {
    "build:rsdoctor": "cross-env RSDOCTOR=true rslib"
  },
  "devDependencies": {
    "cross-env": "^7.0.0"
  }
}
```

在项目内执行上述命令后，Rslib 会自动注册 Rsdoctor 的插件，并在构建完成后打开本次构建的分析页面，参考 [Rsdoctor 文档](https://rsdoctor.rs/) 来了解完整功能。

## 配置项

如果你需要配置 Rsdoctor 插件提供的 [选项](https://rsdoctor.rs/zh/config/options/options)，可以手动注册 Rsdoctor 插件：

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

export default defineConfig({
  tools: {
    rspack: {
      plugins: [
        process.env.RSDOCTOR === 'true' &&
          new RsdoctorRspackPlugin({
            // 插件选项
          }),
      ],
    },
  },
});
```
