@dd-code/uni-tools 1.0.10 → 1.0.12

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 ADDED
@@ -0,0 +1,129 @@
1
+ # @dd-code/uni-tools
2
+
3
+ `@dd-code/uni-tools` 是一个针对 UniApp 的微信小程序(mp-weixin)模块化解决方案与工具集,目标是让「主应用 + 多子应用(分包)」的开发、联调与构建更顺畅、更自动化。
4
+
5
+ ## 项目简介
6
+ - 面向微信小程序的模块化架构:主应用负责壳与公共能力,子应用以分包形式集成。
7
+ - 自动管理主/子应用的 `app.json`、`pages.json` 等配置合并,减少人工维护成本。
8
+ - 开发态具备文件监听、自动同步与应用间通信能力,提升联调效率。
9
+ - 提供统一 CLI 命令,简化拉取子应用仓库、开发、构建等流程。
10
+
11
+ ## 有哪些命令
12
+ - `npx uni-tools fetch`:交互式拉取已配置的子应用仓库到本地工作空间(packages/uni-tools/src/cli.ts:20)。
13
+ - `npx uni-tools serve -p mp-weixin --mode development`:启动开发态;自动识别主/子应用并启动对应插件逻辑(packages/uni-tools/src/cli.ts:14)。
14
+ - `npx uni-tools build -p mp-weixin --mode production`:执行生产构建,按模块化架构输出主应用及分包(packages/uni-tools/src/cli.ts:52)。
15
+ - 可选参数:
16
+ - `-p <platform>`:平台,默认 `h5`,支持 `mp-weixin`。
17
+ - `--mode <mode>`:模式,默认 `development`。
18
+ - `--b <buildDir>`:指定构建目标输出路径(部分场景有效)。
19
+
20
+ ## 是干嘛的 / 实现的效果是什么
21
+ - 统一管理主/子应用:自动收集各子应用的页面配置,合并生成主应用最终 `app.json`。
22
+ - 开发态联动:
23
+ - 子应用构建产物变更后,自动同步到主应用分包目录;
24
+ - 利用 WebSocket 在主子应用之间广播页面/静态资源变更,减少手动刷新与重复构建。
25
+ - 生产构建可控:按环境与模式输出稳定的主应用与分包结构,避免配置漂移与遗漏。
26
+ - 仓库管理简化:通过交互命令拉取/更新子应用仓库,快速搭建本地联调环境。
27
+
28
+ ## 大致的实现逻辑
29
+ - 插件入口与平台分发(packages/uni-tools/src/plugins/mp-weixin.ts:8-22)
30
+ - 检测当前平台是否为 `mp-weixin`,是则注册一组 Vite 插件:环境初始化、资源处理、Manifest 管理、主应用逻辑等。
31
+ - Manifest 管理与 `app.json` 合并(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:31-76, 105-119, 261-279)
32
+ - 读取各子应用的 `pages.json`,生成分包结构并合并到主应用的 `app.json`。
33
+ - 在 `closeBundle` 钩子中根据当前模式(开发/构建)与是否主应用,决定是否生成或更新最终的 `app.json`。
34
+ - 开发态联调(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-156, 187-245)
35
+ - 主应用启动 WebSocket 服务,子应用作为客户端连接;
36
+ - 监听子应用构建输出目录变更(`DistWatcher`),变更时拷贝至主应用对应分包路径,并通过 WS 通知;
37
+ - 收到子应用变更通知后,主应用实时增量更新 `app.json` 中的对应分包页面配置。
38
+ - 交互式拉取子应用仓库(packages/uni-tools/src/plugins/modules/mp-weixin/gitlib/index.ts)
39
+ - 使用交互框选择需要拉取的应用,自动将其下载/更新至本地指定目录,便于快速联调。
40
+
41
+ ## 配置与环境变量
42
+ - `mfe.json`(项目根目录,packages/uni-tools/src/config/const.ts:1)
43
+ ```json
44
+ {
45
+ "platform": "mp-weixin",
46
+ "apps": [
47
+ { "appCode": "modules/manage", "repoUrl": "git@xxx:manage.git" },
48
+ { "appCode": "modules/bwzb", "repoUrl": "git@xxx:bwzb.git" },
49
+ { "appCode": "login" }
50
+ ]
51
+ }
52
+ ```
53
+ - `platform`:目标平台;插件入口会根据此值启用对应模块(packages/uni-tools/src/plugins/mp-weixin.ts:16-20)。
54
+ - `apps`:主应用下要集成的子应用分包列表;主应用读取并参与合并(packages/uni-tools/src/plugins/modules/mp-weixin/manifest-core.ts:75-87)。
55
+ - `.env.*` 中的 `MFE_` 前缀变量(packages/uni-tools/src/utils/utils.ts:93-97)
56
+ - `MFE_UNI_IS_ROOT=true|false`:是否主应用(影响 `appCode` 填充与插件行为)(packages/uni-tools/src/config/config.ts:130-141)。
57
+ - `MFE_UNI_CODE=your-project-code`:项目代码,用于 CDN 路径拼接与 Manifest 标识(packages/uni-tools/src/config/config.ts:90-105, 116-141)。
58
+ - `MFE_APP_CODE=__MFE_APP_ROOT__|modules/manage`:当前应用的 `appCode`。
59
+ - `MFE_UNI_SERVE=true|false`:开发态是否启用联调(WS/监听)(packages/uni-tools/src/plugins/modules/mp-weixin/manifest-core.ts:75-87)。
60
+ - `MFE_CDN_HOST=https://static.example.com`:CDN 域名,供 Manifest 资源定位(packages/uni-tools/src/config/config.ts:116-141)。
61
+ - `UNI_PLATFORM=mp-weixin`:平台选择(packages/uni-tools/src/config/config.ts:103-105)。
62
+ - 运行时自动设置的变量(无需手动维护)
63
+ - `MFE_SOURCE_OUTPUT_DIR`:当前应用构建输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:64)。
64
+ - `MFE_ROOT_OUTPUT_DIR`:主应用根输出目录(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:65-69)。
65
+ - `UNI_OUTPUT_DIR`:根输出目录,生成/读取 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:72-76)。
66
+ - `MFE_INNER_BUILD=true`:子应用处于“内部构建”模式(packages/uni-tools/src/utils/utils.ts:204)。
67
+ - `MFE_TARGET_DIR=root`:由 `--b root` 触发,用于判断内部构建(packages/uni-tools/src/utils/utils.ts:202)。
68
+
69
+ ## 快速开始
70
+ 1. 安装依赖:
71
+ - 使用你的包管理器安装 `@dd-code/uni-tools`。
72
+ 2. 在 `vite.config.ts` 中启用插件:
73
+ ```ts
74
+ import { defineConfig } from 'vite';
75
+ import uni from '@dcloudio/vite-plugin-uni';
76
+ import uniTools from '@dd-code/uni-tools';
77
+
78
+ export default defineConfig({
79
+ plugins: [
80
+ uni(),
81
+ uniTools()
82
+ ],
83
+ });
84
+ ```
85
+ 3. 拉取子应用仓库:`npx uni-tools fetch`
86
+ 4. 启动开发:`npx uni-tools serve -p mp-weixin --mode development`
87
+ 5. 构建生产:`npx uni-tools build -p mp-weixin --mode production`
88
+
89
+ ## 开发流程(主/子应用)
90
+ - 主应用开发
91
+ - `.env.development` 示例:
92
+ ```
93
+ MFE_UNI_IS_ROOT=true
94
+ MFE_UNI_CODE=your-project-code
95
+ MFE_UNI_SERVE=true
96
+ UNI_PLATFORM=mp-weixin
97
+ ```
98
+ - 启动:`npx uni-tools serve -p mp-weixin --mode development`
99
+ - 行为:开启 WS 服务与合并逻辑,动态维护最终 `app.json`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:121-143, 261-279)。
100
+ - 子应用开发(联调)
101
+ - `.env.development` 示例:
102
+ ```
103
+ MFE_UNI_IS_ROOT=false
104
+ MFE_UNI_CODE=your-project-code
105
+ MFE_APP_CODE=modules/manage
106
+ MFE_UNI_SERVE=true
107
+ UNI_PLATFORM=mp-weixin
108
+ ```
109
+ - 启动:`npx uni-tools serve -p mp-weixin --mode development`
110
+ - 行为:作为 WS 客户端与主应用通信,监听自身输出变更并同步到主应用分包路径(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:145-156, 78-103)。
111
+ - 子应用构建(不联调,直接写入主应用)
112
+ - 前置:主应用开发态启动但 `MFE_UNI_SERVE=false`,提供 HTTP 接口返回主应用 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:190-200)。
113
+ - 子应用构建命令:`npx uni-tools build -p mp-weixin --mode development --b root`
114
+ - 行为:自动把子应用 `outDir` 改写到主应用 `UNI_OUTPUT_DIR/<appCode>`,并设置内部构建环境(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59, 64-76)。
115
+
116
+ ## 目录与约定
117
+ - 主应用:负责最终 `app.json` 合并输出。
118
+ - 子应用:分包形式接入,拥有独立 `pages.json` 与资源;构建输出由插件监听并同步。
119
+ - Manifest 与分包配置生成:读取各子应用 `pages.json` 并渲染至主应用 `subPackages`(packages/uni-tools/src/plugins/modules/mp-weixin/plugins/main-app.ts:41-69)。
120
+
121
+ ## 适用场景
122
+ - 同一个小程序中承载多个业务模块/团队的功能分包,需要高效联调与统一发布。
123
+ - 希望减少主/子应用配置的重复劳动与易错区域。
124
+ - 多仓库协作,希望有一套拉取、开发、构建的统一流程。
125
+
126
+ ## 注意
127
+ - 开发态会根据 `mode`、`isRoot` 等状态决定是否启动 WS 服务与目录监听。
128
+ - 请确保各子应用的 `pages.json` 合法且页面路径正确,便于自动合并。
129
+ - 使用 `--b root` 时需先启动主应用的 HTTP 服务,以便子应用定位 `UNI_OUTPUT_DIR`(packages/uni-tools/src/plugins/modules/mp-weixin/output.ts:39-59)。
package/dist/cli.js CHANGED
@@ -91,6 +91,22 @@ function uniReadFile(filaPath) {
91
91
  }
92
92
  return "";
93
93
  }
94
+ /**
95
+ * 写入文件内容
96
+ * 自动创建目录结构,支持对象和字符串内容
97
+ * @param {string} filaPath - 文件路径
98
+ * @param {string|Object} content - 文件内容
99
+ * @returns {void}
100
+ */
101
+ function writeFiles(filaPath, content) {
102
+ // 检查文件路径是否存在
103
+ var dir = path.dirname(filaPath);
104
+ if (!fs.existsSync(dir)) {
105
+ // 如果路径不存在,创建路径
106
+ fs.mkdirSync(dir, { recursive: true });
107
+ }
108
+ return fs.writeFileSync(filaPath, content, "utf-8");
109
+ }
94
110
  var checkAndgenreDir = function (dir) {
95
111
  if (!fs.existsSync(dir)) {
96
112
  // 如果路径不存在,创建路径
@@ -248,6 +264,21 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
248
264
  });
249
265
  }); };
250
266
 
267
+ var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
268
+ var configPath;
269
+ return __generator(this, function (_a) {
270
+ configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
271
+ writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
272
+ // pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
273
+ child_process.execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
274
+ stdio: "inherit",
275
+ });
276
+ // 删除文件
277
+ fs.unlinkSync(configPath);
278
+ return [2 /*return*/];
279
+ });
280
+ }); };
281
+
251
282
  var addUniOptions = function (program) {
252
283
  return program
253
284
  .option("-p <platform>", "平台", "h5")
@@ -261,6 +292,7 @@ var dev = shared.program
261
292
  var build = shared.program.command("build").description("构建 uni 项目");
262
293
  // const pushCdn = program.command("push-cdn").description("推送 uni 项目到 cdn");
263
294
  var fetchGit = shared.program.command("fetch").description("拉取 uni 项目到本地");
295
+ var runAll = shared.program.command("runAll").description("运行所有 uni 项目");
264
296
  fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function () {
265
297
  return __generator(this, function (_a) {
266
298
  switch (_a.label) {
@@ -274,6 +306,15 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
274
306
  // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
275
307
  // pushDistToCdn(mode);
276
308
  // });
309
+ addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
310
+ _b.mode; var platform = _b.p; _b.b;
311
+ return __generator(this, function (_c) {
312
+ if (platform === 'mp-weixin') {
313
+ runParallelAllUni("dev:mp-weixin");
314
+ }
315
+ return [2 /*return*/];
316
+ });
317
+ }); });
277
318
  addUniOptions(dev).action(function (_a) {
278
319
  var mode = _a.mode, platform = _a.p, b = _a.b;
279
320
  process.env.MFE_BUILD_MODE = EBuildMode.SERVE;
package/dist/cli.mjs.js CHANGED
@@ -89,6 +89,22 @@ function uniReadFile(filaPath) {
89
89
  }
90
90
  return "";
91
91
  }
92
+ /**
93
+ * 写入文件内容
94
+ * 自动创建目录结构,支持对象和字符串内容
95
+ * @param {string} filaPath - 文件路径
96
+ * @param {string|Object} content - 文件内容
97
+ * @returns {void}
98
+ */
99
+ function writeFiles(filaPath, content) {
100
+ // 检查文件路径是否存在
101
+ var dir = path.dirname(filaPath);
102
+ if (!fs.existsSync(dir)) {
103
+ // 如果路径不存在,创建路径
104
+ fs.mkdirSync(dir, { recursive: true });
105
+ }
106
+ return fs.writeFileSync(filaPath, content, "utf-8");
107
+ }
92
108
  var checkAndgenreDir = function (dir) {
93
109
  if (!fs.existsSync(dir)) {
94
110
  // 如果路径不存在,创建路径
@@ -246,6 +262,21 @@ var fetchAppsRepo = function () { return __awaiter(void 0, void 0, void 0, funct
246
262
  });
247
263
  }); };
248
264
 
265
+ var runParallelAllUni = function (cmd) { return __awaiter(void 0, void 0, void 0, function () {
266
+ var configPath;
267
+ return __generator(this, function (_a) {
268
+ configPath = path.join(process.cwd(), "pnpm-workspace.yaml");
269
+ writeFiles(configPath, "packages:\n - '*'\n - 'src/subtree/*'");
270
+ // pnpm -r --parallel --workspace-root --filter=* run dev:mp-weixin --b root
271
+ execSync("pnpm -r --parallel --workspace-root --filter=* run ".concat(cmd, " --b root"), {
272
+ stdio: "inherit",
273
+ });
274
+ // 删除文件
275
+ fs.unlinkSync(configPath);
276
+ return [2 /*return*/];
277
+ });
278
+ }); };
279
+
249
280
  var addUniOptions = function (program) {
250
281
  return program
251
282
  .option("-p <platform>", "平台", "h5")
@@ -259,6 +290,7 @@ var dev = program
259
290
  var build = program.command("build").description("构建 uni 项目");
260
291
  // const pushCdn = program.command("push-cdn").description("推送 uni 项目到 cdn");
261
292
  var fetchGit = program.command("fetch").description("拉取 uni 项目到本地");
293
+ var runAll = program.command("runAll").description("运行所有 uni 项目");
262
294
  fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function () {
263
295
  return __generator(this, function (_a) {
264
296
  switch (_a.label) {
@@ -272,6 +304,15 @@ fetchGit.action(function () { return __awaiter(void 0, void 0, void 0, function
272
304
  // pushCdn.option("--mode <mode>", "模式", "dev").action(({ mode }) => {
273
305
  // pushDistToCdn(mode);
274
306
  // });
307
+ addUniOptions(runAll).action(function (_a) { return __awaiter(void 0, [_a], void 0, function (_b) {
308
+ _b.mode; var platform = _b.p; _b.b;
309
+ return __generator(this, function (_c) {
310
+ if (platform === 'mp-weixin') {
311
+ runParallelAllUni("dev:mp-weixin");
312
+ }
313
+ return [2 /*return*/];
314
+ });
315
+ }); });
275
316
  addUniOptions(dev).action(function (_a) {
276
317
  var mode = _a.mode, platform = _a.p, b = _a.b;
277
318
  process.env.MFE_BUILD_MODE = EBuildMode.SERVE;
package/dist/index.js CHANGED
@@ -262,6 +262,8 @@ var createFileWatcher = function (filePath, opt) {
262
262
  var watcher = chokidar.watch(filePath, __assign({ persistent: true, ignoreInitial: true, usePolling: true, interval: 100, awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 100 }, followSymlinks: true, depth: 99 }, opt));
263
263
  return watcher;
264
264
  };
265
+ var checkIsBuildInChild = function () { return process.env.MFE_TARGET_DIR === "root"; };
266
+ var checkIsInnerBuild = function () { return process.env.MFE_INNER_BUILD === "true"; };
265
267
 
266
268
  var UniCdnManager = /** @class */ (function () {
267
269
  function UniCdnManager() {
@@ -723,6 +725,7 @@ var createManifestManager = function () {
723
725
  };
724
726
  };
725
727
 
728
+ var num = 5;
726
729
  var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, function () {
727
730
  var URL, mainProcess, result;
728
731
  return __generator(this, function (_a) {
@@ -755,26 +758,37 @@ var getMainProcessLoop = function () { return __awaiter(void 0, void 0, void 0,
755
758
  return [3 /*break*/, 3];
756
759
  case 3: return [2 /*return*/, new Promise(function (resolve) {
757
760
  setTimeout(function () {
761
+ num--;
762
+ if (num <= 0) {
763
+ resolve(null);
764
+ num = 5;
765
+ }
758
766
  resolve(getMainProcessLoop());
759
- }, 1000);
767
+ }, 100);
760
768
  })];
761
769
  }
762
770
  });
763
771
  }); };
764
772
  var resetOutDir = function (currentManifestJson, config) { return __awaiter(void 0, void 0, void 0, function () {
765
- var childBuild, currentManifest, mainProcess, exp;
773
+ var currentManifest, mainProcess, exp;
766
774
  var _a;
767
775
  return __generator(this, function (_b) {
768
776
  switch (_b.label) {
769
777
  case 0:
770
- childBuild = process.env.MFE_TARGET_DIR === "root";
771
778
  currentManifest = currentManifestJson.value;
772
- if (!(!currentManifest.isRoot && childBuild && !currentManifest.isServe)) return [3 /*break*/, 2];
779
+ if (!(!currentManifest.isRoot &&
780
+ checkIsBuildInChild() &&
781
+ !currentManifest.isServe)) return [3 /*break*/, 2];
773
782
  return [4 /*yield*/, getMainProcessLoop()];
774
783
  case 1:
775
784
  mainProcess = _b.sent();
776
- // mainProcess.UNI_OUTPUT_DIR
777
- config.build.outDir = "".concat(mainProcess.UNI_OUTPUT_DIR, "/").concat(currentManifest.appCode);
785
+ if (mainProcess) {
786
+ config.build.outDir = "".concat(mainProcess.UNI_OUTPUT_DIR, "/").concat(currentManifest.appCode);
787
+ process.env.MFE_INNER_BUILD = "true";
788
+ }
789
+ else {
790
+ process.env.MFE_TARGET_DIR = "";
791
+ }
778
792
  _b.label = 2;
779
793
  case 2:
780
794
  exp = new RegExp("/(".concat(currentManifest.appCode, ")/?"));
@@ -783,7 +797,12 @@ var resetOutDir = function (currentManifestJson, config) { return __awaiter(void
783
797
  process.env.MFE_SOURCE_OUTPUT_DIR = config.build.outDir;
784
798
  process.env.MFE_ROOT_OUTPUT_DIR = (_a = process.env.MFE_SOURCE_OUTPUT_DIR) === null || _a === void 0 ? void 0 : _a.replace(exp, "/").replace(/\/$/, "");
785
799
  // if (!currentManifestJson.value.isRoot)
786
- process.env.UNI_OUTPUT_DIR = process.env.MFE_ROOT_OUTPUT_DIR;
800
+ // console.log(config);
801
+ if (!checkIsInnerBuild()) {
802
+ // setTimeout(() => {
803
+ process.env.UNI_OUTPUT_DIR = process.env.MFE_ROOT_OUTPUT_DIR;
804
+ // }, 500);
805
+ }
787
806
  return [2 /*return*/];
788
807
  }
789
808
  });
@@ -871,6 +890,8 @@ var createAppsAssetsPlugin = function (manifestJson) {
871
890
  buildStart: function () {
872
891
  if (isMoved)
873
892
  return;
893
+ if (checkIsBuildInChild() && !manifestJson.value.isRoot)
894
+ return;
874
895
  var basePath = path.resolve(SAVE_CDN_FILE_PATH, manifestJson.value.mode || "dev");
875
896
  moveOtherApps({
876
897
  base: basePath,
@@ -1055,7 +1076,6 @@ var createManifestPlugin = function (manifestJson) {
1055
1076
  var collectFiles = new CollectFiles();
1056
1077
  return {
1057
1078
  name: "@dd-code:genre-mainfest-file-list",
1058
- enforce: "post",
1059
1079
  config: function (config) {
1060
1080
  var _a;
1061
1081
  var outDir = ((_a = config.build) === null || _a === void 0 ? void 0 : _a.outDir) || "dist";
@@ -1087,9 +1107,9 @@ var createManifestPlugin = function (manifestJson) {
1087
1107
  manifestJson.saveFile(filePath);
1088
1108
  // console.log({ filePath }, "sourcePath");
1089
1109
  // addRunningAppToSave(manifestJson.value.appCode, filePath);
1090
- setTimeout(function () {
1091
- collectFiles.copyFilesToPublishDir();
1092
- }, 0);
1110
+ // setTimeout(() => {
1111
+ collectFiles.copyFilesToPublishDir();
1112
+ // }, 0);
1093
1113
  },
1094
1114
  };
1095
1115
  };
@@ -1406,6 +1426,7 @@ var createMainAppPlugin = function (manifestJson) {
1406
1426
  var fn = null;
1407
1427
  var isBuild = function () { return process.env.MFE_BUILD_MODE === EBuildMode.BUILD; };
1408
1428
  var isServe = function () { return manifestJson.value.isServe; };
1429
+ var isRoot = function () { return manifestJson.value.isRoot; };
1409
1430
  var isStartServer = function () {
1410
1431
  if (isBuild())
1411
1432
  return false;
@@ -1430,14 +1451,16 @@ var createMainAppPlugin = function (manifestJson) {
1430
1451
  var isServe, _a, start;
1431
1452
  return __generator(this, function (_b) {
1432
1453
  isServe = isStartServer();
1454
+ if (isBuild())
1455
+ return [2 /*return*/];
1433
1456
  if (!isServe) {
1434
- if (!isBuild() && manifestJson.value.isRoot) {
1457
+ if (!isBuild() && isRoot()) {
1435
1458
  _a = createHttpServer(), _a.server, start = _a.start;
1436
1459
  start();
1437
1460
  }
1438
1461
  return [2 /*return*/];
1439
1462
  }
1440
- if (manifestJson.value.isRoot) {
1463
+ if (isRoot()) {
1441
1464
  createMainAppServer();
1442
1465
  }
1443
1466
  else {
@@ -1453,7 +1476,7 @@ var createMainAppPlugin = function (manifestJson) {
1453
1476
  var isServe = isStartServer();
1454
1477
  if (!isServe)
1455
1478
  return;
1456
- if (!manifestJson.value.isRoot) {
1479
+ if (!isRoot()) {
1457
1480
  serverPlugin.initWatchChange();
1458
1481
  fn === null || fn === void 0 ? void 0 : fn();
1459
1482
  fn = null;
@@ -1493,6 +1516,8 @@ var createMainAppPlugin = function (manifestJson) {
1493
1516
  return [2 /*return*/];
1494
1517
  if (isServe())
1495
1518
  return [2 /*return*/];
1519
+ if (!checkIsBuildInChild())
1520
+ return [2 /*return*/];
1496
1521
  // if (!isServe) return;
1497
1522
  watchFile === null || watchFile === void 0 ? void 0 : watchFile();
1498
1523
  watchFile = null;
@@ -1511,7 +1536,10 @@ var createMainAppPlugin = function (manifestJson) {
1511
1536
  _a = filterManifestJsonListAndMainPageJson(__spreadArray(__spreadArray([], manifestJson.dependencies, true), [
1512
1537
  manifestJson.value,
1513
1538
  ], false)), outputPageJsonPath = _a.outputPageJsonPath, manifestList = _a.manifestList;
1514
- mainAppJsonPath = !manifestJson.value.isRoot
1539
+ if (!isRoot() && !isServe() && checkIsBuildInChild()) {
1540
+ return [2 /*return*/];
1541
+ }
1542
+ mainAppJsonPath = !isRoot()
1515
1543
  ? getNodeModuleMainAppJSon(manifestJson.value.mode, ROOT_APP_CODE)
1516
1544
  : getMainAppJsonPath();
1517
1545
  mainAppJson = uniReadFile(mainAppJsonPath);
package/dist/index.mjs.js CHANGED
@@ -260,6 +260,8 @@ var createFileWatcher = function (filePath, opt) {
260
260
  var watcher = chokidar.watch(filePath, __assign({ persistent: true, ignoreInitial: true, usePolling: true, interval: 100, awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 100 }, followSymlinks: true, depth: 99 }, opt));
261
261
  return watcher;
262
262
  };
263
+ var checkIsBuildInChild = function () { return process.env.MFE_TARGET_DIR === "root"; };
264
+ var checkIsInnerBuild = function () { return process.env.MFE_INNER_BUILD === "true"; };
263
265
 
264
266
  var UniCdnManager = /** @class */ (function () {
265
267
  function UniCdnManager() {
@@ -721,6 +723,7 @@ var createManifestManager = function () {
721
723
  };
722
724
  };
723
725
 
726
+ var num = 5;
724
727
  var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, function () {
725
728
  var URL, mainProcess, result;
726
729
  return __generator(this, function (_a) {
@@ -753,26 +756,37 @@ var getMainProcessLoop = function () { return __awaiter(void 0, void 0, void 0,
753
756
  return [3 /*break*/, 3];
754
757
  case 3: return [2 /*return*/, new Promise(function (resolve) {
755
758
  setTimeout(function () {
759
+ num--;
760
+ if (num <= 0) {
761
+ resolve(null);
762
+ num = 5;
763
+ }
756
764
  resolve(getMainProcessLoop());
757
- }, 1000);
765
+ }, 100);
758
766
  })];
759
767
  }
760
768
  });
761
769
  }); };
762
770
  var resetOutDir = function (currentManifestJson, config) { return __awaiter(void 0, void 0, void 0, function () {
763
- var childBuild, currentManifest, mainProcess, exp;
771
+ var currentManifest, mainProcess, exp;
764
772
  var _a;
765
773
  return __generator(this, function (_b) {
766
774
  switch (_b.label) {
767
775
  case 0:
768
- childBuild = process.env.MFE_TARGET_DIR === "root";
769
776
  currentManifest = currentManifestJson.value;
770
- if (!(!currentManifest.isRoot && childBuild && !currentManifest.isServe)) return [3 /*break*/, 2];
777
+ if (!(!currentManifest.isRoot &&
778
+ checkIsBuildInChild() &&
779
+ !currentManifest.isServe)) return [3 /*break*/, 2];
771
780
  return [4 /*yield*/, getMainProcessLoop()];
772
781
  case 1:
773
782
  mainProcess = _b.sent();
774
- // mainProcess.UNI_OUTPUT_DIR
775
- config.build.outDir = "".concat(mainProcess.UNI_OUTPUT_DIR, "/").concat(currentManifest.appCode);
783
+ if (mainProcess) {
784
+ config.build.outDir = "".concat(mainProcess.UNI_OUTPUT_DIR, "/").concat(currentManifest.appCode);
785
+ process.env.MFE_INNER_BUILD = "true";
786
+ }
787
+ else {
788
+ process.env.MFE_TARGET_DIR = "";
789
+ }
776
790
  _b.label = 2;
777
791
  case 2:
778
792
  exp = new RegExp("/(".concat(currentManifest.appCode, ")/?"));
@@ -781,7 +795,12 @@ var resetOutDir = function (currentManifestJson, config) { return __awaiter(void
781
795
  process.env.MFE_SOURCE_OUTPUT_DIR = config.build.outDir;
782
796
  process.env.MFE_ROOT_OUTPUT_DIR = (_a = process.env.MFE_SOURCE_OUTPUT_DIR) === null || _a === void 0 ? void 0 : _a.replace(exp, "/").replace(/\/$/, "");
783
797
  // if (!currentManifestJson.value.isRoot)
784
- process.env.UNI_OUTPUT_DIR = process.env.MFE_ROOT_OUTPUT_DIR;
798
+ // console.log(config);
799
+ if (!checkIsInnerBuild()) {
800
+ // setTimeout(() => {
801
+ process.env.UNI_OUTPUT_DIR = process.env.MFE_ROOT_OUTPUT_DIR;
802
+ // }, 500);
803
+ }
785
804
  return [2 /*return*/];
786
805
  }
787
806
  });
@@ -869,6 +888,8 @@ var createAppsAssetsPlugin = function (manifestJson) {
869
888
  buildStart: function () {
870
889
  if (isMoved)
871
890
  return;
891
+ if (checkIsBuildInChild() && !manifestJson.value.isRoot)
892
+ return;
872
893
  var basePath = path.resolve(SAVE_CDN_FILE_PATH, manifestJson.value.mode || "dev");
873
894
  moveOtherApps({
874
895
  base: basePath,
@@ -1053,7 +1074,6 @@ var createManifestPlugin = function (manifestJson) {
1053
1074
  var collectFiles = new CollectFiles();
1054
1075
  return {
1055
1076
  name: "@dd-code:genre-mainfest-file-list",
1056
- enforce: "post",
1057
1077
  config: function (config) {
1058
1078
  var _a;
1059
1079
  var outDir = ((_a = config.build) === null || _a === void 0 ? void 0 : _a.outDir) || "dist";
@@ -1085,9 +1105,9 @@ var createManifestPlugin = function (manifestJson) {
1085
1105
  manifestJson.saveFile(filePath);
1086
1106
  // console.log({ filePath }, "sourcePath");
1087
1107
  // addRunningAppToSave(manifestJson.value.appCode, filePath);
1088
- setTimeout(function () {
1089
- collectFiles.copyFilesToPublishDir();
1090
- }, 0);
1108
+ // setTimeout(() => {
1109
+ collectFiles.copyFilesToPublishDir();
1110
+ // }, 0);
1091
1111
  },
1092
1112
  };
1093
1113
  };
@@ -1404,6 +1424,7 @@ var createMainAppPlugin = function (manifestJson) {
1404
1424
  var fn = null;
1405
1425
  var isBuild = function () { return process.env.MFE_BUILD_MODE === EBuildMode.BUILD; };
1406
1426
  var isServe = function () { return manifestJson.value.isServe; };
1427
+ var isRoot = function () { return manifestJson.value.isRoot; };
1407
1428
  var isStartServer = function () {
1408
1429
  if (isBuild())
1409
1430
  return false;
@@ -1428,14 +1449,16 @@ var createMainAppPlugin = function (manifestJson) {
1428
1449
  var isServe, _a, start;
1429
1450
  return __generator(this, function (_b) {
1430
1451
  isServe = isStartServer();
1452
+ if (isBuild())
1453
+ return [2 /*return*/];
1431
1454
  if (!isServe) {
1432
- if (!isBuild() && manifestJson.value.isRoot) {
1455
+ if (!isBuild() && isRoot()) {
1433
1456
  _a = createHttpServer(), _a.server, start = _a.start;
1434
1457
  start();
1435
1458
  }
1436
1459
  return [2 /*return*/];
1437
1460
  }
1438
- if (manifestJson.value.isRoot) {
1461
+ if (isRoot()) {
1439
1462
  createMainAppServer();
1440
1463
  }
1441
1464
  else {
@@ -1451,7 +1474,7 @@ var createMainAppPlugin = function (manifestJson) {
1451
1474
  var isServe = isStartServer();
1452
1475
  if (!isServe)
1453
1476
  return;
1454
- if (!manifestJson.value.isRoot) {
1477
+ if (!isRoot()) {
1455
1478
  serverPlugin.initWatchChange();
1456
1479
  fn === null || fn === void 0 ? void 0 : fn();
1457
1480
  fn = null;
@@ -1491,6 +1514,8 @@ var createMainAppPlugin = function (manifestJson) {
1491
1514
  return [2 /*return*/];
1492
1515
  if (isServe())
1493
1516
  return [2 /*return*/];
1517
+ if (!checkIsBuildInChild())
1518
+ return [2 /*return*/];
1494
1519
  // if (!isServe) return;
1495
1520
  watchFile === null || watchFile === void 0 ? void 0 : watchFile();
1496
1521
  watchFile = null;
@@ -1509,7 +1534,10 @@ var createMainAppPlugin = function (manifestJson) {
1509
1534
  _a = filterManifestJsonListAndMainPageJson(__spreadArray(__spreadArray([], manifestJson.dependencies, true), [
1510
1535
  manifestJson.value,
1511
1536
  ], false)), outputPageJsonPath = _a.outputPageJsonPath, manifestList = _a.manifestList;
1512
- mainAppJsonPath = !manifestJson.value.isRoot
1537
+ if (!isRoot() && !isServe() && checkIsBuildInChild()) {
1538
+ return [2 /*return*/];
1539
+ }
1540
+ mainAppJsonPath = !isRoot()
1513
1541
  ? getNodeModuleMainAppJSon(manifestJson.value.mode, ROOT_APP_CODE)
1514
1542
  : getMainAppJsonPath();
1515
1543
  mainAppJson = uniReadFile(mainAppJsonPath);
package/package.json CHANGED
@@ -1,12 +1,13 @@
1
1
  {
2
2
  "name": "@dd-code/uni-tools",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "Universal tools for code utilities",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
7
7
  "types": "./dist/index.d.ts",
8
8
  "files": [
9
- "dist"
9
+ "dist",
10
+ "README.md"
10
11
  ],
11
12
  "bin": {
12
13
  "@dd-code/uni-tools": "dist/cli.js"