@dd-code/uni-tools 1.0.13 → 1.0.15
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 +100 -32
- package/dist/cli.js +78 -36
- package/dist/cli.mjs.js +78 -36
- package/dist/index.d.ts +5 -1
- package/dist/index.js +474 -6
- package/dist/index.mjs.js +474 -6
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -2,16 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
`@dd-code/uni-tools` 是一个针对 UniApp 的微信小程序(mp-weixin)模块化解决方案与工具集,目标是让「主应用 + 多子应用(分包)」的开发、联调与构建更顺畅、更自动化。
|
|
4
4
|
|
|
5
|
-
##
|
|
6
|
-
-
|
|
7
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
5
|
+
## 特性
|
|
6
|
+
- 主/子应用模块化:主应用负责壳与公共能力,子应用以分包形式接入。
|
|
7
|
+
- 配置自动合并:自动汇总各子应用的 `pages.json` 并生成最终 `app.json`。
|
|
8
|
+
- 开发态联调:监听子应用构建输出并同步至主应用分包目录,支持 WS 通知。
|
|
9
|
+
- 运行时暴露:通过 `@dd-code/runtime` 统一导出公共模块(如 `store/index.js`)。
|
|
10
|
+
- 一体化 CLI:拉取子应用仓库、启动开发、执行构建,开箱即用。
|
|
10
11
|
|
|
11
12
|
## 有哪些命令
|
|
12
|
-
- `npx uni-tools fetch
|
|
13
|
-
- `npx uni-tools serve -p mp-weixin --mode development
|
|
14
|
-
- `npx uni-tools build -p mp-weixin --mode production
|
|
13
|
+
- `npx uni-tools fetch`:交互式拉取子应用仓库到本地(packages/uni-tools/src/cli.ts:20)。
|
|
14
|
+
- `npx uni-tools serve -p mp-weixin --mode development`:启动开发态,识别主/子应用并启用对应插件(packages/uni-tools/src/cli.ts:14)。
|
|
15
|
+
- `npx uni-tools build -p mp-weixin --mode production`:生产构建输出主应用与分包(packages/uni-tools/src/cli.ts:52)。
|
|
15
16
|
- 可选参数:
|
|
16
17
|
- `-p <platform>`:平台,默认 `h5`,支持 `mp-weixin`。
|
|
17
18
|
- `--mode <mode>`:模式,默认 `development`。
|
|
@@ -25,18 +26,29 @@
|
|
|
25
26
|
- 生产构建可控:按环境与模式输出稳定的主应用与分包结构,避免配置漂移与遗漏。
|
|
26
27
|
- 仓库管理简化:通过交互命令拉取/更新子应用仓库,快速搭建本地联调环境。
|
|
27
28
|
|
|
28
|
-
##
|
|
29
|
-
-
|
|
30
|
-
|
|
31
|
-
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
|
|
36
|
-
-
|
|
37
|
-
-
|
|
38
|
-
-
|
|
39
|
-
-
|
|
29
|
+
## 架构与插件
|
|
30
|
+
- 入口与平台分发:`packages/uni-tools/src/plugins/mp-weixin.ts:8-22`
|
|
31
|
+
- 目录分层:`core/ + plugins/ + server/ + utils`,职责明确、低耦合
|
|
32
|
+
- `core`:运行时与 `app.json` 处理(如 `core/app-json.ts`、`core/runtime.ts`)
|
|
33
|
+
- `plugins`:Vite 插件(主应用、exposes 等)
|
|
34
|
+
- `server`:HTTP/WS 服务
|
|
35
|
+
- `utils`:通用工具(文件复制、下载等)
|
|
36
|
+
- Manifest 管理与 `app.json` 合并:`packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:31-76, 105-119, 261-279`
|
|
37
|
+
- 汇总子应用 `pages.json`,渲染为主应用分包配置并写入 `app.json`
|
|
38
|
+
- 写入采用内容比较,避免频繁重启与重复打包(`core/app-json.ts`)
|
|
39
|
+
- 开发态联调:`packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-156, 187-245`
|
|
40
|
+
- 主应用启动 WS 服务,子应用变更触发同步与增量更新
|
|
41
|
+
- 监听拷贝使用内容比较,避免无效触发(`utils/copy.ts`)
|
|
42
|
+
|
|
43
|
+
## 运行时暴露(Runtime Exposes)
|
|
44
|
+
- 目标:在任意页面通过 `@dd-code/runtime` 访问公共模块导出(如 `store/index.js`)
|
|
45
|
+
- 使用方式:
|
|
46
|
+
- 在页面或模块中 `import { userStore } from '@dd-code/runtime'`
|
|
47
|
+
- 插件在打包阶段生成 `__mfe_runtime__.js` 于根目录,并将各处引用指向它
|
|
48
|
+
- 实现要点:
|
|
49
|
+
- `load` 阶段生成占位引用,`generateBundle` 计算相对路径并替换(`packages/uni-tools/src/plugins/modules/mp-weixin/plugins/exposes.ts:91-109`)
|
|
50
|
+
- 所有 `@dd-code/runtime/*` 形式在 `resolveId` 阶段重定向到同一虚拟模块(保持单一产物)
|
|
51
|
+
- 可通过 `options.exposes` 扩展需要暴露的模块与导出
|
|
40
52
|
|
|
41
53
|
## 配置与环境变量
|
|
42
54
|
- `mfe.json`(项目根目录,packages/uni-tools/src/config/const.ts:1)
|
|
@@ -70,18 +82,18 @@
|
|
|
70
82
|
1. 安装依赖:
|
|
71
83
|
- 使用你的包管理器安装 `@dd-code/uni-tools`。
|
|
72
84
|
2. 在 `vite.config.ts` 中启用插件:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
+
```ts
|
|
86
|
+
import { defineConfig } from 'vite';
|
|
87
|
+
import uni from '@dcloudio/vite-plugin-uni';
|
|
88
|
+
import uniTools from '@dd-code/uni-tools';
|
|
89
|
+
|
|
90
|
+
export default defineConfig({
|
|
91
|
+
plugins: [
|
|
92
|
+
uni(),
|
|
93
|
+
uniTools()
|
|
94
|
+
],
|
|
95
|
+
});
|
|
96
|
+
```
|
|
85
97
|
3. 拉取子应用仓库:`npx uni-tools fetch`
|
|
86
98
|
4. 启动开发:`npx uni-tools serve -p mp-weixin --mode development`
|
|
87
99
|
5. 构建生产:`npx uni-tools build -p mp-weixin --mode production`
|
|
@@ -118,6 +130,11 @@
|
|
|
118
130
|
- 子应用:分包形式接入,拥有独立 `pages.json` 与资源;构建输出由插件监听并同步。
|
|
119
131
|
- Manifest 与分包配置生成:读取各子应用 `pages.json` 并渲染至主应用 `subPackages`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:41-69)。
|
|
120
132
|
|
|
133
|
+
## 常见问题
|
|
134
|
+
- 端口被占用(`MFE_UNI_SERVE=true`):HTTP/WS 服务具备幂等复用与端口探测,避免重复 `listen`(packages/uni-tools/src/plugins/modules/mp-weixin/server/http-server.ts)。
|
|
135
|
+
- 开发态频繁重启/重复打包:写入 `app.json` 与文件拷贝均进行内容比较,仅在变更时写入(`core/app-json.ts`、`utils/copy.ts`)。
|
|
136
|
+
- CLI 交互报错(inquirer 兼容):已采用动态导入并移动到依赖,`npx` 下可正常使用(packages/uni-tools/src/plugins/modules/mp-weixin/gitlib/index.ts, packages/uni-tools/package.json)。
|
|
137
|
+
|
|
121
138
|
## 适用场景
|
|
122
139
|
- 同一个小程序中承载多个业务模块/团队的功能分包,需要高效联调与统一发布。
|
|
123
140
|
- 希望减少主/子应用配置的重复劳动与易错区域。
|
|
@@ -127,3 +144,54 @@
|
|
|
127
144
|
- 开发态会根据 `mode`、`isRoot` 等状态决定是否启动 WS 服务与目录监听。
|
|
128
145
|
- 请确保各子应用的 `pages.json` 合法且页面路径正确,便于自动合并。
|
|
129
146
|
- 使用 `--b root` 时需先启动主应用的 HTTP 服务,以便子应用定位 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59)。
|
|
147
|
+
|
|
148
|
+
## 接入指南(主/子应用)
|
|
149
|
+
- 主应用配置
|
|
150
|
+
- 在 `vite.config.ts` 中启用插件并声明暴露映射:
|
|
151
|
+
```ts
|
|
152
|
+
import { defineConfig } from 'vite';
|
|
153
|
+
import uni from '@dcloudio/vite-plugin-uni';
|
|
154
|
+
import uniTools from '@dd-code/uni-tools';
|
|
155
|
+
|
|
156
|
+
export default defineConfig({
|
|
157
|
+
plugins: [
|
|
158
|
+
uni(),
|
|
159
|
+
uniTools({
|
|
160
|
+
exposes: {
|
|
161
|
+
'.': '@/store/index', // -> @dd-code/runtime
|
|
162
|
+
'./axios': '@/axios/index', // -> @dd-code/runtime/axios
|
|
163
|
+
}
|
|
164
|
+
})
|
|
165
|
+
],
|
|
166
|
+
});
|
|
167
|
+
```
|
|
168
|
+
- 键规则:以 `.` 开头,`'.'` 表示根运行时入口,`'./xxx'` 表示子入口;值为主应用 `src/` 下模块路径,支持 `@/` 别名。
|
|
169
|
+
- 子应用使用
|
|
170
|
+
- 在任意页面或模块中直接引用:
|
|
171
|
+
```ts
|
|
172
|
+
import { userStore } from '@dd-code/runtime';
|
|
173
|
+
import { axios } from '@dd-code/runtime/axios';
|
|
174
|
+
```
|
|
175
|
+
- 构建后,运行时文件输出为根目录 `__mfe_runtime__.js`,各子入口统一指向该文件的导出。
|
|
176
|
+
- 运行与构建约定
|
|
177
|
+
- 主应用 `.env.development`:
|
|
178
|
+
```
|
|
179
|
+
MFE_UNI_IS_ROOT=true
|
|
180
|
+
MFE_UNI_CODE=your-project-code
|
|
181
|
+
MFE_UNI_SERVE=true
|
|
182
|
+
UNI_PLATFORM=mp-weixin
|
|
183
|
+
```
|
|
184
|
+
- 子应用 `.env.development`:
|
|
185
|
+
```
|
|
186
|
+
MFE_UNI_IS_ROOT=false
|
|
187
|
+
MFE_UNI_CODE=your-project-code
|
|
188
|
+
MFE_APP_CODE=modules/your-app
|
|
189
|
+
MFE_UNI_SERVE=true
|
|
190
|
+
UNI_PLATFORM=mp-weixin
|
|
191
|
+
```
|
|
192
|
+
- 开发:主/子应用分别执行 `npx uni-tools serve -p mp-weixin --mode development`,联调时主应用作为 WS 服务端,子应用作为客户端。
|
|
193
|
+
- 子应用直写主应用(不联调):在主应用启动 HTTP 接口后执行 `npx uni-tools build -p mp-weixin --mode development --b root`,产物将按 `appCode` 写入主应用输出目录。
|
|
194
|
+
- 验证与排错
|
|
195
|
+
- 构建后检查根目录是否存在 `__mfe_runtime__.js`。
|
|
196
|
+
- 若子入口未生效,确认主应用 `exposes` 配置键值对应正确,且导出为命名导出。
|
|
197
|
+
- 避免无效重启:确保 `app.json` 与文件拷贝只在内容变化时写入(已内置比较)。
|
package/dist/cli.js
CHANGED
|
@@ -9,6 +9,7 @@ require('crypto');
|
|
|
9
9
|
require('chokidar');
|
|
10
10
|
var child_process = require('child_process');
|
|
11
11
|
var inquirer = require('inquirer');
|
|
12
|
+
var yaml = require('js-yaml');
|
|
12
13
|
|
|
13
14
|
/******************************************************************************
|
|
14
15
|
Copyright (c) Microsoft Corporation.
|
|
@@ -65,6 +66,16 @@ function __generator(thisArg, body) {
|
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
|
|
69
|
+
function __spreadArray(to, from, pack) {
|
|
70
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
71
|
+
if (ar || !(i in from)) {
|
|
72
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
73
|
+
ar[i] = from[i];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
77
|
+
}
|
|
78
|
+
|
|
68
79
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
69
80
|
var e = new Error(message);
|
|
70
81
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
@@ -100,6 +111,9 @@ function uniReadFile(filaPath) {
|
|
|
100
111
|
* @returns {void}
|
|
101
112
|
*/
|
|
102
113
|
function writeFiles(filaPath, content) {
|
|
114
|
+
if (typeof content === "object" && content) {
|
|
115
|
+
content = JSON.stringify(content, null, 2);
|
|
116
|
+
}
|
|
103
117
|
// 检查文件路径是否存在
|
|
104
118
|
var dir = path.dirname(filaPath);
|
|
105
119
|
if (!fs.existsSync(dir)) {
|
|
@@ -132,7 +146,7 @@ var unlinkDeepDirOrFile = function (filePath) {
|
|
|
132
146
|
};
|
|
133
147
|
|
|
134
148
|
// 项目 Git 子模块路径
|
|
135
|
-
var PROJECT_GIT_PATH = "
|
|
149
|
+
var PROJECT_GIT_PATH = "./sub-repo";
|
|
136
150
|
// CDN 文件保存路径
|
|
137
151
|
path.join(process.cwd(), "node_modules/@dd-code/uni-files");
|
|
138
152
|
path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
|
|
@@ -201,30 +215,6 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
|
|
|
201
215
|
process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
|
|
202
216
|
_a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
|
|
203
217
|
cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
|
|
204
|
-
// if (isRoot) {
|
|
205
|
-
// const mfeJson = getMfeJson();
|
|
206
|
-
// const apps = mfeJson.apps?.map((i) => i.appCode) || [];
|
|
207
|
-
// apps.forEach((appCode) => {
|
|
208
|
-
// const appCodeDir = appCode.replace(/\//g, "_");
|
|
209
|
-
// // console.log(appCodeDir, 'appCodeDir');
|
|
210
|
-
// const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
|
|
211
|
-
// console.log(existsSync(appCodeDir), childPath, "childPath");
|
|
212
|
-
// // if (existsSync(appCodeDir)) {
|
|
213
|
-
// // excuteUniCommand(`cd ${childPath} && ${command}`, {
|
|
214
|
-
// // appCode,
|
|
215
|
-
// // target: appCodeDir,
|
|
216
|
-
// // });
|
|
217
|
-
// // 开一个子线程启动
|
|
218
|
-
// // execSync(cmd, {
|
|
219
|
-
// // stdio: "inherit",
|
|
220
|
-
// // });
|
|
221
|
-
// // }
|
|
222
|
-
// });
|
|
223
|
-
// // setTimeout(() => {
|
|
224
|
-
// // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
|
|
225
|
-
// // });
|
|
226
|
-
// }
|
|
227
|
-
// console.log(cmd, "-------");
|
|
228
218
|
child_process.execSync(cmd, {
|
|
229
219
|
stdio: "inherit",
|
|
230
220
|
});
|
|
@@ -236,14 +226,59 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
|
|
|
236
226
|
var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
|
|
237
227
|
inquirer);
|
|
238
228
|
|
|
229
|
+
var parseYaml = function (content) {
|
|
230
|
+
try {
|
|
231
|
+
return yaml.load(content, { json: true });
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
return '';
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
var dumpYaml = function (content) {
|
|
238
|
+
try {
|
|
239
|
+
return yaml.dump(content, {
|
|
240
|
+
// 可选配置:保证中文等 Unicode 字符正常显示(不转义)
|
|
241
|
+
skipInvalid: true,
|
|
242
|
+
encoding: 'utf8',
|
|
243
|
+
// 可选:设置缩进空格数,增强可读性
|
|
244
|
+
indent: 2
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
catch (e) {
|
|
248
|
+
return '';
|
|
249
|
+
}
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
var writeChildWorkspace = function (content) {
|
|
253
|
+
var configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
|
|
254
|
+
var fileContent = parseYaml(uniReadFile(configPath));
|
|
255
|
+
if (!fileContent) {
|
|
256
|
+
// fileContent = "packages:\n";
|
|
257
|
+
fileContent = {
|
|
258
|
+
packages: [
|
|
259
|
+
'.',
|
|
260
|
+
]
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
var packages = new Set(__spreadArray(__spreadArray([], fileContent.packages, true), [content + '/*'], false));
|
|
264
|
+
fileContent.packages = Array.from(packages);
|
|
265
|
+
var str = dumpYaml(fileContent);
|
|
266
|
+
writeFiles(configPath, str);
|
|
267
|
+
};
|
|
239
268
|
var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
240
|
-
var mfeJson, apps, answers, selectedApps, selectedGitApps;
|
|
269
|
+
var mfeJson, apps, answers, selectedApps, childPath, selectedGitApps;
|
|
241
270
|
return __generator(this, function (_a) {
|
|
242
271
|
switch (_a.label) {
|
|
243
272
|
case 0:
|
|
244
273
|
mfeJson = getMfeJson();
|
|
245
274
|
apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
|
|
246
275
|
return [4 /*yield*/, inquirerPrompt.prompt([
|
|
276
|
+
{
|
|
277
|
+
type: 'input',
|
|
278
|
+
name: "dir",
|
|
279
|
+
message: "请输入子模块路径:",
|
|
280
|
+
default: PROJECT_GIT_PATH,
|
|
281
|
+
},
|
|
247
282
|
{
|
|
248
283
|
type: "checkbox",
|
|
249
284
|
name: "selectedApps",
|
|
@@ -257,19 +292,24 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
|
|
|
257
292
|
case 1:
|
|
258
293
|
answers = _a.sent();
|
|
259
294
|
selectedApps = answers.selectedApps;
|
|
295
|
+
childPath = answers.dir;
|
|
260
296
|
selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
|
|
261
|
-
console.log("选择的应用仓库:", selectedGitApps);
|
|
297
|
+
// console.log("选择的应用仓库:", selectedGitApps);
|
|
262
298
|
// 执行拉取操作
|
|
263
299
|
selectedGitApps.forEach(function (_a) {
|
|
264
300
|
var appCode = _a.appCode, repoUrl = _a.repoUrl;
|
|
265
301
|
if (repoUrl) {
|
|
266
302
|
var fileName = appCode.replace(/\//g, "_");
|
|
267
|
-
var dir =
|
|
303
|
+
var dir = childPath;
|
|
268
304
|
unlinkDeepDirOrFile(path.join(dir, fileName));
|
|
269
305
|
checkAndgenreDir(dir);
|
|
270
306
|
child_process.execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
|
|
271
307
|
stdio: "inherit",
|
|
272
308
|
});
|
|
309
|
+
writeChildWorkspace(dir);
|
|
310
|
+
// execSync(`cd ${dir} && git clone ${repoUrl} ${fileName}`, {
|
|
311
|
+
// stdio: "inherit",
|
|
312
|
+
// });
|
|
273
313
|
}
|
|
274
314
|
});
|
|
275
315
|
return [2 /*return*/];
|
|
@@ -278,20 +318,22 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
|
|
|
278
318
|
}); };
|
|
279
319
|
|
|
280
320
|
var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
|
|
281
|
-
var configPath;
|
|
282
321
|
return __generator(this, function (_a) {
|
|
283
|
-
configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
|
|
284
|
-
writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
|
|
285
322
|
// pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
|
|
286
323
|
child_process.execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
|
|
287
324
|
stdio: "inherit",
|
|
288
325
|
});
|
|
289
|
-
// 删除文件
|
|
290
|
-
fs.unlinkSync(configPath);
|
|
291
326
|
return [2 /*return*/];
|
|
292
327
|
});
|
|
293
328
|
}); };
|
|
294
329
|
|
|
330
|
+
/**
|
|
331
|
+
* CLI 命令
|
|
332
|
+
* - serve:开发态运行,支持 `-p` 平台、`--mode` 模式、`--b` 目标路径
|
|
333
|
+
* - build:生产构建,支持平台/模式;mp-weixin 下根据 isRoot/appCode 进行差异化参数
|
|
334
|
+
* - fetch:交互式拉取子应用仓库(选择 apps 列表并 clone 到本地)
|
|
335
|
+
* - runAll:并行启动所有 mp-weixin 项目
|
|
336
|
+
*/
|
|
295
337
|
var addUniOptions = function (program) {
|
|
296
338
|
return program
|
|
297
339
|
.option("-p <platform>", "平台", "h5")
|
|
@@ -319,11 +361,11 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
|
|
|
319
361
|
// pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
|
|
320
362
|
// pushDistToCdn(mode);
|
|
321
363
|
// });
|
|
322
|
-
addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
323
|
-
_b.mode; var platform = _b.p
|
|
364
|
+
addUniOptions(runAll).option("--cmd <execCmd>", "启动命令", "dev:mp-weixin").action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
365
|
+
_b.mode; var platform = _b.p, cmd = _b.cmd;
|
|
324
366
|
return __generator(this, function (_c) {
|
|
325
367
|
if (platform === 'mp-weixin') {
|
|
326
|
-
runParallelAllUni(
|
|
368
|
+
runParallelAllUni(cmd);
|
|
327
369
|
}
|
|
328
370
|
return [2 /*return*/];
|
|
329
371
|
});
|
package/dist/cli.mjs.js
CHANGED
|
@@ -7,6 +7,7 @@ import 'crypto';
|
|
|
7
7
|
import 'chokidar';
|
|
8
8
|
import { execSync } from 'child_process';
|
|
9
9
|
import inquirer from 'inquirer';
|
|
10
|
+
import yaml from 'js-yaml';
|
|
10
11
|
|
|
11
12
|
/******************************************************************************
|
|
12
13
|
Copyright (c) Microsoft Corporation.
|
|
@@ -63,6 +64,16 @@ function __generator(thisArg, body) {
|
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
|
|
67
|
+
function __spreadArray(to, from, pack) {
|
|
68
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
69
|
+
if (ar || !(i in from)) {
|
|
70
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
71
|
+
ar[i] = from[i];
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
75
|
+
}
|
|
76
|
+
|
|
66
77
|
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
67
78
|
var e = new Error(message);
|
|
68
79
|
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
@@ -98,6 +109,9 @@ function uniReadFile(filaPath) {
|
|
|
98
109
|
* @returns {void}
|
|
99
110
|
*/
|
|
100
111
|
function writeFiles(filaPath, content) {
|
|
112
|
+
if (typeof content === "object" && content) {
|
|
113
|
+
content = JSON.stringify(content, null, 2);
|
|
114
|
+
}
|
|
101
115
|
// 检查文件路径是否存在
|
|
102
116
|
var dir = path.dirname(filaPath);
|
|
103
117
|
if (!fs.existsSync(dir)) {
|
|
@@ -130,7 +144,7 @@ var unlinkDeepDirOrFile = function (filePath) {
|
|
|
130
144
|
};
|
|
131
145
|
|
|
132
146
|
// 项目 Git 子模块路径
|
|
133
|
-
var PROJECT_GIT_PATH = "
|
|
147
|
+
var PROJECT_GIT_PATH = "./sub-repo";
|
|
134
148
|
// CDN 文件保存路径
|
|
135
149
|
path.join(process.cwd(), "node_modules/@dd-code/uni-files");
|
|
136
150
|
path.join(process.cwd(), "node_modules/@dd-code/__main-pwd.txt");
|
|
@@ -199,30 +213,6 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
|
|
|
199
213
|
process.env.MFE_TARGET_DIR = (opt === null || opt === void 0 ? void 0 : opt.buildDir) || "";
|
|
200
214
|
_a = opt || {}, isRoot = _a.isRoot, appCode = _a.appCode;
|
|
201
215
|
cmd = "".concat(command, " ").concat(!isRoot ? "--subpackage=".concat(appCode) : "");
|
|
202
|
-
// if (isRoot) {
|
|
203
|
-
// const mfeJson = getMfeJson();
|
|
204
|
-
// const apps = mfeJson.apps?.map((i) => i.appCode) || [];
|
|
205
|
-
// apps.forEach((appCode) => {
|
|
206
|
-
// const appCodeDir = appCode.replace(/\//g, "_");
|
|
207
|
-
// // console.log(appCodeDir, 'appCodeDir');
|
|
208
|
-
// const childPath = path.join(process.cwd(), PROJECT_GIT_PATH, appCodeDir);
|
|
209
|
-
// console.log(existsSync(appCodeDir), childPath, "childPath");
|
|
210
|
-
// // if (existsSync(appCodeDir)) {
|
|
211
|
-
// // excuteUniCommand(`cd ${childPath} && ${command}`, {
|
|
212
|
-
// // appCode,
|
|
213
|
-
// // target: appCodeDir,
|
|
214
|
-
// // });
|
|
215
|
-
// // 开一个子线程启动
|
|
216
|
-
// // execSync(cmd, {
|
|
217
|
-
// // stdio: "inherit",
|
|
218
|
-
// // });
|
|
219
|
-
// // }
|
|
220
|
-
// });
|
|
221
|
-
// // setTimeout(() => {
|
|
222
|
-
// // console.log(process.env.UNI_OUTPUT_DIR, "process.env.UNI_OUTPUT_DIR");
|
|
223
|
-
// // });
|
|
224
|
-
// }
|
|
225
|
-
// console.log(cmd, "-------");
|
|
226
216
|
execSync(cmd, {
|
|
227
217
|
stdio: "inherit",
|
|
228
218
|
});
|
|
@@ -234,14 +224,59 @@ var excuteUniCommand = function (command, opt) { return __awaiter(void 0, void 0
|
|
|
234
224
|
var inquirerPrompt = ((inquirer === null || inquirer === void 0 ? void 0 : inquirer.default) ||
|
|
235
225
|
inquirer);
|
|
236
226
|
|
|
227
|
+
var parseYaml = function (content) {
|
|
228
|
+
try {
|
|
229
|
+
return yaml.load(content, { json: true });
|
|
230
|
+
}
|
|
231
|
+
catch (e) {
|
|
232
|
+
return '';
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
var dumpYaml = function (content) {
|
|
236
|
+
try {
|
|
237
|
+
return yaml.dump(content, {
|
|
238
|
+
// 可选配置:保证中文等 Unicode 字符正常显示(不转义)
|
|
239
|
+
skipInvalid: true,
|
|
240
|
+
encoding: 'utf8',
|
|
241
|
+
// 可选:设置缩进空格数,增强可读性
|
|
242
|
+
indent: 2
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
catch (e) {
|
|
246
|
+
return '';
|
|
247
|
+
}
|
|
248
|
+
};
|
|
249
|
+
|
|
250
|
+
var writeChildWorkspace = function (content) {
|
|
251
|
+
var configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
|
|
252
|
+
var fileContent = parseYaml(uniReadFile(configPath));
|
|
253
|
+
if (!fileContent) {
|
|
254
|
+
// fileContent = "packages:\n";
|
|
255
|
+
fileContent = {
|
|
256
|
+
packages: [
|
|
257
|
+
'.',
|
|
258
|
+
]
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
var packages = new Set(__spreadArray(__spreadArray([], fileContent.packages, true), [content + '/*'], false));
|
|
262
|
+
fileContent.packages = Array.from(packages);
|
|
263
|
+
var str = dumpYaml(fileContent);
|
|
264
|
+
writeFiles(configPath, str);
|
|
265
|
+
};
|
|
237
266
|
var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
238
|
-
var mfeJson, apps, answers, selectedApps, selectedGitApps;
|
|
267
|
+
var mfeJson, apps, answers, selectedApps, childPath, selectedGitApps;
|
|
239
268
|
return __generator(this, function (_a) {
|
|
240
269
|
switch (_a.label) {
|
|
241
270
|
case 0:
|
|
242
271
|
mfeJson = getMfeJson();
|
|
243
272
|
apps = (mfeJson === null || mfeJson === void 0 ? void 0 : mfeJson.apps) || [];
|
|
244
273
|
return [4 /*yield*/, inquirerPrompt.prompt([
|
|
274
|
+
{
|
|
275
|
+
type: 'input',
|
|
276
|
+
name: "dir",
|
|
277
|
+
message: "请输入子模块路径:",
|
|
278
|
+
default: PROJECT_GIT_PATH,
|
|
279
|
+
},
|
|
245
280
|
{
|
|
246
281
|
type: "checkbox",
|
|
247
282
|
name: "selectedApps",
|
|
@@ -255,19 +290,24 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
|
|
|
255
290
|
case 1:
|
|
256
291
|
answers = _a.sent();
|
|
257
292
|
selectedApps = answers.selectedApps;
|
|
293
|
+
childPath = answers.dir;
|
|
258
294
|
selectedGitApps = apps.filter(function (app) { return selectedApps.includes(app.appCode) && app.repoUrl; });
|
|
259
|
-
console.log("选择的应用仓库:", selectedGitApps);
|
|
295
|
+
// console.log("选择的应用仓库:", selectedGitApps);
|
|
260
296
|
// 执行拉取操作
|
|
261
297
|
selectedGitApps.forEach(function (_a) {
|
|
262
298
|
var appCode = _a.appCode, repoUrl = _a.repoUrl;
|
|
263
299
|
if (repoUrl) {
|
|
264
300
|
var fileName = appCode.replace(/\//g, "_");
|
|
265
|
-
var dir =
|
|
301
|
+
var dir = childPath;
|
|
266
302
|
unlinkDeepDirOrFile(path.join(dir, fileName));
|
|
267
303
|
checkAndgenreDir(dir);
|
|
268
304
|
execSync("cd ".concat(dir, " && git clone ").concat(repoUrl, " ").concat(fileName), {
|
|
269
305
|
stdio: "inherit",
|
|
270
306
|
});
|
|
307
|
+
writeChildWorkspace(dir);
|
|
308
|
+
// execSync(`cd ${dir} && git clone ${repoUrl} ${fileName}`, {
|
|
309
|
+
// stdio: "inherit",
|
|
310
|
+
// });
|
|
271
311
|
}
|
|
272
312
|
});
|
|
273
313
|
return [2 /*return*/];
|
|
@@ -276,20 +316,22 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
|
|
|
276
316
|
}); };
|
|
277
317
|
|
|
278
318
|
var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
|
|
279
|
-
var configPath;
|
|
280
319
|
return __generator(this, function (_a) {
|
|
281
|
-
configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
|
|
282
|
-
writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
|
|
283
320
|
// pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
|
|
284
321
|
execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
|
|
285
322
|
stdio: "inherit",
|
|
286
323
|
});
|
|
287
|
-
// 删除文件
|
|
288
|
-
fs.unlinkSync(configPath);
|
|
289
324
|
return [2 /*return*/];
|
|
290
325
|
});
|
|
291
326
|
}); };
|
|
292
327
|
|
|
328
|
+
/**
|
|
329
|
+
* CLI 命令
|
|
330
|
+
* - serve:开发态运行,支持 `-p` 平台、`--mode` 模式、`--b` 目标路径
|
|
331
|
+
* - build:生产构建,支持平台/模式;mp-weixin 下根据 isRoot/appCode 进行差异化参数
|
|
332
|
+
* - fetch:交互式拉取子应用仓库(选择 apps 列表并 clone 到本地)
|
|
333
|
+
* - runAll:并行启动所有 mp-weixin 项目
|
|
334
|
+
*/
|
|
293
335
|
var addUniOptions = function (program) {
|
|
294
336
|
return program
|
|
295
337
|
.option("-p <platform>", "平台", "h5")
|
|
@@ -317,11 +359,11 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
|
|
|
317
359
|
// pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
|
|
318
360
|
// pushDistToCdn(mode);
|
|
319
361
|
// });
|
|
320
|
-
addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
321
|
-
_b.mode; var platform = _b.p
|
|
362
|
+
addUniOptions(runAll).option("--cmd <execCmd>", "启动命令", "dev:mp-weixin").action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
|
|
363
|
+
_b.mode; var platform = _b.p, cmd = _b.cmd;
|
|
322
364
|
return __generator(this, function (_c) {
|
|
323
365
|
if (platform === 'mp-weixin') {
|
|
324
|
-
runParallelAllUni(
|
|
366
|
+
runParallelAllUni(cmd);
|
|
325
367
|
}
|
|
326
368
|
return [2 /*return*/];
|
|
327
369
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
interface IUniConfigOptions {
|
|
2
|
+
exposes?: Record<string, string>;
|
|
3
|
+
}
|
|
4
|
+
|
|
1
5
|
/**
|
|
2
6
|
* 创建 dd-code Uni 插件
|
|
3
7
|
* @description 根据配置文件和传入选项创建插件实例
|
|
@@ -9,6 +13,6 @@
|
|
|
9
13
|
* customOption: 'value'
|
|
10
14
|
* });
|
|
11
15
|
*/
|
|
12
|
-
declare const _default: (options?:
|
|
16
|
+
declare const _default: (options?: IUniConfigOptions) => any[];
|
|
13
17
|
|
|
14
18
|
export { _default as default };
|