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

# 数据上报

进行数据上报需要借助自定义拓展规则的接口，请先查看[自定义拓展规则](https://rsdoctor.rs/zh/guide/rules/rule-custom.md)。

自定义规则也可以收集和上传构建数据。`check` 可以是异步函数，Rsdoctor 会等待上传完成。请将示例 URL 替换为自己的接口：

```ts
// src/rules/upload-data.ts
import { defineRule } from '@rsdoctor/core';

export const UploadData = defineRule(() => ({
  meta: {
    category: 'bundle',
    severity: 'Warn',
    title: 'upload-data',
  },
  async check({ chunkGraph, moduleGraph }) {
    const response = await fetch('https://example.com/api/build-metrics', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({
        assetCount: chunkGraph.getAssets().length,
        moduleCount: moduleGraph.getModules().length,
      }),
    });
    if (!response.ok) throw new Error(`Upload failed: ${response.status}`);
  },
}));
```

```ts
// rsbuild.config.ts
import { RsdoctorRspackPlugin } from '@rsdoctor/core';
import { UploadData } from './rules/upload-data';

export default {
  tools: {
    bundlerChain: (chain) => {
      chain.plugin('Rsdoctor').use(RsdoctorRspackPlugin, [
        {
          linter: {
            level: 'Warn',
            extends: [UploadData],
          },
        },
      ]);
    },
  },
};
```
