miaoda-react-devkit 0.1.1-beta.4 → 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/package.json +2 -1
- package/cloud/vite.config.ts +0 -156
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",
|
|
@@ -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);
|