generator-mico-cli 0.2.31 → 0.2.32
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/README.md +145 -18
- package/bin/mico.js +76 -0
- package/generators/h5-react/ignore-list.json +1 -0
- package/generators/h5-react/index.js +349 -0
- package/generators/h5-react/meta.json +11 -0
- package/generators/h5-react/templates/.commitlintrc.js +7 -0
- package/generators/h5-react/templates/.cursor/rules/cicd-deploy.mdc +104 -0
- package/generators/h5-react/templates/.cursor/rules/common-intl.mdc +42 -0
- package/generators/h5-react/templates/.cursor/rules/git-hooks.mdc +40 -0
- package/generators/h5-react/templates/.cursor/rules/internal-packages.mdc +46 -0
- package/generators/h5-react/templates/.cursor/rules/monorepo.mdc +64 -0
- package/generators/h5-react/templates/.cursor/rules/package-json.mdc +52 -0
- package/generators/h5-react/templates/.cursor/rules/tailwind-umi.mdc +60 -0
- package/generators/h5-react/templates/.cursor/rules/umi-app.mdc +74 -0
- package/generators/h5-react/templates/.cursor/rules/umi-config.mdc +86 -0
- package/generators/h5-react/templates/.cursor/rules/umi-mock.mdc +80 -0
- package/generators/h5-react/templates/.cursor/rules/workspace-request.mdc +52 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/SKILL.md +213 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/evals/evals.json +23 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/cursor-rule-template.md +60 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/phase-1-scanning.md +102 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/phase-2-context-analysis.md +102 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/phase-3-pattern-extraction.md +105 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/phase-4-module-mapping.md +65 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/phase-5-glossary.md +63 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/templates/DEV_PATTERNS.tpl.md +77 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/templates/GLOSSARY.tpl.md +17 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/templates/MODULE_MAP.tpl.md +45 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/templates/PROJECT_CONTEXT.tpl.md +155 -0
- package/generators/h5-react/templates/.cursor/skills/biz-app-analyzer/references/update-mode.md +116 -0
- package/generators/h5-react/templates/.env.development +5 -0
- package/generators/h5-react/templates/.env.production +5 -0
- package/generators/h5-react/templates/.env.testing +5 -0
- package/generators/h5-react/templates/.husky/commit-msg +2 -0
- package/generators/h5-react/templates/.husky/pre-commit +2 -0
- package/generators/h5-react/templates/.lintstagedrc.js +8 -0
- package/generators/h5-react/templates/.prettierrc.json +7 -0
- package/generators/h5-react/templates/CICD/before_build.sh +76 -0
- package/generators/h5-react/templates/CICD/start_dev.sh +54 -0
- package/generators/h5-react/templates/CICD/start_local.sh +30 -0
- package/generators/h5-react/templates/CICD/start_prod.sh +53 -0
- package/generators/h5-react/templates/CICD/start_test.sh +55 -0
- package/generators/h5-react/templates/CICD/wangsu_fresh_dev.sh +19 -0
- package/generators/h5-react/templates/CICD/wangsu_fresh_prod.sh +19 -0
- package/generators/h5-react/templates/CICD/wangsu_fresh_test.sh +19 -0
- package/generators/h5-react/templates/README.md +301 -0
- package/generators/h5-react/templates/_gitignore +30 -0
- package/generators/h5-react/templates/_npmrc +6 -0
- package/generators/h5-react/templates/apps/.gitkeep +0 -0
- package/generators/h5-react/templates/dev.preset.json +10 -0
- package/generators/h5-react/templates/package.json +56 -0
- package/generators/h5-react/templates/packages/common-intl/README.md +180 -0
- package/generators/h5-react/templates/packages/common-intl/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/common-intl/package.json +31 -0
- package/generators/h5-react/templates/packages/common-intl/src/index.ts +3 -0
- package/generators/h5-react/templates/packages/common-intl/src/intl.ts +100 -0
- package/generators/h5-react/templates/packages/common-intl/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/components/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/components/package.json +32 -0
- package/generators/h5-react/templates/packages/components/src/Layout/ImmersiveHeader.tsx +126 -0
- package/generators/h5-react/templates/packages/components/src/Layout/LayoutFooter.tsx +72 -0
- package/generators/h5-react/templates/packages/components/src/Layout/index.tsx +121 -0
- package/generators/h5-react/templates/packages/components/src/assets/image/back.png +0 -0
- package/generators/h5-react/templates/packages/components/src/index.ts +0 -0
- package/generators/h5-react/templates/packages/components/tsconfig.json +13 -0
- package/generators/h5-react/templates/packages/components/typings.d.ts +1 -0
- package/generators/h5-react/templates/packages/constant/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/constant/package.json +19 -0
- package/generators/h5-react/templates/packages/constant/src/index.ts +0 -0
- package/generators/h5-react/templates/packages/constant/src/member.ts +8 -0
- package/generators/h5-react/templates/packages/constant/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/deeplink/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/deeplink/package.json +18 -0
- package/generators/h5-react/templates/packages/deeplink/src/index.ts +7 -0
- package/generators/h5-react/templates/packages/deeplink/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/domain/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/domain/package.json +18 -0
- package/generators/h5-react/templates/packages/domain/src/index.ts +29 -0
- package/generators/h5-react/templates/packages/domain/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/domain/types.d.ts +11 -0
- package/generators/h5-react/templates/packages/eslint/eslint.config.base.ts +36 -0
- package/generators/h5-react/templates/packages/eslint/eslint.config.react.ts +33 -0
- package/generators/h5-react/templates/packages/eslint/package.json +22 -0
- package/generators/h5-react/templates/packages/js-bridge/eslint.config.ts +17 -0
- package/generators/h5-react/templates/packages/js-bridge/package.json +23 -0
- package/generators/h5-react/templates/packages/js-bridge/src/call.ts +126 -0
- package/generators/h5-react/templates/packages/js-bridge/src/closeH5Page.ts +9 -0
- package/generators/h5-react/templates/packages/js-bridge/src/getUserInfo.ts +96 -0
- package/generators/h5-react/templates/packages/js-bridge/src/index.ts +15 -0
- package/generators/h5-react/templates/packages/js-bridge/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/js-bridge/type.d.ts +24 -0
- package/generators/h5-react/templates/packages/request/axios.d.ts +42 -0
- package/generators/h5-react/templates/packages/request/eslint.config.ts +17 -0
- package/generators/h5-react/templates/packages/request/package.json +22 -0
- package/generators/h5-react/templates/packages/request/src/index.ts +165 -0
- package/generators/h5-react/templates/packages/request/src/interceptors.ts +126 -0
- package/generators/h5-react/templates/packages/request/src/types.ts +101 -0
- package/generators/h5-react/templates/packages/request/src/url-resolver.ts +66 -0
- package/generators/h5-react/templates/packages/request/src/utils.ts +12 -0
- package/generators/h5-react/templates/packages/request/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/request/umi.d.ts +94 -0
- package/generators/h5-react/templates/packages/typescript/package.json +11 -0
- package/generators/h5-react/templates/packages/typescript/tsconfig.base.json +23 -0
- package/generators/h5-react/templates/packages/typescript/tsconfig.react.json +7 -0
- package/generators/h5-react/templates/packages/umi-config/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/umi-config/package.json +31 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.dev.ts +34 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.prod.development.ts +17 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.prod.production.ts +42 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.prod.testing.ts +17 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.prod.ts +56 -0
- package/generators/h5-react/templates/packages/umi-config/src/config.ts +86 -0
- package/generators/h5-react/templates/packages/umi-config/src/index.ts +25 -0
- package/generators/h5-react/templates/packages/umi-config/src/plugins/apply-sentry-plugin.ts +57 -0
- package/generators/h5-react/templates/packages/umi-config/src/type.d.ts +3 -0
- package/generators/h5-react/templates/packages/umi-config/tsconfig.json +3 -0
- package/generators/h5-react/templates/packages/utils/eslint.config.ts +12 -0
- package/generators/h5-react/templates/packages/utils/package.json +27 -0
- package/generators/h5-react/templates/packages/utils/src/date.ts +21 -0
- package/generators/h5-react/templates/packages/utils/src/env.ts +40 -0
- package/generators/h5-react/templates/packages/utils/src/index.ts +3 -0
- package/generators/h5-react/templates/packages/utils/src/md5.ts +17 -0
- package/generators/h5-react/templates/packages/utils/src/mock.ts +83 -0
- package/generators/h5-react/templates/packages/utils/src/number.ts +23 -0
- package/generators/h5-react/templates/packages/utils/src/tailwind.ts +12 -0
- package/generators/h5-react/templates/packages/utils/src/url.ts +19 -0
- package/generators/h5-react/templates/packages/utils/tsconfig.json +9 -0
- package/generators/h5-react/templates/page.config.ts +1 -0
- package/generators/h5-react/templates/pnpm-workspace.yaml +17 -0
- package/generators/h5-react/templates/scripts/collect-dist.js +78 -0
- package/generators/h5-react/templates/scripts/dev-preset.js +265 -0
- package/generators/h5-react/templates/scripts/dev-preset.schema.json +39 -0
- package/generators/h5-react/templates/scripts/dev.js +133 -0
- package/generators/h5-react/templates/scripts/gateway.ts +241 -0
- package/generators/h5-react/templates/turbo.json +86 -0
- package/generators/subapp-h5/ignore-list.json +1 -0
- package/generators/subapp-h5/index.js +424 -0
- package/generators/subapp-h5/meta.json +10 -0
- package/generators/subapp-h5/templates/.env +1 -0
- package/generators/subapp-h5/templates/.stylelintrc.js +22 -0
- package/generators/subapp-h5/templates/config/config.dev.ts +7 -0
- package/generators/subapp-h5/templates/config/config.prod.development.ts +7 -0
- package/generators/subapp-h5/templates/config/config.prod.production.ts +10 -0
- package/generators/subapp-h5/templates/config/config.prod.testing.ts +7 -0
- package/generators/subapp-h5/templates/config/config.prod.ts +7 -0
- package/generators/subapp-h5/templates/config/config.ts +6 -0
- package/generators/subapp-h5/templates/config/routes.ts +13 -0
- package/generators/subapp-h5/templates/eslint.config.ts +12 -0
- package/generators/subapp-h5/templates/mock/user.ts +34 -0
- package/generators/subapp-h5/templates/package.json +42 -0
- package/generators/subapp-h5/templates/src/app.tsx +14 -0
- package/generators/subapp-h5/templates/src/assets/yay.jpg +0 -0
- package/generators/subapp-h5/templates/src/intl.ts +37 -0
- package/generators/subapp-h5/templates/src/layouts/index.tsx +10 -0
- package/generators/subapp-h5/templates/src/pages/index.tsx +22 -0
- package/generators/subapp-h5/templates/src/services/user.ts +38 -0
- package/generators/subapp-h5/templates/tailwind.config.js +16 -0
- package/generators/subapp-h5/templates/tailwind.css +7 -0
- package/generators/subapp-h5/templates/tsconfig.json +3 -0
- package/generators/subapp-h5/templates/typings.d.ts +1 -0
- package/lib/setup-multica-desktop.js +154 -0
- package/package.json +1 -1
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地开发网关脚本:读取根目录 `page.config.ts`,为每个 `apps/<key>` 分配端口并启动 `pnpm dev`,
|
|
3
|
+
* 再启动统一 HTTP/WebSocket 代理(默认从 3000 起占端口),通过路径前缀将流量转发到对应子应用。
|
|
4
|
+
*/
|
|
5
|
+
import http from 'http';
|
|
6
|
+
import path from 'path';
|
|
7
|
+
import fs from 'fs';
|
|
8
|
+
import { spawn, type ChildProcessWithoutNullStreams } from 'child_process';
|
|
9
|
+
import httpProxy from 'http-proxy';
|
|
10
|
+
import portfinder from 'portfinder';
|
|
11
|
+
import chalk from 'chalk';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* 仓库根目录的 `page.config.ts`:键名为子应用目录名(对应 `apps/<key>`)。
|
|
15
|
+
*/
|
|
16
|
+
import pageConfig from '../page.config.ts';
|
|
17
|
+
|
|
18
|
+
/** 单个子应用在网关中的运行时元数据(端口、路径前缀等)。 */
|
|
19
|
+
interface PageConfig {
|
|
20
|
+
key: string;
|
|
21
|
+
name: string;
|
|
22
|
+
fullPath: string;
|
|
23
|
+
prefixPath: string;
|
|
24
|
+
port: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* 遍历 `page.config.ts` 的每个键,在 `apps/<key>` 下读取 `package.json`,
|
|
29
|
+
* 并为每个子应用从 4000 起顺序分配可用端口,生成 `PageConfig` 列表。
|
|
30
|
+
*/
|
|
31
|
+
async function formatPageConfig(): Promise<PageConfig[]> {
|
|
32
|
+
const pageConfigs: PageConfig[] = [];
|
|
33
|
+
const appPath = path.join(process.cwd(), 'apps');
|
|
34
|
+
let basePort = 4000;
|
|
35
|
+
for (const [key, value] of Object.entries(pageConfig)) {
|
|
36
|
+
const appPackagePath = path.join(appPath, key, 'package.json');
|
|
37
|
+
try {
|
|
38
|
+
const appPackage = await fs.promises.readFile(appPackagePath, 'utf8');
|
|
39
|
+
const appPackageJson = JSON.parse(appPackage);
|
|
40
|
+
const port = await portfinder.getPortPromise({ port: basePort });
|
|
41
|
+
basePort = port + 1;
|
|
42
|
+
const pageConfig: PageConfig = {
|
|
43
|
+
key,
|
|
44
|
+
name: appPackageJson.name,
|
|
45
|
+
fullPath: path.join(appPath, key),
|
|
46
|
+
prefixPath: `/${key}`,
|
|
47
|
+
port,
|
|
48
|
+
};
|
|
49
|
+
pageConfigs.push(pageConfig);
|
|
50
|
+
} catch (error) {
|
|
51
|
+
console.error(`[${key}] 读取 package.json 失败: ${(error as Error).message}`);
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return pageConfigs;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 在各自 `cwd` 下执行 `pnpm run dev -p <port>` 启动每个子应用。
|
|
60
|
+
* 监听 stdout 出现 Ready/successfully 时视为已就绪并 resolve(stderr 常含非错误日志,见下方)。
|
|
61
|
+
*/
|
|
62
|
+
async function runApp(pageConfigs: PageConfig[]) {
|
|
63
|
+
const promises = pageConfigs.map(config => {
|
|
64
|
+
return new Promise<ChildProcessWithoutNullStreams>((resolve, reject) => {
|
|
65
|
+
const child = spawn('pnpm', ['run', 'dev'], {
|
|
66
|
+
cwd: config.fullPath,
|
|
67
|
+
env: { ...process.env, PORT: String(config.port) },
|
|
68
|
+
// 如需在终端直接看到子应用原始输出,可改为 stdio: 'inherit'(需自行调整就绪判断逻辑)
|
|
69
|
+
shell: process.platform === 'win32',
|
|
70
|
+
});
|
|
71
|
+
child.on('error', (err) => {
|
|
72
|
+
console.error(chalk.red(`[${config.key}] 子进程启动失败:`), err.message);
|
|
73
|
+
});
|
|
74
|
+
child.on('exit', (code, signal) => {
|
|
75
|
+
if (code !== null && code !== 0) {
|
|
76
|
+
console.error(chalk.red(`[${config.key}] 子进程退出 code=${code} signal=${signal}`));
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
child.stdout.on('data', (data) => {
|
|
80
|
+
const msg = data.toString();
|
|
81
|
+
console.log(`${chalk.green(`[${config.key}]`)} ${msg}`);
|
|
82
|
+
const reg = /Local: http:\/\/localhost:(\d+)/
|
|
83
|
+
const match = msg.match(reg);
|
|
84
|
+
if (match) {
|
|
85
|
+
const port = match[1];
|
|
86
|
+
config.port = parseInt(port);
|
|
87
|
+
setTimeout(() => {
|
|
88
|
+
resolve(child)
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
child.stderr.on('data', (data) => {
|
|
93
|
+
const msg = data.toString();
|
|
94
|
+
console.log(`${chalk.dim(`[${config.key}]`)} ${msg}`);
|
|
95
|
+
});
|
|
96
|
+
})
|
|
97
|
+
})
|
|
98
|
+
return Promise.all(promises).then((childs) => {
|
|
99
|
+
console.log(chalk.green('所有子应用已启动'))
|
|
100
|
+
pageConfigs.forEach(config => {
|
|
101
|
+
console.log(`${chalk.green(`[${config.key}]`)} 子应用已启动:${chalk.blue(`http://127.0.0.1:${config.port}`)}`)
|
|
102
|
+
})
|
|
103
|
+
return childs
|
|
104
|
+
})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 根据当前请求解析应转发到的子应用:优先用路径匹配;无 Referer 或 Referer 为根时用请求路径;
|
|
109
|
+
* 否则用 Referer 的路径(便于静态资源等请求仍落到同一子应用)。无匹配时回退上次目标或第一个应用。
|
|
110
|
+
*/
|
|
111
|
+
function getTargetByRes(req: http.IncomingMessage, pageConfigs: PageConfig[], lastTargetPageConfig: PageConfig | null) {
|
|
112
|
+
const pathname = urlPathname(req.url) ?? '/';
|
|
113
|
+
const refererPathname = urlPathname(req.headers.referer)
|
|
114
|
+
const targetPathname = (
|
|
115
|
+
!refererPathname || refererPathname === '/'
|
|
116
|
+
? pathname
|
|
117
|
+
: refererPathname
|
|
118
|
+
)
|
|
119
|
+
const pathConfig = getTargetByPath(targetPathname, pageConfigs);
|
|
120
|
+
return pathConfig ?? lastTargetPageConfig ?? pageConfigs[0]!;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/** 判断 `pathname` 是否属于某子应用(等于 `prefixPath` 或以其为前缀)。 */
|
|
124
|
+
function getTargetByPath(pathname: string, pageConfigs: PageConfig[]) {
|
|
125
|
+
for (const config of pageConfigs) {
|
|
126
|
+
if (pathname === config.prefixPath || pathname.startsWith(`${config.prefixPath}/`)) {
|
|
127
|
+
return config;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 从完整 URL 或相对路径中解析 pathname;无效输入返回 `null`。 */
|
|
134
|
+
function urlPathname(url: string | undefined) {
|
|
135
|
+
if (!url) return null;
|
|
136
|
+
try {
|
|
137
|
+
return new URL(url, 'http://127.0.0.1').pathname || null;
|
|
138
|
+
} catch {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* 转发到子应用 dev 服务器前去掉网关层前缀(如 `/app`),使子应用收到根路径下的请求。
|
|
145
|
+
*/
|
|
146
|
+
function rewriteUrlForTarget(rawUrl: string | undefined, target: PageConfig): string {
|
|
147
|
+
if (!rawUrl) return '/';
|
|
148
|
+
const url = new URL(rawUrl, 'http://127.0.0.1');
|
|
149
|
+
url.pathname = url.pathname.replace(new RegExp(`^${target.prefixPath}`), '') || '/';
|
|
150
|
+
return url.toString();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 在可用端口(默认从 3000 起探测)上启动 HTTP 代理,支持 WebSocket;
|
|
155
|
+
* 根路径 `/` 返回各子应用入口链接,其余请求按路由规则转发到对应本地端口。
|
|
156
|
+
*/
|
|
157
|
+
async function runProxyServer(pageConfigs: PageConfig[]) {
|
|
158
|
+
const proxy = httpProxy.createProxyServer({ ws: true });
|
|
159
|
+
proxy.on('error', (err, req, res) => {
|
|
160
|
+
console.error(chalk.red('[proxy error]'), err.message);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
let lastTargetPageConfig: PageConfig | null = null;
|
|
164
|
+
|
|
165
|
+
const server = http.createServer((req, res) => {
|
|
166
|
+
const pathname = urlPathname(req.url) ?? '/';
|
|
167
|
+
if (pathname === '/' || pathname === '') {
|
|
168
|
+
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
|
169
|
+
const links = pageConfigs
|
|
170
|
+
.map((c) => ` <li><a href="${c.prefixPath}">${c.prefixPath}</a> (${c.key})</li>`)
|
|
171
|
+
.join('\n');
|
|
172
|
+
res.end(
|
|
173
|
+
`<!DOCTYPE html><html><body><h1>子应用入口</h1><ul>\n${links}\n</ul></body></html>`
|
|
174
|
+
);
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
lastTargetPageConfig = getTargetByRes(req, pageConfigs, lastTargetPageConfig);
|
|
179
|
+
req.url = rewriteUrlForTarget(req.url, lastTargetPageConfig);
|
|
180
|
+
proxy.web(req, res, {
|
|
181
|
+
target: `http://127.0.0.1:${lastTargetPageConfig.port}`,
|
|
182
|
+
changeOrigin: true,
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
server.on('upgrade', (req, socket, head) => {
|
|
187
|
+
lastTargetPageConfig = getTargetByRes(req, pageConfigs, lastTargetPageConfig);
|
|
188
|
+
req.url = rewriteUrlForTarget(req.url, lastTargetPageConfig);
|
|
189
|
+
proxy.ws(req, socket, head, {
|
|
190
|
+
target: `http://127.0.0.1:${lastTargetPageConfig.port}`,
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
const port = await portfinder.getPortPromise({ port: 3000 });
|
|
194
|
+
server.listen(port, () => {
|
|
195
|
+
console.log(chalk.green(`[gateway] 代理服务已启动 端口:${chalk.blue(port)}`));
|
|
196
|
+
console.log(`>>> 统一通过代理访问: ${chalk.blue(`http://127.0.0.1:${port}`)}`);
|
|
197
|
+
pageConfigs.forEach(config => {
|
|
198
|
+
console.log(`${chalk.green(`[${config.key}]`)} 子应用访问路径:${chalk.blue(`http://127.0.0.1:${port}${config.prefixPath}`)}`)
|
|
199
|
+
})
|
|
200
|
+
});
|
|
201
|
+
return server;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* 在收到 SIGINT(Ctrl+C)或 SIGTERM 时关闭代理 `server` 并终止所有子进程;
|
|
206
|
+
* `server.close` 若迟迟不回调,3 秒后强制 `process.exit(0)`。
|
|
207
|
+
*/
|
|
208
|
+
function listenerClose(server: http.Server, childs: ChildProcessWithoutNullStreams[]) {
|
|
209
|
+
let cleaning = false;
|
|
210
|
+
const cleanup = () => {
|
|
211
|
+
if (cleaning) return;
|
|
212
|
+
cleaning = true;
|
|
213
|
+
|
|
214
|
+
server.close(() => {
|
|
215
|
+
childs.forEach((child) => {
|
|
216
|
+
if (child.killed) return;
|
|
217
|
+
child.kill('SIGTERM');
|
|
218
|
+
});
|
|
219
|
+
process.exit(0);
|
|
220
|
+
});
|
|
221
|
+
// 若 server.close 回调未及时触发,超时后强制退出
|
|
222
|
+
setTimeout(() => process.exit(0), 3000);
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
process.on('SIGINT', cleanup);
|
|
226
|
+
process.on('SIGTERM', cleanup);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/** 加载配置 → 拉起子应用 → 启动网关代理 → 注册退出清理。 */
|
|
230
|
+
async function main() {
|
|
231
|
+
const pageConfigs = await formatPageConfig();
|
|
232
|
+
if (pageConfigs.length === 0) {
|
|
233
|
+
console.error('未开启任何子应用,请检查 page.config.ts 配置');
|
|
234
|
+
process.exit(1);
|
|
235
|
+
}
|
|
236
|
+
const childs = await runApp(pageConfigs);
|
|
237
|
+
const proxyServer = await runProxyServer(pageConfigs);
|
|
238
|
+
listenerClose(proxyServer, childs);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
main()
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://turbo.build/schema.json",
|
|
3
|
+
"globalEnv": [
|
|
4
|
+
"CDN_PUBLIC_PATH",
|
|
5
|
+
"BRANCH_OR_TAG",
|
|
6
|
+
"VERSION",
|
|
7
|
+
"SENTRY_AUTH_TOKEN"
|
|
8
|
+
],
|
|
9
|
+
"envMode": "loose",
|
|
10
|
+
"tasks": {
|
|
11
|
+
"build": {
|
|
12
|
+
"dependsOn": ["^build"],
|
|
13
|
+
"inputs": [
|
|
14
|
+
"$TURBO_DEFAULT$",
|
|
15
|
+
"!.umi/**",
|
|
16
|
+
"!.turbo/**",
|
|
17
|
+
"!src/.umi/**",
|
|
18
|
+
"!src/.umi-production/**",
|
|
19
|
+
"!src/.umi-test/**",
|
|
20
|
+
".env",
|
|
21
|
+
".env.local",
|
|
22
|
+
".env.production",
|
|
23
|
+
".env.production.local"
|
|
24
|
+
],
|
|
25
|
+
"outputs": ["dist/**"]
|
|
26
|
+
},
|
|
27
|
+
"build:development": {
|
|
28
|
+
"dependsOn": ["^build:development"],
|
|
29
|
+
"inputs": [
|
|
30
|
+
"$TURBO_DEFAULT$",
|
|
31
|
+
"!.umi/**",
|
|
32
|
+
"!.turbo/**",
|
|
33
|
+
"!src/.umi/**",
|
|
34
|
+
"!src/.umi-production/**",
|
|
35
|
+
"!src/.umi-test/**",
|
|
36
|
+
".env",
|
|
37
|
+
".env.local",
|
|
38
|
+
".env.development",
|
|
39
|
+
".env.development.local"
|
|
40
|
+
],
|
|
41
|
+
"outputs": ["dist/**"]
|
|
42
|
+
},
|
|
43
|
+
"build:testing": {
|
|
44
|
+
"dependsOn": ["^build:testing"],
|
|
45
|
+
"inputs": [
|
|
46
|
+
"$TURBO_DEFAULT$",
|
|
47
|
+
"!.umi/**",
|
|
48
|
+
"!.turbo/**",
|
|
49
|
+
"!src/.umi/**",
|
|
50
|
+
"!src/.umi-production/**",
|
|
51
|
+
"!src/.umi-test/**",
|
|
52
|
+
".env",
|
|
53
|
+
".env.local",
|
|
54
|
+
".env.testing",
|
|
55
|
+
".env.testing.local"
|
|
56
|
+
],
|
|
57
|
+
"outputs": ["dist/**"]
|
|
58
|
+
},
|
|
59
|
+
"build:production": {
|
|
60
|
+
"dependsOn": ["^build:production"],
|
|
61
|
+
"inputs": [
|
|
62
|
+
"$TURBO_DEFAULT$",
|
|
63
|
+
"!.umi/**",
|
|
64
|
+
"!.turbo/**",
|
|
65
|
+
"!src/.umi/**",
|
|
66
|
+
"!src/.umi-production/**",
|
|
67
|
+
"!src/.umi-test/**",
|
|
68
|
+
".env",
|
|
69
|
+
".env.local",
|
|
70
|
+
".env.production",
|
|
71
|
+
".env.production.local"
|
|
72
|
+
],
|
|
73
|
+
"outputs": ["dist/**"]
|
|
74
|
+
},
|
|
75
|
+
"dev": {
|
|
76
|
+
"cache": false,
|
|
77
|
+
"persistent": true
|
|
78
|
+
},
|
|
79
|
+
"lint": {
|
|
80
|
+
"outputs": []
|
|
81
|
+
},
|
|
82
|
+
"lint:fix": {
|
|
83
|
+
"outputs": []
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
["node_modules", "dist", ".umi", ".umi-production", ".turbo"]
|