miaoda-react-devkit 0.1.1-beta.4 → 0.1.1-beta.6
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/dist/index.cjs +113 -1
- package/dist/index.d.cts +60 -1
- package/dist/index.d.ts +60 -1
- package/dist/index.js +111 -1
- package/dist/stubs/sentry-react-stub.js +107 -1
- package/package.json +2 -1
- package/cloud/vite.config.ts +0 -156
package/dist/index.cjs
CHANGED
|
@@ -22,6 +22,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
|
|
|
22
22
|
// src/index.ts
|
|
23
23
|
var src_exports = {};
|
|
24
24
|
__export(src_exports, {
|
|
25
|
+
DEFAULT_AUTH_URL_PATTERNS: () => DEFAULT_AUTH_URL_PATTERNS,
|
|
26
|
+
DEFAULT_AUTH_WHITELIST_CODES: () => DEFAULT_AUTH_WHITELIST_CODES,
|
|
25
27
|
DEFAULT_CONSOLE_FILTER_KEYWORDS: () => DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
26
28
|
DEFAULT_IGNORE_URLS: () => DEFAULT_IGNORE_URLS,
|
|
27
29
|
DEFAULT_PGREST_URL_PATTERNS: () => DEFAULT_PGREST_URL_PATTERNS,
|
|
@@ -116,7 +118,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
|
|
|
116
118
|
|
|
117
119
|
// src/capture/pgrest.ts
|
|
118
120
|
var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
|
|
119
|
-
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
121
|
+
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
122
|
+
"22P02",
|
|
123
|
+
// 无效的文本表示(如类型转换失败)
|
|
124
|
+
"42703",
|
|
125
|
+
// 未定义的列
|
|
126
|
+
"42P01",
|
|
127
|
+
// 未定义的表
|
|
128
|
+
"42501",
|
|
129
|
+
// 权限不足
|
|
130
|
+
"23503",
|
|
131
|
+
// 外键约束失败
|
|
132
|
+
"42P17",
|
|
133
|
+
// 对象已存在
|
|
134
|
+
"22008",
|
|
135
|
+
// 日期时间字段值超出范围
|
|
136
|
+
"23505",
|
|
137
|
+
// 唯一键冲突
|
|
138
|
+
"PGRST116",
|
|
139
|
+
// 期望单行但返回多行
|
|
140
|
+
"PGRST200",
|
|
141
|
+
// 外键关系查找失败
|
|
142
|
+
"PGRST204"
|
|
143
|
+
// 响应中找不到列
|
|
144
|
+
];
|
|
120
145
|
function isPgRestRequest(url, patterns) {
|
|
121
146
|
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
122
147
|
}
|
|
@@ -141,6 +166,32 @@ function parsePgRestErrorBody(body) {
|
|
|
141
166
|
};
|
|
142
167
|
}
|
|
143
168
|
|
|
169
|
+
// src/capture/auth.ts
|
|
170
|
+
var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
|
|
171
|
+
var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
|
|
172
|
+
function isAuthRequest(url, patterns) {
|
|
173
|
+
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
174
|
+
}
|
|
175
|
+
function parseAuthErrorBody(body) {
|
|
176
|
+
if (!body) return null;
|
|
177
|
+
let parsed;
|
|
178
|
+
try {
|
|
179
|
+
parsed = JSON.parse(body);
|
|
180
|
+
} catch {
|
|
181
|
+
return null;
|
|
182
|
+
}
|
|
183
|
+
if (typeof parsed !== "object") return null;
|
|
184
|
+
if (parsed === null) return null;
|
|
185
|
+
const p = parsed;
|
|
186
|
+
if (typeof p["code"] !== "string") return null;
|
|
187
|
+
if (typeof p["message"] !== "string") return null;
|
|
188
|
+
return {
|
|
189
|
+
code: p["code"],
|
|
190
|
+
message: p["message"],
|
|
191
|
+
requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
|
|
144
195
|
// src/capture/network.ts
|
|
145
196
|
var BODY_TRUNCATE_LIMIT = 2e3;
|
|
146
197
|
var DEFAULT_IGNORE_URLS = [
|
|
@@ -241,6 +292,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
241
292
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
242
293
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
243
294
|
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
295
|
+
const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
|
|
296
|
+
const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
|
|
244
297
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
245
298
|
if (statusCode == null) return;
|
|
246
299
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
@@ -261,6 +314,26 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
261
314
|
};
|
|
262
315
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
263
316
|
}
|
|
317
|
+
function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
318
|
+
if (statusCode == null) return;
|
|
319
|
+
if (!isAuthRequest(url, authPatterns)) return;
|
|
320
|
+
const authError = parseAuthErrorBody(responseBody);
|
|
321
|
+
if (!authError) return;
|
|
322
|
+
if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
|
|
323
|
+
const allBreadcrumbs = (0, import_core3.getIsolationScope)().getScopeData().breadcrumbs;
|
|
324
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
325
|
+
const info = {
|
|
326
|
+
type: requestType,
|
|
327
|
+
method,
|
|
328
|
+
url,
|
|
329
|
+
statusCode,
|
|
330
|
+
authError,
|
|
331
|
+
requestBody,
|
|
332
|
+
responseBody,
|
|
333
|
+
precedingBreadcrumbs
|
|
334
|
+
};
|
|
335
|
+
for (const sub of subscribers) sub.onAuthWhitelistError(info);
|
|
336
|
+
}
|
|
264
337
|
function makeHandler(requestType) {
|
|
265
338
|
return (info) => {
|
|
266
339
|
for (const sub of subscribers) void sub.onNetworkRequest(info);
|
|
@@ -272,6 +345,14 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
272
345
|
info.requestBody,
|
|
273
346
|
info.responseBody
|
|
274
347
|
);
|
|
348
|
+
tryDispatchAuthWhitelistError(
|
|
349
|
+
requestType,
|
|
350
|
+
info.method,
|
|
351
|
+
info.url,
|
|
352
|
+
info.statusCode,
|
|
353
|
+
info.requestBody,
|
|
354
|
+
info.responseBody
|
|
355
|
+
);
|
|
275
356
|
};
|
|
276
357
|
}
|
|
277
358
|
return {
|
|
@@ -457,6 +538,33 @@ var PostMessageSubscriber = class {
|
|
|
457
538
|
};
|
|
458
539
|
this.send(msg, "*");
|
|
459
540
|
}
|
|
541
|
+
onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
|
|
542
|
+
const urlPath = extractUrlPath(url);
|
|
543
|
+
const code = authError.code;
|
|
544
|
+
const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
|
|
545
|
+
const msg = {
|
|
546
|
+
type: "GLOBAL_ERROR",
|
|
547
|
+
message: errorMessage,
|
|
548
|
+
error: null,
|
|
549
|
+
errorData: {
|
|
550
|
+
name: "AuthWhitelistError",
|
|
551
|
+
message: authError.message,
|
|
552
|
+
stack: null,
|
|
553
|
+
fileName: url,
|
|
554
|
+
lineNumber: 0,
|
|
555
|
+
columnNumber: 0,
|
|
556
|
+
httpPath: urlPath,
|
|
557
|
+
httpStatus: statusCode,
|
|
558
|
+
httpMethod: method,
|
|
559
|
+
requestBody: requestBody ?? "",
|
|
560
|
+
responseBody,
|
|
561
|
+
authCode: code,
|
|
562
|
+
authRequestId: authError.requestId ?? void 0,
|
|
563
|
+
requestType: type
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
this.send(msg, "*");
|
|
567
|
+
}
|
|
460
568
|
};
|
|
461
569
|
function createPostMessageSubscriber(send) {
|
|
462
570
|
return new PostMessageSubscriber(send);
|
|
@@ -472,11 +580,15 @@ function createNoopSubscriber() {
|
|
|
472
580
|
onNetworkRequest() {
|
|
473
581
|
},
|
|
474
582
|
onPgRestWhitelistError() {
|
|
583
|
+
},
|
|
584
|
+
onAuthWhitelistError() {
|
|
475
585
|
}
|
|
476
586
|
};
|
|
477
587
|
}
|
|
478
588
|
// Annotate the CommonJS export names for ESM import in node:
|
|
479
589
|
0 && (module.exports = {
|
|
590
|
+
DEFAULT_AUTH_URL_PATTERNS,
|
|
591
|
+
DEFAULT_AUTH_WHITELIST_CODES,
|
|
480
592
|
DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
481
593
|
DEFAULT_IGNORE_URLS,
|
|
482
594
|
DEFAULT_PGREST_URL_PATTERNS,
|
package/dist/index.d.cts
CHANGED
|
@@ -24,6 +24,29 @@ interface IPgRestErrorBody {
|
|
|
24
24
|
hint: string | null;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/** Supabase Auth 响应检测与错误体解析。 */
|
|
28
|
+
/**
|
|
29
|
+
* 默认匹配 Supabase Auth 的标准路径前缀。
|
|
30
|
+
* 可通过 ISentryCaptureOptions.authUrlPatterns 覆盖。
|
|
31
|
+
*/
|
|
32
|
+
declare const DEFAULT_AUTH_URL_PATTERNS: Array<string | RegExp>;
|
|
33
|
+
/**
|
|
34
|
+
* 白名单 Auth error code 列表。
|
|
35
|
+
* 命中白名单的响应才触发 onAuthWhitelistError。
|
|
36
|
+
*
|
|
37
|
+
* TODO: 根据业务需要补充具体 code。格式示例:
|
|
38
|
+
* - 'email_not_confirmed'(邮箱未确认)
|
|
39
|
+
* - 'invalid_email'(邮箱格式无效)
|
|
40
|
+
* - 'unexpected_failure'(未知失败)
|
|
41
|
+
*/
|
|
42
|
+
declare const DEFAULT_AUTH_WHITELIST_CODES: string[];
|
|
43
|
+
/** Supabase Auth 标准错误响应体结构(HTTP 4xx/5xx 时返回)。 */
|
|
44
|
+
interface IAuthErrorBody {
|
|
45
|
+
requestId: string | null;
|
|
46
|
+
code: string;
|
|
47
|
+
message: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
27
50
|
/** Sentry 捕获模块共享类型定义。 */
|
|
28
51
|
|
|
29
52
|
interface IErrorCaptureInfo {
|
|
@@ -92,6 +115,25 @@ interface IPgRestWhitelistErrorInfo {
|
|
|
92
115
|
*/
|
|
93
116
|
precedingBreadcrumbs: Breadcrumb[];
|
|
94
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* 捕获到 Auth 白名单错误时的事件数据。
|
|
120
|
+
* 由 network observer 在响应体符合 Auth 结构且 code 命中白名单时构造并派发。
|
|
121
|
+
*/
|
|
122
|
+
interface IAuthWhitelistErrorInfo {
|
|
123
|
+
/** 请求类型 */
|
|
124
|
+
type: 'fetch' | 'xhr';
|
|
125
|
+
method: string;
|
|
126
|
+
url: string;
|
|
127
|
+
statusCode: number;
|
|
128
|
+
/** 解析后的 Auth 标准错误体 */
|
|
129
|
+
authError: IAuthErrorBody;
|
|
130
|
+
/** 原始请求体文本 */
|
|
131
|
+
requestBody: string | undefined;
|
|
132
|
+
/** 原始响应体文本 */
|
|
133
|
+
responseBody: string;
|
|
134
|
+
/** 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。 */
|
|
135
|
+
precedingBreadcrumbs: Breadcrumb[];
|
|
136
|
+
}
|
|
95
137
|
/** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
|
|
96
138
|
interface ISentryCaptureOptions {
|
|
97
139
|
/**
|
|
@@ -126,6 +168,15 @@ interface ISentryCaptureOptions {
|
|
|
126
168
|
* 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
|
|
127
169
|
*/
|
|
128
170
|
precedingBreadcrumbsLimit?: number;
|
|
171
|
+
/**
|
|
172
|
+
* 匹配 Auth 服务端点的 URL 模式(可选)。
|
|
173
|
+
* 命中模式的请求会额外做 Auth 响应体解析,检查是否触发白名单错误事件。
|
|
174
|
+
*/
|
|
175
|
+
authUrlPatterns?: Array<string | RegExp>;
|
|
176
|
+
/**
|
|
177
|
+
* Auth error 白名单 error code 列表(可选,默认为 `DEFAULT_AUTH_WHITELIST_CODES`)。
|
|
178
|
+
*/
|
|
179
|
+
authWhitelistCodes?: string[];
|
|
129
180
|
}
|
|
130
181
|
|
|
131
182
|
/** 捕获事件订阅者接口与消息类型定义。 */
|
|
@@ -151,6 +202,11 @@ interface ICaptureSubscriber {
|
|
|
151
202
|
* 适用于需要对特定数据库业务错误(如唯一键冲突、JWT 过期)做专项处理的场景。
|
|
152
203
|
*/
|
|
153
204
|
onPgRestWhitelistError(info: IPgRestWhitelistErrorInfo): void;
|
|
205
|
+
/**
|
|
206
|
+
* 请求命中 Auth 端点,且响应体 code 字段在白名单中时触发。
|
|
207
|
+
* 适用于需要对特定认证错误(如邮箱未确认、格式无效)做专项处理的场景。
|
|
208
|
+
*/
|
|
209
|
+
onAuthWhitelistError(info: IAuthWhitelistErrorInfo): void;
|
|
154
210
|
}
|
|
155
211
|
/** postMessage 发送函数,可注入替换(便于测试)。 */
|
|
156
212
|
interface GlobalErrorMessage {
|
|
@@ -178,6 +234,9 @@ interface GlobalErrorMessage {
|
|
|
178
234
|
pgRestDetails?: string;
|
|
179
235
|
pgRestHint?: string;
|
|
180
236
|
requestType?: 'fetch' | 'xhr';
|
|
237
|
+
/** AuthWhitelistError 专属字段 */
|
|
238
|
+
authCode?: string;
|
|
239
|
+
authRequestId?: string;
|
|
181
240
|
};
|
|
182
241
|
}
|
|
183
242
|
type PostMessageFn = (message: GlobalErrorMessage, targetOrigin: string) => void;
|
|
@@ -235,4 +294,4 @@ declare function createPostMessageSubscriber(send?: PostMessageFn): ICaptureSubs
|
|
|
235
294
|
/** 创建一个什么都不做的订阅者,方便测试时按需 spy 或替换单个方法。 */
|
|
236
295
|
declare function createNoopSubscriber(): ICaptureSubscriber;
|
|
237
296
|
|
|
238
|
-
export { DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
|
|
297
|
+
export { DEFAULT_AUTH_URL_PATTERNS, DEFAULT_AUTH_WHITELIST_CODES, DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type IAuthErrorBody, type IAuthWhitelistErrorInfo, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
|
package/dist/index.d.ts
CHANGED
|
@@ -24,6 +24,29 @@ interface IPgRestErrorBody {
|
|
|
24
24
|
hint: string | null;
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
+
/** Supabase Auth 响应检测与错误体解析。 */
|
|
28
|
+
/**
|
|
29
|
+
* 默认匹配 Supabase Auth 的标准路径前缀。
|
|
30
|
+
* 可通过 ISentryCaptureOptions.authUrlPatterns 覆盖。
|
|
31
|
+
*/
|
|
32
|
+
declare const DEFAULT_AUTH_URL_PATTERNS: Array<string | RegExp>;
|
|
33
|
+
/**
|
|
34
|
+
* 白名单 Auth error code 列表。
|
|
35
|
+
* 命中白名单的响应才触发 onAuthWhitelistError。
|
|
36
|
+
*
|
|
37
|
+
* TODO: 根据业务需要补充具体 code。格式示例:
|
|
38
|
+
* - 'email_not_confirmed'(邮箱未确认)
|
|
39
|
+
* - 'invalid_email'(邮箱格式无效)
|
|
40
|
+
* - 'unexpected_failure'(未知失败)
|
|
41
|
+
*/
|
|
42
|
+
declare const DEFAULT_AUTH_WHITELIST_CODES: string[];
|
|
43
|
+
/** Supabase Auth 标准错误响应体结构(HTTP 4xx/5xx 时返回)。 */
|
|
44
|
+
interface IAuthErrorBody {
|
|
45
|
+
requestId: string | null;
|
|
46
|
+
code: string;
|
|
47
|
+
message: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
27
50
|
/** Sentry 捕获模块共享类型定义。 */
|
|
28
51
|
|
|
29
52
|
interface IErrorCaptureInfo {
|
|
@@ -92,6 +115,25 @@ interface IPgRestWhitelistErrorInfo {
|
|
|
92
115
|
*/
|
|
93
116
|
precedingBreadcrumbs: Breadcrumb[];
|
|
94
117
|
}
|
|
118
|
+
/**
|
|
119
|
+
* 捕获到 Auth 白名单错误时的事件数据。
|
|
120
|
+
* 由 network observer 在响应体符合 Auth 结构且 code 命中白名单时构造并派发。
|
|
121
|
+
*/
|
|
122
|
+
interface IAuthWhitelistErrorInfo {
|
|
123
|
+
/** 请求类型 */
|
|
124
|
+
type: 'fetch' | 'xhr';
|
|
125
|
+
method: string;
|
|
126
|
+
url: string;
|
|
127
|
+
statusCode: number;
|
|
128
|
+
/** 解析后的 Auth 标准错误体 */
|
|
129
|
+
authError: IAuthErrorBody;
|
|
130
|
+
/** 原始请求体文本 */
|
|
131
|
+
requestBody: string | undefined;
|
|
132
|
+
/** 原始响应体文本 */
|
|
133
|
+
responseBody: string;
|
|
134
|
+
/** 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。 */
|
|
135
|
+
precedingBreadcrumbs: Breadcrumb[];
|
|
136
|
+
}
|
|
95
137
|
/** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
|
|
96
138
|
interface ISentryCaptureOptions {
|
|
97
139
|
/**
|
|
@@ -126,6 +168,15 @@ interface ISentryCaptureOptions {
|
|
|
126
168
|
* 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
|
|
127
169
|
*/
|
|
128
170
|
precedingBreadcrumbsLimit?: number;
|
|
171
|
+
/**
|
|
172
|
+
* 匹配 Auth 服务端点的 URL 模式(可选)。
|
|
173
|
+
* 命中模式的请求会额外做 Auth 响应体解析,检查是否触发白名单错误事件。
|
|
174
|
+
*/
|
|
175
|
+
authUrlPatterns?: Array<string | RegExp>;
|
|
176
|
+
/**
|
|
177
|
+
* Auth error 白名单 error code 列表(可选,默认为 `DEFAULT_AUTH_WHITELIST_CODES`)。
|
|
178
|
+
*/
|
|
179
|
+
authWhitelistCodes?: string[];
|
|
129
180
|
}
|
|
130
181
|
|
|
131
182
|
/** 捕获事件订阅者接口与消息类型定义。 */
|
|
@@ -151,6 +202,11 @@ interface ICaptureSubscriber {
|
|
|
151
202
|
* 适用于需要对特定数据库业务错误(如唯一键冲突、JWT 过期)做专项处理的场景。
|
|
152
203
|
*/
|
|
153
204
|
onPgRestWhitelistError(info: IPgRestWhitelistErrorInfo): void;
|
|
205
|
+
/**
|
|
206
|
+
* 请求命中 Auth 端点,且响应体 code 字段在白名单中时触发。
|
|
207
|
+
* 适用于需要对特定认证错误(如邮箱未确认、格式无效)做专项处理的场景。
|
|
208
|
+
*/
|
|
209
|
+
onAuthWhitelistError(info: IAuthWhitelistErrorInfo): void;
|
|
154
210
|
}
|
|
155
211
|
/** postMessage 发送函数,可注入替换(便于测试)。 */
|
|
156
212
|
interface GlobalErrorMessage {
|
|
@@ -178,6 +234,9 @@ interface GlobalErrorMessage {
|
|
|
178
234
|
pgRestDetails?: string;
|
|
179
235
|
pgRestHint?: string;
|
|
180
236
|
requestType?: 'fetch' | 'xhr';
|
|
237
|
+
/** AuthWhitelistError 专属字段 */
|
|
238
|
+
authCode?: string;
|
|
239
|
+
authRequestId?: string;
|
|
181
240
|
};
|
|
182
241
|
}
|
|
183
242
|
type PostMessageFn = (message: GlobalErrorMessage, targetOrigin: string) => void;
|
|
@@ -235,4 +294,4 @@ declare function createPostMessageSubscriber(send?: PostMessageFn): ICaptureSubs
|
|
|
235
294
|
/** 创建一个什么都不做的订阅者,方便测试时按需 spy 或替换单个方法。 */
|
|
236
295
|
declare function createNoopSubscriber(): ICaptureSubscriber;
|
|
237
296
|
|
|
238
|
-
export { DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
|
|
297
|
+
export { DEFAULT_AUTH_URL_PATTERNS, DEFAULT_AUTH_WHITELIST_CODES, DEFAULT_CONSOLE_FILTER_KEYWORDS, DEFAULT_IGNORE_URLS, DEFAULT_PGREST_URL_PATTERNS, DEFAULT_PGREST_WHITELIST_CODES, type GlobalErrorMessage, type IAuthErrorBody, type IAuthWhitelistErrorInfo, type ICaptureSubscriber, type IConsoleCaptureInfo, type IErrorCaptureInfo, type INetworkRequestInfo, type IPgRestErrorBody, type IPgRestWhitelistErrorInfo, type ISentryCaptureOptions, type PostMessageFn, SentryCapture, createNoopSubscriber, createPostMessageSubscriber };
|
package/dist/index.js
CHANGED
|
@@ -86,7 +86,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
|
|
|
86
86
|
|
|
87
87
|
// src/capture/pgrest.ts
|
|
88
88
|
var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
|
|
89
|
-
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
89
|
+
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
90
|
+
"22P02",
|
|
91
|
+
// 无效的文本表示(如类型转换失败)
|
|
92
|
+
"42703",
|
|
93
|
+
// 未定义的列
|
|
94
|
+
"42P01",
|
|
95
|
+
// 未定义的表
|
|
96
|
+
"42501",
|
|
97
|
+
// 权限不足
|
|
98
|
+
"23503",
|
|
99
|
+
// 外键约束失败
|
|
100
|
+
"42P17",
|
|
101
|
+
// 对象已存在
|
|
102
|
+
"22008",
|
|
103
|
+
// 日期时间字段值超出范围
|
|
104
|
+
"23505",
|
|
105
|
+
// 唯一键冲突
|
|
106
|
+
"PGRST116",
|
|
107
|
+
// 期望单行但返回多行
|
|
108
|
+
"PGRST200",
|
|
109
|
+
// 外键关系查找失败
|
|
110
|
+
"PGRST204"
|
|
111
|
+
// 响应中找不到列
|
|
112
|
+
];
|
|
90
113
|
function isPgRestRequest(url, patterns) {
|
|
91
114
|
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
92
115
|
}
|
|
@@ -111,6 +134,32 @@ function parsePgRestErrorBody(body) {
|
|
|
111
134
|
};
|
|
112
135
|
}
|
|
113
136
|
|
|
137
|
+
// src/capture/auth.ts
|
|
138
|
+
var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
|
|
139
|
+
var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
|
|
140
|
+
function isAuthRequest(url, patterns) {
|
|
141
|
+
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
142
|
+
}
|
|
143
|
+
function parseAuthErrorBody(body) {
|
|
144
|
+
if (!body) return null;
|
|
145
|
+
let parsed;
|
|
146
|
+
try {
|
|
147
|
+
parsed = JSON.parse(body);
|
|
148
|
+
} catch {
|
|
149
|
+
return null;
|
|
150
|
+
}
|
|
151
|
+
if (typeof parsed !== "object") return null;
|
|
152
|
+
if (parsed === null) return null;
|
|
153
|
+
const p = parsed;
|
|
154
|
+
if (typeof p["code"] !== "string") return null;
|
|
155
|
+
if (typeof p["message"] !== "string") return null;
|
|
156
|
+
return {
|
|
157
|
+
code: p["code"],
|
|
158
|
+
message: p["message"],
|
|
159
|
+
requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
|
|
114
163
|
// src/capture/network.ts
|
|
115
164
|
var BODY_TRUNCATE_LIMIT = 2e3;
|
|
116
165
|
var DEFAULT_IGNORE_URLS = [
|
|
@@ -211,6 +260,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
211
260
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
212
261
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
213
262
|
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
263
|
+
const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
|
|
264
|
+
const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
|
|
214
265
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
215
266
|
if (statusCode == null) return;
|
|
216
267
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
@@ -231,6 +282,26 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
231
282
|
};
|
|
232
283
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
233
284
|
}
|
|
285
|
+
function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
286
|
+
if (statusCode == null) return;
|
|
287
|
+
if (!isAuthRequest(url, authPatterns)) return;
|
|
288
|
+
const authError = parseAuthErrorBody(responseBody);
|
|
289
|
+
if (!authError) return;
|
|
290
|
+
if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
|
|
291
|
+
const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
|
|
292
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
293
|
+
const info = {
|
|
294
|
+
type: requestType,
|
|
295
|
+
method,
|
|
296
|
+
url,
|
|
297
|
+
statusCode,
|
|
298
|
+
authError,
|
|
299
|
+
requestBody,
|
|
300
|
+
responseBody,
|
|
301
|
+
precedingBreadcrumbs
|
|
302
|
+
};
|
|
303
|
+
for (const sub of subscribers) sub.onAuthWhitelistError(info);
|
|
304
|
+
}
|
|
234
305
|
function makeHandler(requestType) {
|
|
235
306
|
return (info) => {
|
|
236
307
|
for (const sub of subscribers) void sub.onNetworkRequest(info);
|
|
@@ -242,6 +313,14 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
242
313
|
info.requestBody,
|
|
243
314
|
info.responseBody
|
|
244
315
|
);
|
|
316
|
+
tryDispatchAuthWhitelistError(
|
|
317
|
+
requestType,
|
|
318
|
+
info.method,
|
|
319
|
+
info.url,
|
|
320
|
+
info.statusCode,
|
|
321
|
+
info.requestBody,
|
|
322
|
+
info.responseBody
|
|
323
|
+
);
|
|
245
324
|
};
|
|
246
325
|
}
|
|
247
326
|
return {
|
|
@@ -427,6 +506,33 @@ var PostMessageSubscriber = class {
|
|
|
427
506
|
};
|
|
428
507
|
this.send(msg, "*");
|
|
429
508
|
}
|
|
509
|
+
onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
|
|
510
|
+
const urlPath = extractUrlPath(url);
|
|
511
|
+
const code = authError.code;
|
|
512
|
+
const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
|
|
513
|
+
const msg = {
|
|
514
|
+
type: "GLOBAL_ERROR",
|
|
515
|
+
message: errorMessage,
|
|
516
|
+
error: null,
|
|
517
|
+
errorData: {
|
|
518
|
+
name: "AuthWhitelistError",
|
|
519
|
+
message: authError.message,
|
|
520
|
+
stack: null,
|
|
521
|
+
fileName: url,
|
|
522
|
+
lineNumber: 0,
|
|
523
|
+
columnNumber: 0,
|
|
524
|
+
httpPath: urlPath,
|
|
525
|
+
httpStatus: statusCode,
|
|
526
|
+
httpMethod: method,
|
|
527
|
+
requestBody: requestBody ?? "",
|
|
528
|
+
responseBody,
|
|
529
|
+
authCode: code,
|
|
530
|
+
authRequestId: authError.requestId ?? void 0,
|
|
531
|
+
requestType: type
|
|
532
|
+
}
|
|
533
|
+
};
|
|
534
|
+
this.send(msg, "*");
|
|
535
|
+
}
|
|
430
536
|
};
|
|
431
537
|
function createPostMessageSubscriber(send) {
|
|
432
538
|
return new PostMessageSubscriber(send);
|
|
@@ -442,10 +548,14 @@ function createNoopSubscriber() {
|
|
|
442
548
|
onNetworkRequest() {
|
|
443
549
|
},
|
|
444
550
|
onPgRestWhitelistError() {
|
|
551
|
+
},
|
|
552
|
+
onAuthWhitelistError() {
|
|
445
553
|
}
|
|
446
554
|
};
|
|
447
555
|
}
|
|
448
556
|
export {
|
|
557
|
+
DEFAULT_AUTH_URL_PATTERNS,
|
|
558
|
+
DEFAULT_AUTH_WHITELIST_CODES,
|
|
449
559
|
DEFAULT_CONSOLE_FILTER_KEYWORDS,
|
|
450
560
|
DEFAULT_IGNORE_URLS,
|
|
451
561
|
DEFAULT_PGREST_URL_PATTERNS,
|
|
@@ -89,7 +89,30 @@ function getFetchRequestArgBody(fetchArgs = []) {
|
|
|
89
89
|
|
|
90
90
|
// src/capture/pgrest.ts
|
|
91
91
|
var DEFAULT_PGREST_URL_PATTERNS = [/\/rest\/v1\//];
|
|
92
|
-
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
92
|
+
var DEFAULT_PGREST_WHITELIST_CODES = [
|
|
93
|
+
"22P02",
|
|
94
|
+
// 无效的文本表示(如类型转换失败)
|
|
95
|
+
"42703",
|
|
96
|
+
// 未定义的列
|
|
97
|
+
"42P01",
|
|
98
|
+
// 未定义的表
|
|
99
|
+
"42501",
|
|
100
|
+
// 权限不足
|
|
101
|
+
"23503",
|
|
102
|
+
// 外键约束失败
|
|
103
|
+
"42P17",
|
|
104
|
+
// 对象已存在
|
|
105
|
+
"22008",
|
|
106
|
+
// 日期时间字段值超出范围
|
|
107
|
+
"23505",
|
|
108
|
+
// 唯一键冲突
|
|
109
|
+
"PGRST116",
|
|
110
|
+
// 期望单行但返回多行
|
|
111
|
+
"PGRST200",
|
|
112
|
+
// 外键关系查找失败
|
|
113
|
+
"PGRST204"
|
|
114
|
+
// 响应中找不到列
|
|
115
|
+
];
|
|
93
116
|
function isPgRestRequest(url, patterns) {
|
|
94
117
|
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
95
118
|
}
|
|
@@ -114,6 +137,32 @@ function parsePgRestErrorBody(body) {
|
|
|
114
137
|
};
|
|
115
138
|
}
|
|
116
139
|
|
|
140
|
+
// src/capture/auth.ts
|
|
141
|
+
var DEFAULT_AUTH_URL_PATTERNS = [/\/auth\/v1\//];
|
|
142
|
+
var DEFAULT_AUTH_WHITELIST_CODES = ["unexpected_failure"];
|
|
143
|
+
function isAuthRequest(url, patterns) {
|
|
144
|
+
return patterns.some((p) => (typeof p === "string" ? new RegExp(p) : p).test(url));
|
|
145
|
+
}
|
|
146
|
+
function parseAuthErrorBody(body) {
|
|
147
|
+
if (!body) return null;
|
|
148
|
+
let parsed;
|
|
149
|
+
try {
|
|
150
|
+
parsed = JSON.parse(body);
|
|
151
|
+
} catch {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
if (typeof parsed !== "object") return null;
|
|
155
|
+
if (parsed === null) return null;
|
|
156
|
+
const p = parsed;
|
|
157
|
+
if (typeof p["code"] !== "string") return null;
|
|
158
|
+
if (typeof p["message"] !== "string") return null;
|
|
159
|
+
return {
|
|
160
|
+
code: p["code"],
|
|
161
|
+
message: p["message"],
|
|
162
|
+
requestId: typeof p["requestId"] === "string" ? p["requestId"] : null
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
117
166
|
// src/capture/network.ts
|
|
118
167
|
var BODY_TRUNCATE_LIMIT = 2e3;
|
|
119
168
|
var DEFAULT_IGNORE_URLS = [
|
|
@@ -214,6 +263,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
214
263
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
215
264
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
216
265
|
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
266
|
+
const authPatterns = opts.authUrlPatterns ?? DEFAULT_AUTH_URL_PATTERNS;
|
|
267
|
+
const authWhitelistCodes = opts.authWhitelistCodes ?? DEFAULT_AUTH_WHITELIST_CODES;
|
|
217
268
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
218
269
|
if (statusCode == null) return;
|
|
219
270
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
@@ -234,6 +285,26 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
234
285
|
};
|
|
235
286
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
236
287
|
}
|
|
288
|
+
function tryDispatchAuthWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
289
|
+
if (statusCode == null) return;
|
|
290
|
+
if (!isAuthRequest(url, authPatterns)) return;
|
|
291
|
+
const authError = parseAuthErrorBody(responseBody);
|
|
292
|
+
if (!authError) return;
|
|
293
|
+
if (authWhitelistCodes.length > 0 && !authWhitelistCodes.includes(authError.code)) return;
|
|
294
|
+
const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
|
|
295
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
296
|
+
const info = {
|
|
297
|
+
type: requestType,
|
|
298
|
+
method,
|
|
299
|
+
url,
|
|
300
|
+
statusCode,
|
|
301
|
+
authError,
|
|
302
|
+
requestBody,
|
|
303
|
+
responseBody,
|
|
304
|
+
precedingBreadcrumbs
|
|
305
|
+
};
|
|
306
|
+
for (const sub of subscribers) sub.onAuthWhitelistError(info);
|
|
307
|
+
}
|
|
237
308
|
function makeHandler(requestType) {
|
|
238
309
|
return (info) => {
|
|
239
310
|
for (const sub of subscribers) void sub.onNetworkRequest(info);
|
|
@@ -245,6 +316,14 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
245
316
|
info.requestBody,
|
|
246
317
|
info.responseBody
|
|
247
318
|
);
|
|
319
|
+
tryDispatchAuthWhitelistError(
|
|
320
|
+
requestType,
|
|
321
|
+
info.method,
|
|
322
|
+
info.url,
|
|
323
|
+
info.statusCode,
|
|
324
|
+
info.requestBody,
|
|
325
|
+
info.responseBody
|
|
326
|
+
);
|
|
248
327
|
};
|
|
249
328
|
}
|
|
250
329
|
return {
|
|
@@ -430,6 +509,33 @@ var PostMessageSubscriber = class {
|
|
|
430
509
|
};
|
|
431
510
|
this.send(msg, "*");
|
|
432
511
|
}
|
|
512
|
+
onAuthWhitelistError({ type, method, url, statusCode, authError, requestBody, responseBody }) {
|
|
513
|
+
const urlPath = extractUrlPath(url);
|
|
514
|
+
const code = authError.code;
|
|
515
|
+
const errorMessage = `${method} ${urlPath} -> ${statusCode} [Auth:${code}]`;
|
|
516
|
+
const msg = {
|
|
517
|
+
type: "GLOBAL_ERROR",
|
|
518
|
+
message: errorMessage,
|
|
519
|
+
error: null,
|
|
520
|
+
errorData: {
|
|
521
|
+
name: "AuthWhitelistError",
|
|
522
|
+
message: authError.message,
|
|
523
|
+
stack: null,
|
|
524
|
+
fileName: url,
|
|
525
|
+
lineNumber: 0,
|
|
526
|
+
columnNumber: 0,
|
|
527
|
+
httpPath: urlPath,
|
|
528
|
+
httpStatus: statusCode,
|
|
529
|
+
httpMethod: method,
|
|
530
|
+
requestBody: requestBody ?? "",
|
|
531
|
+
responseBody,
|
|
532
|
+
authCode: code,
|
|
533
|
+
authRequestId: authError.requestId ?? void 0,
|
|
534
|
+
requestType: type
|
|
535
|
+
}
|
|
536
|
+
};
|
|
537
|
+
this.send(msg, "*");
|
|
538
|
+
}
|
|
433
539
|
};
|
|
434
540
|
function createPostMessageSubscriber(send) {
|
|
435
541
|
return new PostMessageSubscriber(send);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "miaoda-react-devkit",
|
|
3
|
-
"version": "0.1.1-beta.
|
|
3
|
+
"version": "0.1.1-beta.6",
|
|
4
4
|
"description": "React admin 应用开发工具集",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -24,6 +24,7 @@
|
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
26
|
"cloud",
|
|
27
|
+
"!cloud/**/*.ts",
|
|
27
28
|
"tsconfig-base.json",
|
|
28
29
|
"!dist/**/*.map"
|
|
29
30
|
],
|
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);
|