@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/dist/index.js
CHANGED
|
@@ -275,6 +275,11 @@ var unlinkDeepDirOrFile = function (filePath) {
|
|
|
275
275
|
console.error("删除文件夹失败:", err);
|
|
276
276
|
}
|
|
277
277
|
};
|
|
278
|
+
var getFilePathWithoutExt = function (filePath) {
|
|
279
|
+
var dirName = path.dirname(filePath);
|
|
280
|
+
var baseName = path.basename(filePath, path.extname(filePath));
|
|
281
|
+
return path.join(dirName, baseName);
|
|
282
|
+
};
|
|
278
283
|
|
|
279
284
|
var UniCdnManager = /** @class */ (function () {
|
|
280
285
|
function UniCdnManager() {
|
|
@@ -624,6 +629,11 @@ var downloadProjectFiles = function (manifestList) { return __awaiter(void 0, vo
|
|
|
624
629
|
});
|
|
625
630
|
}); };
|
|
626
631
|
|
|
632
|
+
/**
|
|
633
|
+
* 计算需要下载/更新的 Manifest 列表
|
|
634
|
+
* - 比较 node_modules 缓存中的 manifest 哈希与远端 manifest 哈希
|
|
635
|
+
* - 返回需要更新的清单(用于后续下载项目文件到本地缓存)
|
|
636
|
+
*/
|
|
627
637
|
var checkDownloadFilesIsExpired = function (manifestList) {
|
|
628
638
|
var filterList = manifestList.filter(function (conf) {
|
|
629
639
|
var targetPath = getNodeModulesEnvAppCodeFilePath(conf, MANIFEST_NAME);
|
|
@@ -633,6 +643,14 @@ var checkDownloadFilesIsExpired = function (manifestList) {
|
|
|
633
643
|
});
|
|
634
644
|
return filterList;
|
|
635
645
|
};
|
|
646
|
+
/**
|
|
647
|
+
* Manifest 管理器(构建态/运行态共享状态)
|
|
648
|
+
* - value:当前应用 Manifest(会随构建过程逐步填充)
|
|
649
|
+
* - setEnv:读取 cli/vite 环境,设置 isRoot/isServe/code/appCode/publicPath 等
|
|
650
|
+
* - setPagesJson/setFiles/setExposes:在插件流程中填充 pages 与产物清单与运行时暴露
|
|
651
|
+
* - saveFile:落盘 Manifest(计算 hash,剔除临时字段 isServe)
|
|
652
|
+
* - dependencies:记录其他应用的 Manifest(用于主应用合并)
|
|
653
|
+
*/
|
|
636
654
|
var createManifestManager = function () {
|
|
637
655
|
var envObj = process.env;
|
|
638
656
|
var row = {
|
|
@@ -661,6 +679,9 @@ var createManifestManager = function () {
|
|
|
661
679
|
newManifest.hash = generateSHA256(JSON.stringify(__assign({}, newManifest)));
|
|
662
680
|
writeFiles(filePath, JSON.stringify(newManifest, null, 2));
|
|
663
681
|
},
|
|
682
|
+
setExposes: function (exposes) {
|
|
683
|
+
row.exposes = exposes || {};
|
|
684
|
+
},
|
|
664
685
|
setEnv: function (mode) {
|
|
665
686
|
var env = formatCliCommandConfig(mode);
|
|
666
687
|
var mfeJson = getMfeJson();
|
|
@@ -682,8 +703,93 @@ var createManifestManager = function () {
|
|
|
682
703
|
},
|
|
683
704
|
};
|
|
684
705
|
};
|
|
706
|
+
/**
|
|
707
|
+
* 从 CDN 获取主应用 Manifest(ROOT_APP_CODE)
|
|
708
|
+
* - 仅用于校验/兜底:优先使用本地 dist 或缓存 node_modules 的 Manifest
|
|
709
|
+
*/
|
|
710
|
+
var downloadMainAppManifestJson = function (currentManifest) { return __awaiter(void 0, void 0, void 0, function () {
|
|
711
|
+
var mode, code, cdnUrl, manifestJson;
|
|
712
|
+
return __generator(this, function (_b) {
|
|
713
|
+
switch (_b.label) {
|
|
714
|
+
case 0:
|
|
715
|
+
mode = currentManifest.mode, code = currentManifest.code;
|
|
716
|
+
_b.label = 1;
|
|
717
|
+
case 1:
|
|
718
|
+
_b.trys.push([1, 3, , 4]);
|
|
719
|
+
cdnUrl = cdn.getManifestUrl({
|
|
720
|
+
code: code,
|
|
721
|
+
appCode: ROOT_APP_CODE,
|
|
722
|
+
mode: mode,
|
|
723
|
+
});
|
|
724
|
+
return [4 /*yield*/, fetchFileByPath(cdnUrl)];
|
|
725
|
+
case 2:
|
|
726
|
+
manifestJson = _b.sent();
|
|
727
|
+
return [2 /*return*/, manifestJson];
|
|
728
|
+
case 3:
|
|
729
|
+
_b.sent();
|
|
730
|
+
return [3 /*break*/, 4];
|
|
731
|
+
case 4: return [2 /*return*/, {}];
|
|
732
|
+
}
|
|
733
|
+
});
|
|
734
|
+
}); };
|
|
735
|
+
/**
|
|
736
|
+
* 读取根主应用 Manifest(优先级:dist > node_modules 缓存 > CDN)
|
|
737
|
+
* - 兼容不同构建场景:
|
|
738
|
+
* - isServe(WS 联调)或内部构建(inner build)下无需校验哈希
|
|
739
|
+
* - 其他场景严格校验本地与远端 Manifest 的 hash 一致性
|
|
740
|
+
* - 选择顺序:
|
|
741
|
+
* 1) 当前构建输出目录的 app.json(dist)
|
|
742
|
+
* 2) node_modules 缓存目录中的 app.json(save cdn file path)
|
|
743
|
+
* 3) 远端 CDN 拉取的 Manifest(兜底)
|
|
744
|
+
*/
|
|
745
|
+
var getRootMainManifestJson = function (currentManifest) { return __awaiter(void 0, void 0, void 0, function () {
|
|
746
|
+
var mode, isInnerBuild, isWsBuild, needCheckHash, originHostManifestJson, checkHash, distPagePath, pageManifest, nodeModulePath, nodeModulesManifest;
|
|
747
|
+
return __generator(this, function (_a) {
|
|
748
|
+
switch (_a.label) {
|
|
749
|
+
case 0:
|
|
750
|
+
mode = currentManifest.mode;
|
|
751
|
+
isInnerBuild = checkIsInnerBuild();
|
|
752
|
+
isWsBuild = currentManifest.isServe;
|
|
753
|
+
needCheckHash = !(isWsBuild || isInnerBuild);
|
|
754
|
+
return [4 /*yield*/, downloadMainAppManifestJson(currentManifest)];
|
|
755
|
+
case 1:
|
|
756
|
+
originHostManifestJson = _a.sent();
|
|
757
|
+
checkHash = function (target) {
|
|
758
|
+
if ((originHostManifestJson === null || originHostManifestJson === void 0 ? void 0 : originHostManifestJson.hash) && needCheckHash && (originHostManifestJson === null || originHostManifestJson === void 0 ? void 0 : originHostManifestJson.hash) !== target.hash) {
|
|
759
|
+
throw new Error("originHostManifestJson hash not equal");
|
|
760
|
+
}
|
|
761
|
+
};
|
|
762
|
+
try {
|
|
763
|
+
distPagePath = path.join(process.env.UNI_OUTPUT_DIR, MANIFEST_NAME);
|
|
764
|
+
pageManifest = uniReadFile(distPagePath);
|
|
765
|
+
checkHash(pageManifest);
|
|
766
|
+
console.log("distPagePath done");
|
|
767
|
+
if (Object.keys(pageManifest).length !== 0) {
|
|
768
|
+
return [2 /*return*/, pageManifest];
|
|
769
|
+
}
|
|
770
|
+
}
|
|
771
|
+
catch (_b) { }
|
|
772
|
+
try {
|
|
773
|
+
nodeModulePath = path.join(SAVE_CDN_FILE_PATH, mode, ROOT_APP_CODE, MANIFEST_NAME);
|
|
774
|
+
nodeModulesManifest = uniReadFile(nodeModulePath);
|
|
775
|
+
checkHash(nodeModulesManifest);
|
|
776
|
+
console.log("nodeModulePath done");
|
|
777
|
+
if (Object.keys(nodeModulesManifest).length !== 0) {
|
|
778
|
+
return [2 /*return*/, nodeModulesManifest];
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
catch (_c) { }
|
|
782
|
+
console.log('origin done');
|
|
783
|
+
return [2 /*return*/, originHostManifestJson];
|
|
784
|
+
}
|
|
785
|
+
});
|
|
786
|
+
}); };
|
|
685
787
|
|
|
686
788
|
var num = 5;
|
|
789
|
+
/**
|
|
790
|
+
* 从主应用 HTTP 服务拉取当前进程环境(含 UNI_OUTPUT_DIR)
|
|
791
|
+
* - serve 非联调模式下,子应用通过此接口获知主应用输出根目录
|
|
792
|
+
*/
|
|
687
793
|
var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
688
794
|
var URL, mainProcess, result;
|
|
689
795
|
return __generator(this, function (_a) {
|
|
@@ -701,6 +807,10 @@ var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, func
|
|
|
701
807
|
}
|
|
702
808
|
});
|
|
703
809
|
}); };
|
|
810
|
+
/**
|
|
811
|
+
* 简易重试(最多 5 次,每次 100ms)
|
|
812
|
+
* - 主应用 HTTP 服务启动可能有延迟,子应用循环尝试获取
|
|
813
|
+
*/
|
|
704
814
|
var getMainProcessLoop = function () { return __awaiter(void 0, void 0, void 0, function () {
|
|
705
815
|
var mainProcess;
|
|
706
816
|
return __generator(this, function (_a) {
|
|
@@ -756,6 +866,10 @@ var resetOutDir = function (currentManifestJson, config) { return __awaiter(void
|
|
|
756
866
|
process.env.MFE_ROOT_OUTPUT_DIR = (_a = process.env.MFE_SOURCE_OUTPUT_DIR) === null || _a === void 0 ? void 0 : _a.replace(exp, "/").replace(/\/$/, "");
|
|
757
867
|
// if (!currentManifestJson.value.isRoot)
|
|
758
868
|
// console.log(config);
|
|
869
|
+
/**
|
|
870
|
+
* 非内部构建下,将 UNI_OUTPUT_DIR 统一指向主应用根输出目录
|
|
871
|
+
* - 便于后续写入 app.json 与拷贝产物
|
|
872
|
+
*/
|
|
759
873
|
if (!checkIsInnerBuild()) {
|
|
760
874
|
// setTimeout(() => {
|
|
761
875
|
process.env.UNI_OUTPUT_DIR = process.env.MFE_ROOT_OUTPUT_DIR;
|
|
@@ -766,6 +880,11 @@ var resetOutDir = function (currentManifestJson, config) { return __awaiter(void
|
|
|
766
880
|
});
|
|
767
881
|
}); };
|
|
768
882
|
|
|
883
|
+
/**
|
|
884
|
+
* 接管 uni 内置复制钩子
|
|
885
|
+
* - 通过 before/after 钩子记录从源到目标的拷贝行为
|
|
886
|
+
* - 便于在 Manifest 插件中收集运行期拷贝产生的文件清单
|
|
887
|
+
*/
|
|
769
888
|
var addUniCopyPluginHook = function (_a) {
|
|
770
889
|
var before = _a.before, after = _a.after;
|
|
771
890
|
var FileWatcher = require("@dcloudio/uni-cli-shared/dist/watcher").FileWatcher;
|
|
@@ -787,6 +906,11 @@ var copyFilesByTargetPath = function (sourcePath, targetPath) {
|
|
|
787
906
|
fsExtra.ensureDirSync(targetDir);
|
|
788
907
|
}
|
|
789
908
|
catch (err) { }
|
|
909
|
+
/**
|
|
910
|
+
* 内容比较:
|
|
911
|
+
* - 仅当源/目标均为文件且内容完全一致时跳过,避免无效写入
|
|
912
|
+
* - 目录或不存在的目标不跳过
|
|
913
|
+
*/
|
|
790
914
|
var shouldSkip = function () {
|
|
791
915
|
try {
|
|
792
916
|
var s = fs.statSync(sourcePath);
|
|
@@ -795,6 +919,11 @@ var copyFilesByTargetPath = function (sourcePath, targetPath) {
|
|
|
795
919
|
return false;
|
|
796
920
|
var sb = fs.readFileSync(sourcePath);
|
|
797
921
|
var tb = fs.readFileSync(targetPath);
|
|
922
|
+
/**
|
|
923
|
+
* Buffer 比较:
|
|
924
|
+
* - 长度与内容完全一致时跳过
|
|
925
|
+
* - 保留二进制级别比较,适配图片/字体等非文本文件
|
|
926
|
+
*/
|
|
798
927
|
return sb.length === tb.length && sb.equals(tb);
|
|
799
928
|
}
|
|
800
929
|
catch (_a) {
|
|
@@ -883,6 +1012,13 @@ var initPrePagesJson = function () {
|
|
|
883
1012
|
var pagesPath = path.resolve(process.cwd(), "src/pages.json");
|
|
884
1013
|
return fs.readFileSync(pagesPath, "utf-8");
|
|
885
1014
|
};
|
|
1015
|
+
/**
|
|
1016
|
+
* 解析并预处理 pages.json
|
|
1017
|
+
* - 读取全局 preprocess 上下文(根据平台注入不同变量)
|
|
1018
|
+
* - 使用 @dcloudio 的 preprocess 对 pages.json 做条件编译(如 #ifdef/#endif)
|
|
1019
|
+
* - 返回 JS 对象形式的 pages 配置
|
|
1020
|
+
* - isFormat=true 且平台为 mp-weixin 时,转换为微信特定格式(去除 globalStyle 并映射到 window/subPackages)
|
|
1021
|
+
*/
|
|
886
1022
|
var getPagesJson = function (jsonFile, isFormat) {
|
|
887
1023
|
if (isFormat === void 0) { isFormat = false; }
|
|
888
1024
|
var platform = process.env.UNI_PLATFORM;
|
|
@@ -895,6 +1031,12 @@ var getPagesJson = function (jsonFile, isFormat) {
|
|
|
895
1031
|
}
|
|
896
1032
|
return pagesConfig;
|
|
897
1033
|
};
|
|
1034
|
+
/**
|
|
1035
|
+
* 将 Uni 的 pages.json 转换为微信小程序格式
|
|
1036
|
+
* - window:映射 globalStyle,并兼容 mp-weixin 特定字段
|
|
1037
|
+
* - subPackages:后续会在 app-json.ts 中由各子应用补充到主应用
|
|
1038
|
+
* - usingComponents:保留全局组件声明
|
|
1039
|
+
*/
|
|
898
1040
|
var transformToWeixinFormat = function (uniPagesConfig) {
|
|
899
1041
|
var _a;
|
|
900
1042
|
var weixinConfig = __assign({ pages: [], window: {}, tabBar: null, subPackages: [], usingComponents: {} }, uniPagesConfig);
|
|
@@ -960,6 +1102,19 @@ var transformToWeixinFormat = function (uniPagesConfig) {
|
|
|
960
1102
|
};
|
|
961
1103
|
|
|
962
1104
|
// import { addRunningAppToSave } from "../running-core";
|
|
1105
|
+
/**
|
|
1106
|
+
* Manifest 采集与产物清单插件
|
|
1107
|
+
* - 目标:收集构建阶段产物与运行期拷贝的文件列表,生成可供主应用消费的文件清单
|
|
1108
|
+
* - CollectFiles:
|
|
1109
|
+
* - emitted:bundle 产物与拷贝文件的统一索引(fileName -> fileUrl)
|
|
1110
|
+
* - callbackFiles:拦截 uni 的复制钩子,收集被拷贝的源与目标相对路径
|
|
1111
|
+
* - copyFilesToPublishDir:将 outDir 下的产物拷贝到 publish 目录,便于后续上载/分发
|
|
1112
|
+
* - 插件钩子:
|
|
1113
|
+
* - config:记录 outDir,拦截 uni 拷贝行为
|
|
1114
|
+
* - renderStart:解析 pages.json,写入到 Manifest 状态
|
|
1115
|
+
* - writeBundle:收集本次打包输出的 chunk 信息
|
|
1116
|
+
* - closeBundle:汇总拷贝与产物,写入 Manifest 文件并拷贝到发布目录
|
|
1117
|
+
*/
|
|
963
1118
|
var CollectFiles = /** @class */ (function () {
|
|
964
1119
|
function CollectFiles() {
|
|
965
1120
|
this.callbackFiles = [];
|
|
@@ -979,6 +1134,11 @@ var CollectFiles = /** @class */ (function () {
|
|
|
979
1134
|
Object.keys(bundles).forEach(function (key) {
|
|
980
1135
|
var boundle = bundles[key];
|
|
981
1136
|
var fileName = boundle.fileName, source = boundle.source, code = boundle.code;
|
|
1137
|
+
/**
|
|
1138
|
+
* code/source:
|
|
1139
|
+
* - chunk 使用 code 字段,asset 使用 source 字段
|
|
1140
|
+
* - 统一抽象为 fileInfo 行(包含 fileUrl 与大小哈希)
|
|
1141
|
+
*/
|
|
982
1142
|
var fileInfo = genreFileInfoRow({ fileName: fileName, source: code || source });
|
|
983
1143
|
_this.emitted.set(fileName, fileInfo);
|
|
984
1144
|
});
|
|
@@ -1073,6 +1233,12 @@ var createManifestPlugin = function (manifestJson) {
|
|
|
1073
1233
|
};
|
|
1074
1234
|
};
|
|
1075
1235
|
|
|
1236
|
+
/**
|
|
1237
|
+
* 主应用 app.json 渲染与写入
|
|
1238
|
+
* - renderPagesJsonByArray:将多个子应用的 pages.json 以分包形式合入主应用
|
|
1239
|
+
* - genreFullMainAppJsonByManifestList:按 Manifest 列表生成完整的主应用 app.json
|
|
1240
|
+
* - genreNewAppJson:内容比较后写入 app.json,避免无效写入导致的频繁重启
|
|
1241
|
+
*/
|
|
1076
1242
|
var renderPagesJsonByArray = function (appPages, pageJson) {
|
|
1077
1243
|
var _loop_1 = function (app) {
|
|
1078
1244
|
var _a = getPagesJson(JSON.stringify((app === null || app === void 0 ? void 0 : app.pagesJson) || {}), true).pages, pages = _a === void 0 ? [] : _a;
|
|
@@ -1106,6 +1272,14 @@ var genreNewAppJson = function (outputPageJsonPath, appJson, manifestList) {
|
|
|
1106
1272
|
}
|
|
1107
1273
|
};
|
|
1108
1274
|
|
|
1275
|
+
/**
|
|
1276
|
+
* 运行期联调核心
|
|
1277
|
+
* - getAppsManifestList:在主应用非联调构建模式下,返回所有子应用的 Manifest 路径列表
|
|
1278
|
+
* - startDistWatcher:监听子应用输出目录的父级变更,增量拷贝到主应用分包路径,并上报变更
|
|
1279
|
+
* - 设计要点:
|
|
1280
|
+
* - 监听父级目录以覆盖新增/删除场景;通过 `isTargetFile` 过滤目标变化
|
|
1281
|
+
* - 拷贝时进行内容比较,避免触发无效写入
|
|
1282
|
+
*/
|
|
1109
1283
|
var getAppsManifestList = function (mode) {
|
|
1110
1284
|
var _a;
|
|
1111
1285
|
var manifest = formatCliCommandConfig(mode);
|
|
@@ -1135,6 +1309,11 @@ var startDistWatcher = function (mainPwd, onReady, onChange) {
|
|
|
1135
1309
|
var filePath = path.resolve(root);
|
|
1136
1310
|
checkAndgenreDir(filePath);
|
|
1137
1311
|
var parentDir = path.dirname(filePath);
|
|
1312
|
+
/**
|
|
1313
|
+
* 监听父级目录:
|
|
1314
|
+
* - uni 输出目录在构建时可能新增/删除文件或目录,监听父级可捕捉到此类变化
|
|
1315
|
+
* - 通过 isTargetFile 精确过滤与目标目录相关的事件
|
|
1316
|
+
*/
|
|
1138
1317
|
var watcher = createFileWatcher(parentDir);
|
|
1139
1318
|
var isTargetFile = function (p) {
|
|
1140
1319
|
return p.startsWith(filePath + path.sep) || p === filePath;
|
|
@@ -1149,6 +1328,11 @@ var startDistWatcher = function (mainPwd, onReady, onChange) {
|
|
|
1149
1328
|
var sourcePath = p;
|
|
1150
1329
|
var targetPath = path.join(mainPwd, rel);
|
|
1151
1330
|
if (["add", "change"].includes(evt)) {
|
|
1331
|
+
/**
|
|
1332
|
+
* 增量拷贝:
|
|
1333
|
+
* - 目录结构保持一致,从子应用输出拷贝到主应用分包路径
|
|
1334
|
+
* - 通过 copyFilesByTargetPath 的内容比较避免重复写入
|
|
1335
|
+
*/
|
|
1152
1336
|
copyFilesByTargetPath(sourcePath, targetPath);
|
|
1153
1337
|
}
|
|
1154
1338
|
onChange === null || onChange === void 0 ? void 0 : onChange({ type: evt, p: p, sourcePath: sourcePath, targetPath: targetPath });
|
|
@@ -1157,6 +1341,13 @@ var startDistWatcher = function (mainPwd, onReady, onChange) {
|
|
|
1157
1341
|
};
|
|
1158
1342
|
|
|
1159
1343
|
var app = null;
|
|
1344
|
+
/**
|
|
1345
|
+
* 创建开发态 HTTP 服务
|
|
1346
|
+
* - 单例复用:避免重复创建与重复监听
|
|
1347
|
+
* - 路由:
|
|
1348
|
+
* - GET `${HTTP_PATH}/root-path`:返回当前进程的环境变量(主应用输出位置等)
|
|
1349
|
+
* - 与 WS 服务配合:WSServer 绑定同一个 HTTP server 进行升级
|
|
1350
|
+
*/
|
|
1160
1351
|
var createHttpServer = function () {
|
|
1161
1352
|
if (app)
|
|
1162
1353
|
return { server: app, start: function () { } };
|
|
@@ -1166,6 +1357,11 @@ var createHttpServer = function () {
|
|
|
1166
1357
|
res.send(process.env);
|
|
1167
1358
|
});
|
|
1168
1359
|
app.use(cors({ origin: "*" }));
|
|
1360
|
+
/**
|
|
1361
|
+
* 返回:
|
|
1362
|
+
* - server:HTTP Server 实例(供 WS 复用)
|
|
1363
|
+
* - start(type):启动监听并输出日志,type 用于日志区分('http' | 'ws')
|
|
1364
|
+
*/
|
|
1169
1365
|
return {
|
|
1170
1366
|
server: server,
|
|
1171
1367
|
start: function (type) {
|
|
@@ -1183,6 +1379,10 @@ var WsServer = /** @class */ (function () {
|
|
|
1183
1379
|
this.httpServer = createHttpServer();
|
|
1184
1380
|
this.clientMap = new Map();
|
|
1185
1381
|
}
|
|
1382
|
+
/**
|
|
1383
|
+
* 单例获取 WS 服务实例
|
|
1384
|
+
* - 若已创建则复用并更新消息回调
|
|
1385
|
+
*/
|
|
1186
1386
|
WsServer.getInstance = function (handleMessage) {
|
|
1187
1387
|
if (!WsServer.instance) {
|
|
1188
1388
|
WsServer.instance = new WsServer(handleMessage);
|
|
@@ -1210,12 +1410,18 @@ var WsServer = /** @class */ (function () {
|
|
|
1210
1410
|
this.handleMessage = callback;
|
|
1211
1411
|
}
|
|
1212
1412
|
};
|
|
1413
|
+
/**
|
|
1414
|
+
* 向指定 appCode 的客户端发送消息
|
|
1415
|
+
*/
|
|
1213
1416
|
WsServer.prototype.sendMessageToApp = function (appCode, message) {
|
|
1214
1417
|
var ws = this.clientMap.get(appCode);
|
|
1215
1418
|
if (ws) {
|
|
1216
1419
|
ws.send(typeof message === "string" ? message : JSON.stringify(message));
|
|
1217
1420
|
}
|
|
1218
1421
|
};
|
|
1422
|
+
/**
|
|
1423
|
+
* 连接事件:建立客户端映射并下发初始化信息(主应用输出目录)
|
|
1424
|
+
*/
|
|
1219
1425
|
WsServer.prototype.onConnection = function () {
|
|
1220
1426
|
var _this = this;
|
|
1221
1427
|
this.wss.on("connection", function (ws, request) {
|
|
@@ -1254,6 +1460,9 @@ var WsClientServer = /** @class */ (function () {
|
|
|
1254
1460
|
this.ws = null;
|
|
1255
1461
|
this.isConnected = false;
|
|
1256
1462
|
}
|
|
1463
|
+
/**
|
|
1464
|
+
* 获取客户端单例
|
|
1465
|
+
*/
|
|
1257
1466
|
WsClientServer.getInstance = function (handleMessage) {
|
|
1258
1467
|
if (!WsClientServer.instance) {
|
|
1259
1468
|
WsClientServer.instance = new WsClientServer(handleMessage);
|
|
@@ -1307,6 +1516,9 @@ var WsClientServer = /** @class */ (function () {
|
|
|
1307
1516
|
this.handleMessage = callback;
|
|
1308
1517
|
}
|
|
1309
1518
|
};
|
|
1519
|
+
/**
|
|
1520
|
+
* 简单重试策略:一定时间后重新连接
|
|
1521
|
+
*/
|
|
1310
1522
|
WsClientServer.prototype.retryConnect = function (appCode) {
|
|
1311
1523
|
var _this = this;
|
|
1312
1524
|
if (this.isConnected)
|
|
@@ -1331,6 +1543,20 @@ var WsClientServer = /** @class */ (function () {
|
|
|
1331
1543
|
// export const mfeServer = new WsServer();
|
|
1332
1544
|
// export const mfeClientServer = new WsClientServer();
|
|
1333
1545
|
|
|
1546
|
+
/**
|
|
1547
|
+
* 主应用插件(Main App)
|
|
1548
|
+
* - 服务端(根应用):
|
|
1549
|
+
* - 启动 WS + HTTP 服务,向子应用广播初始化信息(主应用输出目录)
|
|
1550
|
+
* - 接收子应用文件变更事件,拉取对应子应用 Manifest 并增量更新主应用 app.json
|
|
1551
|
+
* - 客户端(子应用):
|
|
1552
|
+
* - 连接主应用 WS,接收初始化 pwd 后将自身构建产物增量拷贝到主应用分包路径
|
|
1553
|
+
* - 在构建完成后启动本地 DistWatcher,监控自身输出变更并通过 WS 通知主应用
|
|
1554
|
+
* - 构建阶段:
|
|
1555
|
+
* - 合并所有依赖子应用的 pages.json,生成最终主应用 app.json,内容比较避免无效写入
|
|
1556
|
+
* - 关键点:
|
|
1557
|
+
* - `isBuild`/`isServe`/`isRoot` 三态控制插件行为,避免生产与联调逻辑相互影响
|
|
1558
|
+
* - `copyFilesByTargetPath` 与 `genreNewAppJson` 均内置内容比较,防止频繁重启
|
|
1559
|
+
*/
|
|
1334
1560
|
var filterManifestJsonListAndMainPageJson = function (manifestJsonList) {
|
|
1335
1561
|
var outputPageJsonPath = getMainAppJsonPath();
|
|
1336
1562
|
return {
|
|
@@ -1382,6 +1608,11 @@ var createMainAppPlugin = function (manifestJson) {
|
|
|
1382
1608
|
startDistWatcher(state.mainPwd, function () {
|
|
1383
1609
|
state.isWatcherReady = true;
|
|
1384
1610
|
}, function (change) {
|
|
1611
|
+
/**
|
|
1612
|
+
* 通过 WS 将子应用的输出变更上报主应用
|
|
1613
|
+
* - 包含事件类型、源/目标路径、相对路径等
|
|
1614
|
+
* - 主应用端在收到 CHANGE 后增量更新 app.json
|
|
1615
|
+
*/
|
|
1385
1616
|
state.fn = function () {
|
|
1386
1617
|
return state.mfeClientServer.sendMessage(E_WS_TYPE.CHANGE, __assign(__assign({}, change), { appCode: manifestJson.value.appCode }));
|
|
1387
1618
|
};
|
|
@@ -1399,6 +1630,10 @@ var createMainAppPlugin = function (manifestJson) {
|
|
|
1399
1630
|
copyFilesByTargetPath(sourcePath, targetPath);
|
|
1400
1631
|
};
|
|
1401
1632
|
var watchFile = function () {
|
|
1633
|
+
/**
|
|
1634
|
+
* 在非 serve + 主应用构建时,监听所有子应用 Manifest 文件变化
|
|
1635
|
+
* - 变化时重算并写入 app.json,保持主应用 pages 配置最新
|
|
1636
|
+
*/
|
|
1402
1637
|
var allManifest = getAppsManifestList(manifestJson.value.mode);
|
|
1403
1638
|
var watchFileList = allManifest.map(function (item) { return item.filePath; });
|
|
1404
1639
|
var watcher = createFileWatcher(watchFileList);
|
|
@@ -1424,13 +1659,16 @@ var createMainAppPlugin = function (manifestJson) {
|
|
|
1424
1659
|
start();
|
|
1425
1660
|
return [2 /*return*/];
|
|
1426
1661
|
}
|
|
1662
|
+
/**
|
|
1663
|
+
* 联调开发:启动根应用 WS 服务,负责给子应用下发初始化信息并接收变更事件
|
|
1664
|
+
*/
|
|
1427
1665
|
createMainAppServer();
|
|
1428
|
-
// const subs = findLocalSubApps();
|
|
1429
|
-
// if (subs.length) {
|
|
1430
|
-
// startLocalSubApps(subs, "mp-weixin", manifestJson.value.mode);
|
|
1431
|
-
// }
|
|
1432
1666
|
}
|
|
1433
1667
|
else {
|
|
1668
|
+
/**
|
|
1669
|
+
* 子应用:作为 WS 客户端连接主应用
|
|
1670
|
+
* - 收到 INIT 后,将自身产物拷贝到主应用分包路径
|
|
1671
|
+
*/
|
|
1434
1672
|
state.mfeClientServer = createMainAppClient(manifestJson, function (data) {
|
|
1435
1673
|
copyAppDistModule(data);
|
|
1436
1674
|
});
|
|
@@ -1500,8 +1738,237 @@ var createMainAppPlugin = function (manifestJson) {
|
|
|
1500
1738
|
];
|
|
1501
1739
|
};
|
|
1502
1740
|
|
|
1741
|
+
/**
|
|
1742
|
+
* 运行时暴露插件(Exposes)
|
|
1743
|
+
* - 目标:在页面/模块中以 `@dd-code/runtime` 或 `@dd-code/runtime/<name>` 引用主应用的公共导出
|
|
1744
|
+
* - 流程:
|
|
1745
|
+
* 1) configResolved:从主应用 Manifest 读取 exposes 映射('.'、'./axios' 等)
|
|
1746
|
+
* 2) resolveId:拦截并识别 runtime 请求
|
|
1747
|
+
* 3) load:按请求的子路径拼装 require 代码,指向主应用模块(使用相对路径计算)
|
|
1748
|
+
* 4) buildStart:为每个 exposes 键发射独立的入口 chunk,确保输出到根目录
|
|
1749
|
+
* 5) generateBundle:收集产物映射(fileName + exports),保存到当前 Manifest
|
|
1750
|
+
* - 注意:
|
|
1751
|
+
* - `RUNTIME_NAME_REGEXP` 用于识别所有 runtime 形式,键以 '.' 开头并支持层级 './xxx'
|
|
1752
|
+
* - require 的相对路径通过 `renderRuntimeCode` 按当前 appCode + 构建位置计算,保持稳定
|
|
1753
|
+
*/
|
|
1754
|
+
var buildName = "mfe_runtime/__mfe_{template}_runtime__.js";
|
|
1755
|
+
var RUNTIME_NAME = "@dd-code/runtime";
|
|
1756
|
+
// const runtimeDir = 'mfe_runtime'
|
|
1757
|
+
var RUNTIME_NAME_REGEXP = new RegExp(RUNTIME_NAME + "(/.*)?");
|
|
1758
|
+
/**
|
|
1759
|
+
* 生成 runtime 入口代码
|
|
1760
|
+
* - 参数:
|
|
1761
|
+
* - moduleExpose:主应用 exposes 配置中指定的模块信息({ path, exports })
|
|
1762
|
+
* - appCode:当前应用代码,用于计算运行时代码与目标模块的相对路径
|
|
1763
|
+
* - 相对路径计算:
|
|
1764
|
+
* - runtime 文件输出位置:mfe_runtime/__mfe_{template}_runtime__.js
|
|
1765
|
+
* - 运行时代码中 require 的相对路径需指向主应用根下的模块文件(如 store/index.js)
|
|
1766
|
+
* - deepPath = 相对路径(从 runtime 所在目录到项目根 "."),用于拼接 require('deepPath/<filePath>')
|
|
1767
|
+
* - 导出约定:
|
|
1768
|
+
* - 仅支持命名导出列表(exports: string[]),以 `export const {name} = require(...)` 形式暴露
|
|
1769
|
+
*/
|
|
1770
|
+
var renderRuntimeCode = function (moduleExpose, appCode) {
|
|
1771
|
+
var deepPath = path.relative(path.join(appCode, path.dirname(buildName)), ".");
|
|
1772
|
+
var resultCode = "";
|
|
1773
|
+
var _a = moduleExpose || {}, _b = _a.exports, exportName = _b === void 0 ? [] : _b, filePath = _a.path;
|
|
1774
|
+
exportName.forEach(function (item) {
|
|
1775
|
+
resultCode += "export const {".concat(item, "} = require('").concat(deepPath, "/").concat(filePath, "');");
|
|
1776
|
+
});
|
|
1777
|
+
return resultCode;
|
|
1778
|
+
};
|
|
1779
|
+
var createExposesPlugin = function (options, currentManifestJson) {
|
|
1780
|
+
if (options === void 0) { options = {}; }
|
|
1781
|
+
options.exposes = options.exposes || {};
|
|
1782
|
+
var mainAppExposeCode = {};
|
|
1783
|
+
return [
|
|
1784
|
+
{
|
|
1785
|
+
name: "@chagee:uni-exposes",
|
|
1786
|
+
enforce: "post",
|
|
1787
|
+
/**
|
|
1788
|
+
* 读取主应用的 exposes 配置
|
|
1789
|
+
* - 来源:根应用构建时生成的 Manifest(含 exposes 映射)
|
|
1790
|
+
* - 示例:
|
|
1791
|
+
* { '.': { path: 'store/index.js', exports: ['userStore'] },
|
|
1792
|
+
* './axios': { path: 'axios/index.js', exports: ['axios'] } }
|
|
1793
|
+
*/
|
|
1794
|
+
configResolved: function (config) {
|
|
1795
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1796
|
+
var manifestJson;
|
|
1797
|
+
return __generator(this, function (_a) {
|
|
1798
|
+
switch (_a.label) {
|
|
1799
|
+
case 0: return [4 /*yield*/, getRootMainManifestJson(currentManifestJson.value)];
|
|
1800
|
+
case 1:
|
|
1801
|
+
manifestJson = _a.sent();
|
|
1802
|
+
mainAppExposeCode = manifestJson.exposes || {};
|
|
1803
|
+
return [2 /*return*/];
|
|
1804
|
+
}
|
|
1805
|
+
});
|
|
1806
|
+
});
|
|
1807
|
+
},
|
|
1808
|
+
/**
|
|
1809
|
+
* 统一拦截 runtime 引用
|
|
1810
|
+
* - 支持 @dd-code/runtime 与 @dd-code/runtime/<name>
|
|
1811
|
+
* - 返回原始 id,交由 load 钩子生成具体代码
|
|
1812
|
+
*/
|
|
1813
|
+
resolveId: function (id, importer) {
|
|
1814
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1815
|
+
return __generator(this, function (_a) {
|
|
1816
|
+
if (RUNTIME_NAME_REGEXP.test(id)) {
|
|
1817
|
+
return [2 /*return*/, id];
|
|
1818
|
+
}
|
|
1819
|
+
return [2 /*return*/];
|
|
1820
|
+
});
|
|
1821
|
+
});
|
|
1822
|
+
},
|
|
1823
|
+
/**
|
|
1824
|
+
* 生成运行时代码
|
|
1825
|
+
* - 通过解析 id 获取子路径('.' 或 './name')
|
|
1826
|
+
* - 按 exposes 映射找到对应模块与导出列表,拼装 require 代码
|
|
1827
|
+
*/
|
|
1828
|
+
load: function (id) {
|
|
1829
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1830
|
+
var matched, deepPath, moduleExpose;
|
|
1831
|
+
return __generator(this, function (_a) {
|
|
1832
|
+
if (RUNTIME_NAME_REGEXP.test(id)) {
|
|
1833
|
+
matched = id.match(RUNTIME_NAME_REGEXP);
|
|
1834
|
+
deepPath = "." + ((matched === null || matched === void 0 ? void 0 : matched[1]) || "");
|
|
1835
|
+
moduleExpose = mainAppExposeCode[deepPath] || {};
|
|
1836
|
+
return [2 /*return*/, renderRuntimeCode(moduleExpose, currentManifestJson.value.appCode)];
|
|
1837
|
+
}
|
|
1838
|
+
return [2 /*return*/];
|
|
1839
|
+
});
|
|
1840
|
+
});
|
|
1841
|
+
},
|
|
1842
|
+
/**
|
|
1843
|
+
* 为每个 exposes 键发射一个 runtime chunk
|
|
1844
|
+
* - fileName 模板中 {template} 取键名,将非路径字符替换为可用形式(如 './axios' -> '_axios')
|
|
1845
|
+
* - 保证各入口在输出根目录下具有稳定文件名
|
|
1846
|
+
*/
|
|
1847
|
+
buildStart: function () {
|
|
1848
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1849
|
+
var _i, _a, _b, spec, r, id, fileName, fileNameWithoutExt;
|
|
1850
|
+
var _this = this;
|
|
1851
|
+
return __generator(this, function (_c) {
|
|
1852
|
+
switch (_c.label) {
|
|
1853
|
+
case 0:
|
|
1854
|
+
// 显式发射 runtime chunk,确保它被打包到根目录
|
|
1855
|
+
if (!currentManifestJson.value.isRoot) {
|
|
1856
|
+
Object.keys(mainAppExposeCode).forEach(function (key) {
|
|
1857
|
+
var keyDir = key.replace(/\./g, "").replace(/\//g, "_");
|
|
1858
|
+
_this.emitFile({
|
|
1859
|
+
type: "chunk",
|
|
1860
|
+
id: path.join(RUNTIME_NAME, key),
|
|
1861
|
+
fileName: buildName.replace("{template}", keyDir),
|
|
1862
|
+
});
|
|
1863
|
+
});
|
|
1864
|
+
}
|
|
1865
|
+
_i = 0, _a = Object.entries(options.exposes || {});
|
|
1866
|
+
_c.label = 1;
|
|
1867
|
+
case 1:
|
|
1868
|
+
if (!(_i < _a.length)) return [3 /*break*/, 4];
|
|
1869
|
+
_b = _a[_i], _b[0], spec = _b[1];
|
|
1870
|
+
return [4 /*yield*/, this.resolve(spec)];
|
|
1871
|
+
case 2:
|
|
1872
|
+
r = _c.sent();
|
|
1873
|
+
if (r) {
|
|
1874
|
+
id = r === null || r === void 0 ? void 0 : r.id;
|
|
1875
|
+
fileName = path.relative(process.cwd() + "/src/", id);
|
|
1876
|
+
fileNameWithoutExt = getFilePathWithoutExt(fileName);
|
|
1877
|
+
this.emitFile({
|
|
1878
|
+
type: "chunk",
|
|
1879
|
+
id: r.id,
|
|
1880
|
+
name: fileNameWithoutExt,
|
|
1881
|
+
});
|
|
1882
|
+
}
|
|
1883
|
+
_c.label = 3;
|
|
1884
|
+
case 3:
|
|
1885
|
+
_i++;
|
|
1886
|
+
return [3 /*break*/, 1];
|
|
1887
|
+
case 4: return [2 /*return*/];
|
|
1888
|
+
}
|
|
1889
|
+
});
|
|
1890
|
+
});
|
|
1891
|
+
},
|
|
1892
|
+
/**
|
|
1893
|
+
* 产物阶段:收集 exposes 的打包路径与导出列表
|
|
1894
|
+
* - 通过 chunk.modules[moduleId.id] 反查模块所属 chunk
|
|
1895
|
+
* - 保存到当前 Manifest,以便下游消费
|
|
1896
|
+
*/
|
|
1897
|
+
generateBundle: function (_, bundles) {
|
|
1898
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
1899
|
+
var chunks, resolveIdsMap, _loop_1, this_1, _a, _b, _c, _i, i;
|
|
1900
|
+
return __generator(this, function (_d) {
|
|
1901
|
+
switch (_d.label) {
|
|
1902
|
+
case 0:
|
|
1903
|
+
chunks = Object.values(bundles).filter(function (b) { return b.type === "chunk"; });
|
|
1904
|
+
resolveIdsMap = {};
|
|
1905
|
+
_loop_1 = function (i) {
|
|
1906
|
+
var moduleId, owner;
|
|
1907
|
+
return __generator(this, function (_e) {
|
|
1908
|
+
switch (_e.label) {
|
|
1909
|
+
case 0: return [4 /*yield*/, this_1.resolve(options.exposes[i])];
|
|
1910
|
+
case 1:
|
|
1911
|
+
moduleId = _e.sent();
|
|
1912
|
+
if (moduleId) {
|
|
1913
|
+
owner = chunks.find(function (c) { return c.modules && c.modules[moduleId.id]; });
|
|
1914
|
+
if (owner) {
|
|
1915
|
+
resolveIdsMap[i] = {
|
|
1916
|
+
path: owner.fileName,
|
|
1917
|
+
exports: owner.exports,
|
|
1918
|
+
};
|
|
1919
|
+
}
|
|
1920
|
+
}
|
|
1921
|
+
return [2 /*return*/];
|
|
1922
|
+
}
|
|
1923
|
+
});
|
|
1924
|
+
};
|
|
1925
|
+
this_1 = this;
|
|
1926
|
+
_a = options.exposes;
|
|
1927
|
+
_b = [];
|
|
1928
|
+
for (_c in _a)
|
|
1929
|
+
_b.push(_c);
|
|
1930
|
+
_i = 0;
|
|
1931
|
+
_d.label = 1;
|
|
1932
|
+
case 1:
|
|
1933
|
+
if (!(_i < _b.length)) return [3 /*break*/, 4];
|
|
1934
|
+
_c = _b[_i];
|
|
1935
|
+
if (!(_c in _a)) return [3 /*break*/, 3];
|
|
1936
|
+
i = _c;
|
|
1937
|
+
return [5 /*yield**/, _loop_1(i)];
|
|
1938
|
+
case 2:
|
|
1939
|
+
_d.sent();
|
|
1940
|
+
_d.label = 3;
|
|
1941
|
+
case 3:
|
|
1942
|
+
_i++;
|
|
1943
|
+
return [3 /*break*/, 1];
|
|
1944
|
+
case 4:
|
|
1945
|
+
currentManifestJson.setExposes(resolveIdsMap);
|
|
1946
|
+
return [2 /*return*/];
|
|
1947
|
+
}
|
|
1948
|
+
});
|
|
1949
|
+
});
|
|
1950
|
+
},
|
|
1951
|
+
},
|
|
1952
|
+
];
|
|
1953
|
+
};
|
|
1954
|
+
|
|
1955
|
+
/**
|
|
1956
|
+
* mp-weixin 平台插件聚合器
|
|
1957
|
+
* - 初始化并维护当前构建的 Manifest 管理器(state + IO)
|
|
1958
|
+
* - 根据模式预清理发布目录并重置 outDir(避免脏数据影响产物)
|
|
1959
|
+
* - 组合注册各业务插件:运行时暴露、资源搬运、Manifest 采集与主应用逻辑
|
|
1960
|
+
* - 插件间通过 `currentManifestJson` 共享上下文(环境、文件列表、依赖、exposes 映射)
|
|
1961
|
+
* - 插件注册顺序:
|
|
1962
|
+
* 1) pre: @dd-code:genre-params(设置环境、重置 outDir)
|
|
1963
|
+
* 2) post: exposes(生成 runtime 入口与 exposes 产物映射)
|
|
1964
|
+
* 3) pre: apps-assets(下载/搬运其它应用资源到主输出目录)
|
|
1965
|
+
* 4) default: manifest-plugin(采集 pages.json 与产物清单)
|
|
1966
|
+
* 5) post: main-app(serve/watch/merge 核心逻辑)
|
|
1967
|
+
*/
|
|
1503
1968
|
var createMpWeixinUniPlugin = function (options) {
|
|
1969
|
+
if (options === void 0) { options = {}; }
|
|
1504
1970
|
var currentManifestJson = createManifestManager();
|
|
1971
|
+
options.exposes = options.exposes || {};
|
|
1505
1972
|
return [
|
|
1506
1973
|
{
|
|
1507
1974
|
name: "@dd-code:genre-params",
|
|
@@ -1511,7 +1978,7 @@ var createMpWeixinUniPlugin = function (options) {
|
|
|
1511
1978
|
return __generator(this, function (_a) {
|
|
1512
1979
|
switch (_a.label) {
|
|
1513
1980
|
case 0:
|
|
1514
|
-
currentManifestJson.setEnv(config.mode);
|
|
1981
|
+
currentManifestJson.setEnv(config.mode || "dev");
|
|
1515
1982
|
// 清空 PUBLISH_PATH 目录
|
|
1516
1983
|
unlinkDeepDirOrFile(PUBLISH_PATH);
|
|
1517
1984
|
return [4 /*yield*/, resetOutDir(currentManifestJson, config)];
|
|
@@ -1523,6 +1990,7 @@ var createMpWeixinUniPlugin = function (options) {
|
|
|
1523
1990
|
});
|
|
1524
1991
|
},
|
|
1525
1992
|
},
|
|
1993
|
+
createExposesPlugin(options, currentManifestJson),
|
|
1526
1994
|
createAppsAssetsPlugin(currentManifestJson),
|
|
1527
1995
|
createManifestPlugin(currentManifestJson),
|
|
1528
1996
|
createMainAppPlugin(currentManifestJson),
|
|
@@ -1543,7 +2011,7 @@ var createMpWeixinUniPlugin = function (options) {
|
|
|
1543
2011
|
var index = (function (options) {
|
|
1544
2012
|
var platform = getPlatform();
|
|
1545
2013
|
return platform === EPlaform.MP_WEIXIN
|
|
1546
|
-
? createMpWeixinUniPlugin()
|
|
2014
|
+
? createMpWeixinUniPlugin(options)
|
|
1547
2015
|
: [];
|
|
1548
2016
|
});
|
|
1549
2017
|
|