miaoda-react-devkit 0.1.1-beta.1 → 0.1.1-beta.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/cloud/vite.config.js +118 -0
- package/dist/index.cjs +253 -69
- package/dist/index.d.cts +111 -2
- package/dist/index.d.ts +111 -2
- package/dist/index.js +250 -69
- package/dist/stubs/sentry-react-noop-stub.js +78 -0
- package/dist/stubs/sentry-react-stub.js +187 -69
- package/dist/vite/index.cjs +20 -1
- package/dist/vite/index.d.cts +3 -1
- package/dist/vite/index.d.ts +3 -1
- package/dist/vite/index.js +19 -1
- package/package.json +3 -1
- package/README.md +0 -181
- package/cloud/vite.config.ts +0 -156
package/README.md
DELETED
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
# miaoda-react-devkit
|
|
2
|
-
|
|
3
|
-
React 管理后台开发工具集。提供 Sentry 开发环境 stub、错误桥接上报,以及云端 dev server 的 vite 配置。
|
|
4
|
-
|
|
5
|
-
## 功能
|
|
6
|
-
|
|
7
|
-
开发模式下,`@sentry/react` 会被自动替换为 stub:
|
|
8
|
-
|
|
9
|
-
- DSN 替换为无害的 stub 端点(project id = 0),错误不会上报到真实 Sentry
|
|
10
|
-
- 捕获到错误时在控制台打印 `[SentryStub]` 前缀的 warn,含错误消息、错误类型和调用栈
|
|
11
|
-
- 每个 fetch / XHR 请求完成后触发 `onNetworkRequest` 回调,可拿到请求体和响应体文本
|
|
12
|
-
- 4xx 请求和网络层失败(fetch 抛出异常)通过 `window.parent.postMessage` 向父窗口发送 `GLOBAL_ERROR` 消息
|
|
13
|
-
- 每次 `console.xxx` 调用触发 `onConsole` 回调,原始输出不受影响,无递归风险
|
|
14
|
-
- 用户传入的 `beforeSend` / `beforeBreadcrumb` 回调仍会被链式调用
|
|
15
|
-
- SSE / 流式响应自动跳过,不消费原始 stream,不影响业务代码
|
|
16
|
-
- 生产构建时插件不生效,恢复正常上报
|
|
17
|
-
|
|
18
|
-
## 目录结构
|
|
19
|
-
|
|
20
|
-
```
|
|
21
|
-
packages/devkit/
|
|
22
|
-
├── src/
|
|
23
|
-
│ ├── index.ts 主入口,导出所有公共 API
|
|
24
|
-
│ ├── sentry/ Sentry 事件捕获核心
|
|
25
|
-
│ │ ├── SentryCapture.ts SentryCapture 类,协调三类观测器
|
|
26
|
-
│ │ ├── consoleObserver.ts 控制台日志捕获与过滤
|
|
27
|
-
│ │ ├── networkObserver.ts 网络请求捕获(fetch / XHR)
|
|
28
|
-
│ │ └── types.ts 共享类型定义(IErrorCaptureInfo 等)
|
|
29
|
-
│ ├── bridge/ 错误桥接:向父窗口发送 postMessage
|
|
30
|
-
│ │ └── errorBridge.ts createErrorBridge,格式对齐 vite-plugin-monitor
|
|
31
|
-
│ ├── vite/ Vite 插件
|
|
32
|
-
│ │ ├── index.ts vite 插件导出
|
|
33
|
-
│ │ ├── withSentryStub.ts dev 模式下将 @sentry/react 重定向到 stub
|
|
34
|
-
│ │ └── hmrTogglePlugin.ts HMR 开关及手动 reload 控制接口
|
|
35
|
-
│ └── stubs/
|
|
36
|
-
│ └── sentry-react-stub.ts dev 模式 stub,透传真实 Sentry API
|
|
37
|
-
└── cloud/
|
|
38
|
-
└── vite.config.ts 云端 dev server 专用 vite 配置
|
|
39
|
-
```
|
|
40
|
-
|
|
41
|
-
## 使用
|
|
42
|
-
|
|
43
|
-
### 本地开发
|
|
44
|
-
|
|
45
|
-
在项目 `vite.config.ts` 中注册插件(需放在第一位):
|
|
46
|
-
|
|
47
|
-
```ts
|
|
48
|
-
import { withSentryStub } from 'miaoda-react-devkit/vite';
|
|
49
|
-
|
|
50
|
-
export default defineConfig({
|
|
51
|
-
plugins: [withSentryStub(), react()],
|
|
52
|
-
});
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
应用代码中正常使用 `@sentry/react`,无需任何改动。
|
|
56
|
-
|
|
57
|
-
### 云端 dev server
|
|
58
|
-
|
|
59
|
-
云端 dev server 指定 `cloud/vite.config.ts` 启动,项目自身的 `vite.config.ts` 无需改动:
|
|
60
|
-
|
|
61
|
-
```bash
|
|
62
|
-
vite --config node_modules/miaoda-react-devkit/cloud/vite.config.ts
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
`cloud/vite.config.ts` 通过 `loadConfigFromFile` 加载项目的 `vite.config.ts`,再用 `mergeConfig` 追加云端专用插件:
|
|
66
|
-
|
|
67
|
-
| 插件 | 说明 |
|
|
68
|
-
|------|------|
|
|
69
|
-
| `withSentryStub` **或** `monitorPlugin` | 二选一,见下方说明 |
|
|
70
|
-
| `makeTagger` | DOM 节点打标(仅 web 类型) |
|
|
71
|
-
| `injectedGuiListenerPlugin` | 注入云端 GUI 监听脚本 |
|
|
72
|
-
| `injectOnErrorPlugin` | 全局错误捕获 |
|
|
73
|
-
| `hmrTogglePlugin` | HMR 开关及手动 reload 控制接口 |
|
|
74
|
-
|
|
75
|
-
**Sentry 插件自动选择**:启动时扫描项目入口文件(`src/main.tsx` 等),若检测到 `import ... from '@sentry/react'`,则使用 `withSentryStub`(新架构:项目自行调用 `Sentry.init()`,stub 拦截 dev 上报);否则使用 `monitorPlugin`(旧架构:通过 CDN 脚本注入 Sentry)。两者互斥,不会同时生效。
|
|
76
|
-
|
|
77
|
-
启动前由 `ViteDevServerStrategy` 通过环境变量传入参数:
|
|
78
|
-
|
|
79
|
-
| 环境变量 | 说明 |
|
|
80
|
-
|----------|------|
|
|
81
|
-
| `DEVKIT_APP_TYPE` | `'web'` 或 `'freestyle'`,决定是否注入 `makeTagger()` |
|
|
82
|
-
| `DEVKIT_CDN_HOST` | CDN 域名 |
|
|
83
|
-
| `DEVKIT_ENV` | 部署环境(`'online'` \| `'dev'` \| `'qa'` 等) |
|
|
84
|
-
| `DEVKIT_SENTRY_DSN` | 云端 Sentry DSN |
|
|
85
|
-
| `DEVKIT_MIAODA_ENV` | miaoda 环境标识 |
|
|
86
|
-
| `DEVKIT_APP_ID` | 应用 ID |
|
|
87
|
-
|
|
88
|
-
### 自定义观测回调(可选)
|
|
89
|
-
|
|
90
|
-
```ts
|
|
91
|
-
import * as Sentry from '@sentry/react';
|
|
92
|
-
import { SentryCapture, DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS } from 'miaoda-react-devkit';
|
|
93
|
-
|
|
94
|
-
const capture = new SentryCapture({
|
|
95
|
-
consoleFilterKeywords: DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
96
|
-
ignoreUrls: DEFAULT_IGNORE_URLS,
|
|
97
|
-
onError(info) {
|
|
98
|
-
console.warn('错误:', info.errorName, info.message);
|
|
99
|
-
console.warn('位置:', info.fileName, info.lineNumber, info.columnNumber);
|
|
100
|
-
},
|
|
101
|
-
onNetworkRequest(info) {
|
|
102
|
-
console.log(`[${info.type}] ${info.method} ${info.url} → ${info.statusCode}`);
|
|
103
|
-
if (info.requestBody) console.log('请求体:', info.requestBody);
|
|
104
|
-
if (info.responseBody) console.log('响应体:', info.responseBody);
|
|
105
|
-
if (info.cause) console.error('网络失败:', info.cause);
|
|
106
|
-
},
|
|
107
|
-
onConsole({ level, args }) {
|
|
108
|
-
console.debug(`[console.${level}]`, ...args);
|
|
109
|
-
},
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
Sentry.init({
|
|
113
|
-
dsn: '...',
|
|
114
|
-
beforeSend: capture.beforeSend,
|
|
115
|
-
beforeBreadcrumb: capture.beforeBreadcrumb,
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
capture.installNetworkObserver();
|
|
119
|
-
capture.installConsoleObserver();
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
### 单独使用 ErrorBridge
|
|
123
|
-
|
|
124
|
-
`createErrorBridge` 可独立于 `SentryCapture` 使用,也可注入自定义 `send` 函数便于测试:
|
|
125
|
-
|
|
126
|
-
```ts
|
|
127
|
-
import { createErrorBridge } from 'miaoda-react-devkit';
|
|
128
|
-
|
|
129
|
-
// 默认:window.parent.postMessage
|
|
130
|
-
const bridge = createErrorBridge();
|
|
131
|
-
|
|
132
|
-
// 自定义(测试或特殊宿主环境)
|
|
133
|
-
const bridge = createErrorBridge((msg, origin) => {
|
|
134
|
-
customChannel.send(msg);
|
|
135
|
-
});
|
|
136
|
-
```
|
|
137
|
-
|
|
138
|
-
`IGlobalErrorMessage` 格式与 `vite-plugin-monitor` 保持一致,接收端可统一处理两种来源的错误消息。
|
|
139
|
-
|
|
140
|
-
## 类型参考
|
|
141
|
-
|
|
142
|
-
### IErrorCaptureInfo
|
|
143
|
-
|
|
144
|
-
| 字段 | 类型 | 说明 |
|
|
145
|
-
|------|------|------|
|
|
146
|
-
| `message` | `string` | 错误消息 |
|
|
147
|
-
| `stack` | `string \| undefined` | 完整调用栈文本 |
|
|
148
|
-
| `errorName` | `string \| undefined` | 错误类型名,如 `'TypeError'`、`'RangeError'` |
|
|
149
|
-
| `fileName` | `string \| undefined` | 抛出错误的文件路径(来自 Sentry stack frame) |
|
|
150
|
-
| `lineNumber` | `number \| undefined` | 行号 |
|
|
151
|
-
| `columnNumber` | `number \| undefined` | 列号 |
|
|
152
|
-
| `componentStack` | `string \| undefined` | React 组件调用栈(ErrorBoundary 提供) |
|
|
153
|
-
| `mechanismType` | `string \| null \| undefined` | 捕获方式,如 `'instrument'`、`'onerror'` |
|
|
154
|
-
|
|
155
|
-
### INetworkRequestInfo
|
|
156
|
-
|
|
157
|
-
| 字段 | 类型 | 说明 |
|
|
158
|
-
|------|------|------|
|
|
159
|
-
| `type` | `'fetch' \| 'xhr'` | 请求类型 |
|
|
160
|
-
| `method` | `string` | HTTP 方法 |
|
|
161
|
-
| `url` | `string` | 请求 URL |
|
|
162
|
-
| `statusCode` | `number \| null` | HTTP 状态码,网络层失败时为 null |
|
|
163
|
-
| `requestBody` | `string \| undefined` | 请求体文本,Blob/ArrayBuffer 等二进制类型为 undefined |
|
|
164
|
-
| `responseBody` | `string \| undefined` | 响应体文本,SSE 流式响应为 `'[streaming response - not captured]'` |
|
|
165
|
-
| `isError` | `boolean` | 网络层失败(未收到 HTTP 响应)时为 true |
|
|
166
|
-
| `cause` | `Error \| undefined` | 网络层失败时的原始 Error(仅 fetch 可用;XHR 无法获取) |
|
|
167
|
-
|
|
168
|
-
## 其他环境变量
|
|
169
|
-
|
|
170
|
-
| 变量 | 说明 |
|
|
171
|
-
|------|------|
|
|
172
|
-
| `SENTRY_OVERRIDE_DSN` | 覆盖 stub DSN,可指向本地 Sentry Relay |
|
|
173
|
-
| `VITE_SENTRY_STUB_LOG` | 设为 `'true'` 时开启 stub 日志输出(仅 E2E 测试使用) |
|
|
174
|
-
|
|
175
|
-
## 构建
|
|
176
|
-
|
|
177
|
-
```bash
|
|
178
|
-
pnpm build
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
E2E 测试见 `../devkit-e2e`。
|
package/cloud/vite.config.ts
DELETED
|
@@ -1,156 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 云端 dev server 专用 vite 配置。
|
|
3
|
-
* 用法:vite --config node_modules/miaoda-react-devkit/cloud/vite.config.ts
|
|
4
|
-
*
|
|
5
|
-
* 启动前由 ViteDevServerStrategy 注入以下环境变量:
|
|
6
|
-
* DEVKIT_APP_TYPE — 'web' | 'freestyle',决定是否注入 makeTagger()
|
|
7
|
-
* DEVKIT_CDN_HOST — CDN 域名,用于拼接 injectedGui 和 monitor 脚本地址
|
|
8
|
-
* DEVKIT_ENV — 部署环境('online' | 'dev' | 'qa' 等),online 时 CDN 路径不含环境前缀
|
|
9
|
-
* DEVKIT_SENTRY_DSN — 云端 Sentry DSN,传给 monitorPlugin
|
|
10
|
-
* DEVKIT_MIAODA_ENV — miaoda 环境标识,传给 monitorPlugin
|
|
11
|
-
* DEVKIT_APP_ID — 应用 ID,传给 monitorPlugin
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
import * as vite from 'vite';
|
|
15
|
-
import { loadConfigFromFile, mergeConfig, type Plugin, type PluginOption, type UserConfig } from 'vite';
|
|
16
|
-
import { withSentryStub, hmrTogglePlugin } from '../dist/vite/index.js';
|
|
17
|
-
// @ts-ignore — miaoda-sc-plugin ships no type declarations
|
|
18
|
-
import {
|
|
19
|
-
makeTagger,
|
|
20
|
-
injectedGuiListenerPlugin,
|
|
21
|
-
injectOnErrorPlugin,
|
|
22
|
-
monitorPlugin,
|
|
23
|
-
fontsCodePlugin,
|
|
24
|
-
mdClickPlugin,
|
|
25
|
-
extractRouterPlugin,
|
|
26
|
-
patchSupabasePlugins,
|
|
27
|
-
} from 'miaoda-sc-plugin';
|
|
28
|
-
|
|
29
|
-
import path from 'node:path';
|
|
30
|
-
import fs from 'node:fs';
|
|
31
|
-
|
|
32
|
-
// vite --config 始终从项目根目录调用,cwd 即为项目根,不依赖目录层级假设
|
|
33
|
-
const projectRoot: string = process.cwd();
|
|
34
|
-
|
|
35
|
-
const appType: string = process.env.DEVKIT_APP_TYPE ?? 'freestyle';
|
|
36
|
-
const cdnHost: string = process.env.DEVKIT_CDN_HOST ?? 'resource-static.cdn.bcebos.com';
|
|
37
|
-
const env: string = process.env.DEVKIT_ENV ?? '';
|
|
38
|
-
const sentryDsn: string = process.env.DEVKIT_SENTRY_DSN ?? '';
|
|
39
|
-
const miaodaEnv: string = process.env.DEVKIT_MIAODA_ENV ?? '';
|
|
40
|
-
const appId: string = process.env.DEVKIT_APP_ID ?? '';
|
|
41
|
-
const isOnline: boolean = env === 'online';
|
|
42
|
-
|
|
43
|
-
// 加载项目自身的 vite.config.ts(不含云端插件)
|
|
44
|
-
const loaded = await loadConfigFromFile(
|
|
45
|
-
{ command: 'serve', mode: 'development' },
|
|
46
|
-
path.join(projectRoot, 'vite.config.ts'),
|
|
47
|
-
projectRoot,
|
|
48
|
-
);
|
|
49
|
-
const projectConfig: UserConfig = loaded?.config ?? {};
|
|
50
|
-
|
|
51
|
-
const configPath: string = path.join(projectRoot, 'vite.config.ts');
|
|
52
|
-
if (!loaded) {
|
|
53
|
-
throw new Error(
|
|
54
|
-
`[miaoda-devkit] 找不到项目 vite 配置:${configPath}\n` +
|
|
55
|
-
`请确认项目根目录下存在 vite.config.ts`,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
const plugins: Plugin[] = ((projectConfig.plugins ?? []) as unknown[]).flat(Infinity).filter((p): p is Plugin => !!p);
|
|
59
|
-
const hasReact: boolean = plugins.some(p => p.name === 'vite:react-babel' || p.name === 'vite:react-swc');
|
|
60
|
-
if (!hasReact) {
|
|
61
|
-
throw new Error(
|
|
62
|
-
`[miaoda-devkit] 项目 vite 配置缺少 React 插件。\n` +
|
|
63
|
-
`请在 ${configPath} 的 plugins 中添加 react()(@vitejs/plugin-react 或 @vitejs/plugin-react-swc)`,
|
|
64
|
-
);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
// warmup 在服务启动时预 transform 入口文件,减少运行时 re-optimize 次数
|
|
68
|
-
const possibleEntries: string[] = ['./src/main.tsx', './src/main.ts', './src/index.tsx', './src/index.ts'];
|
|
69
|
-
const warmupFiles: string[] = possibleEntries.filter(e => fs.existsSync(path.join(projectRoot, e)));
|
|
70
|
-
|
|
71
|
-
// 检测项目入口文件是否真正 import 了 @sentry/react(新架构的判断依据)
|
|
72
|
-
// 仅装了包但没有在入口引入不算;必须有 import ... from '@sentry/react' 才触发新架构
|
|
73
|
-
const SENTRY_IMPORT_RE = /from\s+['"]@sentry\/react['"]/;
|
|
74
|
-
|
|
75
|
-
function entryHasSentryImport(root: string, entries: string[]): boolean {
|
|
76
|
-
for (const entry of entries) {
|
|
77
|
-
const fullPath = path.join(root, entry);
|
|
78
|
-
if (!fs.existsSync(fullPath)) {
|
|
79
|
-
continue
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
if (SENTRY_IMPORT_RE.test(fs.readFileSync(fullPath, 'utf-8'))) {
|
|
84
|
-
return true
|
|
85
|
-
}
|
|
86
|
-
} catch {
|
|
87
|
-
// ignore unreadable files
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
return false;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const hasSentryReact: boolean = entryHasSentryImport(projectRoot, possibleEntries);
|
|
94
|
-
|
|
95
|
-
interface ViteVersionInfo {
|
|
96
|
-
/** vite 自身版本号,例如 "5.4.0" */
|
|
97
|
-
version: string;
|
|
98
|
-
/** rollup 版本号;使用标准 rollup 构建器时存在,rolldown 构建器时为 null */
|
|
99
|
-
rollupVersion: string | null;
|
|
100
|
-
/** rolldown 版本号;仅在 rolldown-vite 分支时存在,标准 vite 时为 null */
|
|
101
|
-
rolldownVersion: string | null;
|
|
102
|
-
/** 当前 vite 是否为 rolldown-vite(基于 rolldown 的实验性构建器) */
|
|
103
|
-
isRolldownVite: boolean;
|
|
104
|
-
}
|
|
105
|
-
// 注入到 define,供运行时代码读取当前 vite 构建器信息
|
|
106
|
-
const viteVersionInfo: ViteVersionInfo = {
|
|
107
|
-
version: vite.version,
|
|
108
|
-
rollupVersion: (vite as { rollupVersion?: string }).rollupVersion ?? null,
|
|
109
|
-
rolldownVersion: (vite as { rolldownVersion?: string }).rolldownVersion ?? null,
|
|
110
|
-
isRolldownVite: 'rolldownVersion' in vite,
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
// online 环境 CDN 路径不含环境前缀,其他环境插入 /{env}/ 段
|
|
114
|
-
const injectedGuiUrl: string = `https://${cdnHost}/common/v2/${isOnline ? '' : `${env}/`}injected.js`;
|
|
115
|
-
|
|
116
|
-
// hasSentryReact=true:新架构,项目自行 init Sentry,用 withSentryStub 拦截 dev 上报
|
|
117
|
-
// hasSentryReact=false:旧架构,由 monitorPlugin 通过 CDN 注入 Sentry
|
|
118
|
-
const cloudPlugins: PluginOption[] = [
|
|
119
|
-
...(hasSentryReact
|
|
120
|
-
? [withSentryStub()]
|
|
121
|
-
: [monitorPlugin({
|
|
122
|
-
scriptSrc: `https://${cdnHost}/sentry/browser.sentry.min.js`,
|
|
123
|
-
sentryDsn,
|
|
124
|
-
environment: miaodaEnv,
|
|
125
|
-
appId,
|
|
126
|
-
})]
|
|
127
|
-
),
|
|
128
|
-
// web 类型需要 DOM 节点打标(GUI使用)
|
|
129
|
-
...(appType === 'web'
|
|
130
|
-
? [
|
|
131
|
-
makeTagger(),
|
|
132
|
-
mdClickPlugin({ appType }),
|
|
133
|
-
fontsCodePlugin(),
|
|
134
|
-
injectedGuiListenerPlugin({ path: injectedGuiUrl })
|
|
135
|
-
]
|
|
136
|
-
: []),
|
|
137
|
-
injectedGuiListenerPlugin({ path: injectedGuiUrl }),
|
|
138
|
-
injectOnErrorPlugin(),
|
|
139
|
-
hmrTogglePlugin(),
|
|
140
|
-
extractRouterPlugin(),
|
|
141
|
-
patchSupabasePlugins(),
|
|
142
|
-
];
|
|
143
|
-
|
|
144
|
-
const cloudConfig: UserConfig = {
|
|
145
|
-
define: {
|
|
146
|
-
__VITE_INFO__: JSON.stringify(viteVersionInfo),
|
|
147
|
-
},
|
|
148
|
-
// 将缓存目录固定到项目本地,避免在 /workspace/node_modules/ 下创建
|
|
149
|
-
cacheDir: path.resolve(projectRoot, 'node_modules/.vite'),
|
|
150
|
-
server: {
|
|
151
|
-
warmup: warmupFiles.length > 0 ? { clientFiles: warmupFiles } : {},
|
|
152
|
-
},
|
|
153
|
-
plugins: cloudPlugins,
|
|
154
|
-
};
|
|
155
|
-
|
|
156
|
-
export default mergeConfig(projectConfig, cloudConfig);
|