miaoda-react-devkit 0.1.1-beta.3 → 0.1.1-beta.5
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 +106 -0
- package/dist/index.cjs +5 -1
- package/dist/index.d.cts +13 -1
- package/dist/index.d.ts +13 -1
- package/dist/index.js +6 -2
- package/dist/stubs/sentry-react-stub.js +6 -2
- package/package.json +3 -2
- package/cloud/vite.config.ts +0 -156
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// cloud/vite.config.ts
|
|
2
|
+
import * as vite from "vite";
|
|
3
|
+
import { loadConfigFromFile, mergeConfig } from "vite";
|
|
4
|
+
import { withSentryStub, hmrTogglePlugin } from "../dist/vite/index.js";
|
|
5
|
+
import {
|
|
6
|
+
makeTagger,
|
|
7
|
+
injectedGuiListenerPlugin,
|
|
8
|
+
injectOnErrorPlugin,
|
|
9
|
+
monitorPlugin,
|
|
10
|
+
fontsCodePlugin,
|
|
11
|
+
mdClickPlugin,
|
|
12
|
+
extractRouterPlugin,
|
|
13
|
+
patchSupabasePlugins
|
|
14
|
+
} from "miaoda-sc-plugin";
|
|
15
|
+
import path from "path";
|
|
16
|
+
import fs from "fs";
|
|
17
|
+
var projectRoot = process.cwd();
|
|
18
|
+
var appType = process.env.DEVKIT_APP_TYPE ?? "freestyle";
|
|
19
|
+
var cdnHost = process.env.DEVKIT_CDN_HOST ?? "resource-static.cdn.bcebos.com";
|
|
20
|
+
var env = process.env.DEVKIT_ENV ?? "";
|
|
21
|
+
var sentryDsn = process.env.DEVKIT_SENTRY_DSN ?? "";
|
|
22
|
+
var miaodaEnv = process.env.DEVKIT_MIAODA_ENV ?? "";
|
|
23
|
+
var appId = process.env.DEVKIT_APP_ID ?? "";
|
|
24
|
+
var isOnline = env === "online";
|
|
25
|
+
var loaded = await loadConfigFromFile(
|
|
26
|
+
{ command: "serve", mode: "development" },
|
|
27
|
+
path.join(projectRoot, "vite.config.ts"),
|
|
28
|
+
projectRoot
|
|
29
|
+
);
|
|
30
|
+
var projectConfig = loaded?.config ?? {};
|
|
31
|
+
var configPath = path.join(projectRoot, "vite.config.ts");
|
|
32
|
+
if (!loaded) {
|
|
33
|
+
throw new Error(
|
|
34
|
+
`[miaoda-devkit] \u627E\u4E0D\u5230\u9879\u76EE vite \u914D\u7F6E\uFF1A${configPath}
|
|
35
|
+
\u8BF7\u786E\u8BA4\u9879\u76EE\u6839\u76EE\u5F55\u4E0B\u5B58\u5728 vite.config.ts`
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
var plugins = (projectConfig.plugins ?? []).flat(Infinity).filter((p) => !!p);
|
|
39
|
+
var hasReact = plugins.some((p) => p.name === "vite:react-babel" || p.name === "vite:react-swc");
|
|
40
|
+
if (!hasReact) {
|
|
41
|
+
throw new Error(
|
|
42
|
+
`[miaoda-devkit] \u9879\u76EE vite \u914D\u7F6E\u7F3A\u5C11 React \u63D2\u4EF6\u3002
|
|
43
|
+
\u8BF7\u5728 ${configPath} \u7684 plugins \u4E2D\u6DFB\u52A0 react()\uFF08@vitejs/plugin-react \u6216 @vitejs/plugin-react-swc\uFF09`
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
var possibleEntries = ["./src/main.tsx", "./src/main.ts", "./src/index.tsx", "./src/index.ts"];
|
|
47
|
+
var warmupFiles = possibleEntries.filter((e) => fs.existsSync(path.join(projectRoot, e)));
|
|
48
|
+
var SENTRY_IMPORT_RE = /from\s+['"]@sentry\/react['"]/;
|
|
49
|
+
function entryHasSentryImport(root, entries) {
|
|
50
|
+
for (const entry of entries) {
|
|
51
|
+
const fullPath = path.join(root, entry);
|
|
52
|
+
if (!fs.existsSync(fullPath)) {
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
if (SENTRY_IMPORT_RE.test(fs.readFileSync(fullPath, "utf-8"))) {
|
|
57
|
+
return true;
|
|
58
|
+
}
|
|
59
|
+
} catch {
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
var hasSentryReact = entryHasSentryImport(projectRoot, possibleEntries);
|
|
65
|
+
var viteVersionInfo = {
|
|
66
|
+
version: vite.version,
|
|
67
|
+
rollupVersion: vite.rollupVersion ?? null,
|
|
68
|
+
rolldownVersion: vite.rolldownVersion ?? null,
|
|
69
|
+
isRolldownVite: "rolldownVersion" in vite
|
|
70
|
+
};
|
|
71
|
+
var injectedGuiUrl = `https://${cdnHost}/common/v2/${isOnline ? "" : `${env}/`}injected.js`;
|
|
72
|
+
var cloudPlugins = [
|
|
73
|
+
...hasSentryReact ? [withSentryStub()] : [monitorPlugin({
|
|
74
|
+
scriptSrc: `https://${cdnHost}/sentry/browser.sentry.min.js`,
|
|
75
|
+
sentryDsn,
|
|
76
|
+
environment: miaodaEnv,
|
|
77
|
+
appId
|
|
78
|
+
})],
|
|
79
|
+
// web 类型需要 DOM 节点打标(GUI使用)
|
|
80
|
+
...appType === "web" ? [
|
|
81
|
+
makeTagger(),
|
|
82
|
+
mdClickPlugin({ appType }),
|
|
83
|
+
fontsCodePlugin(),
|
|
84
|
+
injectedGuiListenerPlugin({ path: injectedGuiUrl })
|
|
85
|
+
] : [],
|
|
86
|
+
injectedGuiListenerPlugin({ path: injectedGuiUrl }),
|
|
87
|
+
injectOnErrorPlugin(),
|
|
88
|
+
hmrTogglePlugin(),
|
|
89
|
+
extractRouterPlugin(),
|
|
90
|
+
patchSupabasePlugins()
|
|
91
|
+
];
|
|
92
|
+
var cloudConfig = {
|
|
93
|
+
define: {
|
|
94
|
+
__VITE_INFO__: JSON.stringify(viteVersionInfo)
|
|
95
|
+
},
|
|
96
|
+
// 将缓存目录固定到项目本地,避免在 /workspace/node_modules/ 下创建
|
|
97
|
+
cacheDir: path.resolve(projectRoot, "node_modules/.vite"),
|
|
98
|
+
server: {
|
|
99
|
+
warmup: warmupFiles.length > 0 ? { clientFiles: warmupFiles } : {}
|
|
100
|
+
},
|
|
101
|
+
plugins: cloudPlugins
|
|
102
|
+
};
|
|
103
|
+
var vite_config_default = mergeConfig(projectConfig, cloudConfig);
|
|
104
|
+
export {
|
|
105
|
+
vite_config_default as default
|
|
106
|
+
};
|
package/dist/index.cjs
CHANGED
|
@@ -240,12 +240,15 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
240
240
|
const shouldIgnore = createUrlMatcher(opts.ignoreUrls);
|
|
241
241
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
242
242
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
243
|
+
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
243
244
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
244
245
|
if (statusCode == null) return;
|
|
245
246
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
246
247
|
const pgRestError = parsePgRestErrorBody(responseBody);
|
|
247
248
|
if (!pgRestError) return;
|
|
248
249
|
if (pgRestWhitelistCodes.length > 0 && !pgRestWhitelistCodes.includes(pgRestError.code)) return;
|
|
250
|
+
const allBreadcrumbs = (0, import_core3.getIsolationScope)().getScopeData().breadcrumbs;
|
|
251
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
249
252
|
const info = {
|
|
250
253
|
type: requestType,
|
|
251
254
|
method,
|
|
@@ -253,7 +256,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
253
256
|
statusCode,
|
|
254
257
|
pgRestError,
|
|
255
258
|
requestBody,
|
|
256
|
-
responseBody
|
|
259
|
+
responseBody,
|
|
260
|
+
precedingBreadcrumbs
|
|
257
261
|
};
|
|
258
262
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
259
263
|
}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorEvent, EventHint,
|
|
1
|
+
import { Breadcrumb, ErrorEvent, EventHint, BreadcrumbHint } from '@sentry/react';
|
|
2
2
|
|
|
3
3
|
/** PostgREST 响应检测与错误体解析。 */
|
|
4
4
|
/**
|
|
@@ -85,6 +85,12 @@ interface IPgRestWhitelistErrorInfo {
|
|
|
85
85
|
requestBody: string | undefined;
|
|
86
86
|
/** 原始响应体文本 */
|
|
87
87
|
responseBody: string;
|
|
88
|
+
/**
|
|
89
|
+
* 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。
|
|
90
|
+
* 包含导航(navigation)、点击(ui.click)、历史网络请求(fetch/xhr)、控制台日志等。
|
|
91
|
+
* 取自 Sentry isolation scope,时序上早于当前这条 pgrest 请求。
|
|
92
|
+
*/
|
|
93
|
+
precedingBreadcrumbs: Breadcrumb[];
|
|
88
94
|
}
|
|
89
95
|
/** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
|
|
90
96
|
interface ISentryCaptureOptions {
|
|
@@ -114,6 +120,12 @@ interface ISentryCaptureOptions {
|
|
|
114
120
|
* '23505'(唯一键冲突)、'23503'(外键约束失败)
|
|
115
121
|
*/
|
|
116
122
|
pgRestWhitelistCodes?: string[];
|
|
123
|
+
/**
|
|
124
|
+
* `onPgRestWhitelistError` 回调中 `precedingBreadcrumbs` 的最大条数(可选)。
|
|
125
|
+
* 取 scope 中最新的 N 条,超出部分(更早的操作)被丢弃。
|
|
126
|
+
* 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
|
|
127
|
+
*/
|
|
128
|
+
precedingBreadcrumbsLimit?: number;
|
|
117
129
|
}
|
|
118
130
|
|
|
119
131
|
/** 捕获事件订阅者接口与消息类型定义。 */
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorEvent, EventHint,
|
|
1
|
+
import { Breadcrumb, ErrorEvent, EventHint, BreadcrumbHint } from '@sentry/react';
|
|
2
2
|
|
|
3
3
|
/** PostgREST 响应检测与错误体解析。 */
|
|
4
4
|
/**
|
|
@@ -85,6 +85,12 @@ interface IPgRestWhitelistErrorInfo {
|
|
|
85
85
|
requestBody: string | undefined;
|
|
86
86
|
/** 原始响应体文本 */
|
|
87
87
|
responseBody: string;
|
|
88
|
+
/**
|
|
89
|
+
* 本次请求触发前 scope 中已积累的 breadcrumb 链路,用于还原用户操作路径。
|
|
90
|
+
* 包含导航(navigation)、点击(ui.click)、历史网络请求(fetch/xhr)、控制台日志等。
|
|
91
|
+
* 取自 Sentry isolation scope,时序上早于当前这条 pgrest 请求。
|
|
92
|
+
*/
|
|
93
|
+
precedingBreadcrumbs: Breadcrumb[];
|
|
88
94
|
}
|
|
89
95
|
/** SentryCapture 的初始化选项,仅包含过滤与端点配置;事件处理通过 subscribers 注入。 */
|
|
90
96
|
interface ISentryCaptureOptions {
|
|
@@ -114,6 +120,12 @@ interface ISentryCaptureOptions {
|
|
|
114
120
|
* '23505'(唯一键冲突)、'23503'(外键约束失败)
|
|
115
121
|
*/
|
|
116
122
|
pgRestWhitelistCodes?: string[];
|
|
123
|
+
/**
|
|
124
|
+
* `onPgRestWhitelistError` 回调中 `precedingBreadcrumbs` 的最大条数(可选)。
|
|
125
|
+
* 取 scope 中最新的 N 条,超出部分(更早的操作)被丢弃。
|
|
126
|
+
* 未配置时取全部(受 Sentry `maxBreadcrumbs` 上限约束,默认最多 100 条)。
|
|
127
|
+
*/
|
|
128
|
+
precedingBreadcrumbsLimit?: number;
|
|
117
129
|
}
|
|
118
130
|
|
|
119
131
|
/** 捕获事件订阅者接口与消息类型定义。 */
|
package/dist/index.js
CHANGED
|
@@ -46,7 +46,7 @@ function createConsoleObserver(keywords, subscribers) {
|
|
|
46
46
|
}
|
|
47
47
|
|
|
48
48
|
// src/capture/network.ts
|
|
49
|
-
import { getClient } from "@sentry/core";
|
|
49
|
+
import { getClient, getIsolationScope } from "@sentry/core";
|
|
50
50
|
|
|
51
51
|
// ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/debug-build.js
|
|
52
52
|
var DEBUG_BUILD = typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__;
|
|
@@ -210,12 +210,15 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
210
210
|
const shouldIgnore = createUrlMatcher(opts.ignoreUrls);
|
|
211
211
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
212
212
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
213
|
+
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
213
214
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
214
215
|
if (statusCode == null) return;
|
|
215
216
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
216
217
|
const pgRestError = parsePgRestErrorBody(responseBody);
|
|
217
218
|
if (!pgRestError) return;
|
|
218
219
|
if (pgRestWhitelistCodes.length > 0 && !pgRestWhitelistCodes.includes(pgRestError.code)) return;
|
|
220
|
+
const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
|
|
221
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
219
222
|
const info = {
|
|
220
223
|
type: requestType,
|
|
221
224
|
method,
|
|
@@ -223,7 +226,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
223
226
|
statusCode,
|
|
224
227
|
pgRestError,
|
|
225
228
|
requestBody,
|
|
226
|
-
responseBody
|
|
229
|
+
responseBody,
|
|
230
|
+
precedingBreadcrumbs
|
|
227
231
|
};
|
|
228
232
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
229
233
|
}
|
|
@@ -49,7 +49,7 @@ function createConsoleObserver(keywords, subscribers) {
|
|
|
49
49
|
}
|
|
50
50
|
|
|
51
51
|
// src/capture/network.ts
|
|
52
|
-
import { getClient } from "@sentry/core";
|
|
52
|
+
import { getClient, getIsolationScope } from "@sentry/core";
|
|
53
53
|
|
|
54
54
|
// ../../node_modules/.pnpm/@sentry-internal+browser-utils@9.47.1/node_modules/@sentry-internal/browser-utils/build/esm/debug-build.js
|
|
55
55
|
var DEBUG_BUILD = typeof __SENTRY_DEBUG__ === "undefined" || __SENTRY_DEBUG__;
|
|
@@ -213,12 +213,15 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
213
213
|
const shouldIgnore = createUrlMatcher(opts.ignoreUrls);
|
|
214
214
|
const pgRestPatterns = opts.pgRestUrlPatterns ?? DEFAULT_PGREST_URL_PATTERNS;
|
|
215
215
|
const pgRestWhitelistCodes = opts.pgRestWhitelistCodes ?? DEFAULT_PGREST_WHITELIST_CODES;
|
|
216
|
+
const precedingBreadcrumbsLimit = opts.precedingBreadcrumbsLimit;
|
|
216
217
|
function tryDispatchPgRestWhitelistError(requestType, method, url, statusCode, requestBody, responseBody) {
|
|
217
218
|
if (statusCode == null) return;
|
|
218
219
|
if (!isPgRestRequest(url, pgRestPatterns)) return;
|
|
219
220
|
const pgRestError = parsePgRestErrorBody(responseBody);
|
|
220
221
|
if (!pgRestError) return;
|
|
221
222
|
if (pgRestWhitelistCodes.length > 0 && !pgRestWhitelistCodes.includes(pgRestError.code)) return;
|
|
223
|
+
const allBreadcrumbs = getIsolationScope().getScopeData().breadcrumbs;
|
|
224
|
+
const precedingBreadcrumbs = precedingBreadcrumbsLimit != null ? allBreadcrumbs.slice(-precedingBreadcrumbsLimit) : allBreadcrumbs.slice();
|
|
222
225
|
const info = {
|
|
223
226
|
type: requestType,
|
|
224
227
|
method,
|
|
@@ -226,7 +229,8 @@ function createNetworkObserver(opts, subscribers) {
|
|
|
226
229
|
statusCode,
|
|
227
230
|
pgRestError,
|
|
228
231
|
requestBody,
|
|
229
|
-
responseBody
|
|
232
|
+
responseBody,
|
|
233
|
+
precedingBreadcrumbs
|
|
230
234
|
};
|
|
231
235
|
for (const sub of subscribers) sub.onPgRestWhitelistError(info);
|
|
232
236
|
}
|
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.5",
|
|
4
4
|
"description": "React admin 应用开发工具集",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -18,12 +18,13 @@
|
|
|
18
18
|
"import": "./dist/vite/index.js",
|
|
19
19
|
"require": "./dist/vite/index.cjs"
|
|
20
20
|
},
|
|
21
|
-
"./cloud/vite.config": "./cloud/vite.config.
|
|
21
|
+
"./cloud/vite.config": "./cloud/vite.config.js",
|
|
22
22
|
"./tsconfig-base": "./tsconfig-base.json"
|
|
23
23
|
},
|
|
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);
|