#数据上报
进行数据上报需要借助自定义拓展规则的接口,请先查看自定义拓展规则。
自定义规则也可以收集和上传构建数据。check 可以是异步函数,Rsdoctor 会等待上传完成。请将示例 URL 替换为自己的接口:
// 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}`);
},
}));// 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],
},
},
]);
},
},
};
