@dd-code/uni-tools 1.0.13 → 1.0.14

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 CHANGED
@@ -2,16 +2,17 @@
2
2
 
3
3
  `@dd-code/uni-tools` 是一个针对 UniApp 的微信小程序(mp-weixin)模块化解决方案与工具集,目标是让「主应用 + 多子应用(分包)」的开发、联调与构建更顺畅、更自动化。
4
4
 
5
- ## 项目简介
6
- - 面向微信小程序的模块化架构:主应用负责壳与公共能力,子应用以分包形式集成。
7
- - 自动管理主/子应用的 `app.json`、`pages.json` 等配置合并,减少人工维护成本。
8
- - 开发态具备文件监听、自动同步与应用间通信能力,提升联调效率。
9
- - 提供统一 CLI 命令,简化拉取子应用仓库、开发、构建等流程。
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`:交互式拉取已配置的子应用仓库到本地工作空间(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)。
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
- - 插件入口与平台分发(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
- - 使用交互框选择需要拉取的应用,自动将其下载/更新至本地指定目录,便于快速联调。
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
- ```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
+ ```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/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?: Record<string, any>) => any[];
16
+ declare const _default: (options?: IUniConfigOptions) => any[];
13
17
 
14
18
  export { _default as default };
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() {
@@ -661,6 +666,9 @@ var createManifestManager = function () {
661
666
  newManifest.hash = generateSHA256(JSON.stringify(__assign({}, newManifest)));
662
667
  writeFiles(filePath, JSON.stringify(newManifest, null, 2));
663
668
  },
669
+ setExposes: function (exposes) {
670
+ row.exposes = exposes || {};
671
+ },
664
672
  setEnv: function (mode) {
665
673
  var env = formatCliCommandConfig(mode);
666
674
  var mfeJson = getMfeJson();
@@ -682,6 +690,47 @@ var createManifestManager = function () {
682
690
  },
683
691
  };
684
692
  };
693
+ var getRootMainManifestJson = function (code, mode) { return __awaiter(void 0, void 0, void 0, function () {
694
+ var pageManifest, nodeModulesManifest, cdnUrl, manifestJson;
695
+ return __generator(this, function (_b) {
696
+ switch (_b.label) {
697
+ case 0:
698
+ try {
699
+ pageManifest = uniReadFile(path.join(process.env.UNI_OUTPUT_DIR, MANIFEST_NAME));
700
+ if (Object.keys(pageManifest).length !== 0) {
701
+ return [2 /*return*/, pageManifest];
702
+ }
703
+ }
704
+ catch (_c) { }
705
+ try {
706
+ nodeModulesManifest = uniReadFile(path.join(SAVE_CDN_FILE_PATH, mode, ROOT_APP_CODE, MANIFEST_NAME));
707
+ if (Object.keys(nodeModulesManifest).length !== 0) {
708
+ return [2 /*return*/, nodeModulesManifest];
709
+ }
710
+ }
711
+ catch (_d) { }
712
+ _b.label = 1;
713
+ case 1:
714
+ _b.trys.push([1, 3, , 4]);
715
+ cdnUrl = cdn.getManifestUrl({
716
+ code: code,
717
+ appCode: ROOT_APP_CODE,
718
+ mode: mode,
719
+ });
720
+ return [4 /*yield*/, fetchFileByPath(cdnUrl)];
721
+ case 2:
722
+ manifestJson = _b.sent();
723
+ if (Object.keys(manifestJson).length !== 0) {
724
+ return [2 /*return*/, manifestJson];
725
+ }
726
+ return [3 /*break*/, 4];
727
+ case 3:
728
+ _b.sent();
729
+ return [3 /*break*/, 4];
730
+ case 4: return [2 /*return*/, { exposes: {} }];
731
+ }
732
+ });
733
+ }); };
685
734
 
686
735
  var num = 5;
687
736
  var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, function () {
@@ -1500,8 +1549,170 @@ var createMainAppPlugin = function (manifestJson) {
1500
1549
  ];
1501
1550
  };
1502
1551
 
1552
+ var buildName = "mfe_runtime/__mfe_{template}_runtime__.js";
1553
+ var RUNTIME_NAME = "@dd-code/runtime";
1554
+ // const runtimeDir = 'mfe_runtime'
1555
+ var RUNTIME_NAME_REGEXP = new RegExp(RUNTIME_NAME + "(/.*)?");
1556
+ var renderRuntimeCode = function (moduleExpose, appCode) {
1557
+ var deepPath = path.relative(path.join(appCode, path.dirname(buildName)), ".");
1558
+ var resultCode = "";
1559
+ var _a = moduleExpose || {}, _b = _a.exports, exportName = _b === void 0 ? [] : _b, filePath = _a.path;
1560
+ exportName.forEach(function (item) {
1561
+ resultCode += "export const {".concat(item, "} = require('").concat(deepPath, "/").concat(filePath, "');");
1562
+ });
1563
+ return resultCode;
1564
+ };
1565
+ var createExposesPlugin = function (options, currentManifestJson) {
1566
+ if (options === void 0) { options = {}; }
1567
+ options.exposes = options.exposes || {};
1568
+ var mainAppExposeCode = {};
1569
+ return [
1570
+ {
1571
+ name: "@chagee:uni-exposes",
1572
+ enforce: "post",
1573
+ configResolved: function (config) {
1574
+ return __awaiter(this, void 0, void 0, function () {
1575
+ var manifestJson;
1576
+ return __generator(this, function (_a) {
1577
+ switch (_a.label) {
1578
+ case 0: return [4 /*yield*/, getRootMainManifestJson(currentManifestJson.value.code, currentManifestJson.value.mode)];
1579
+ case 1:
1580
+ manifestJson = _a.sent();
1581
+ mainAppExposeCode = manifestJson.exposes || {};
1582
+ return [2 /*return*/];
1583
+ }
1584
+ });
1585
+ });
1586
+ },
1587
+ resolveId: function (id, importer) {
1588
+ return __awaiter(this, void 0, void 0, function () {
1589
+ return __generator(this, function (_a) {
1590
+ if (RUNTIME_NAME_REGEXP.test(id)) {
1591
+ return [2 /*return*/, id];
1592
+ }
1593
+ return [2 /*return*/];
1594
+ });
1595
+ });
1596
+ },
1597
+ load: function (id) {
1598
+ return __awaiter(this, void 0, void 0, function () {
1599
+ var matched, deepPath, moduleExpose;
1600
+ return __generator(this, function (_a) {
1601
+ if (RUNTIME_NAME_REGEXP.test(id)) {
1602
+ matched = id.match(RUNTIME_NAME_REGEXP);
1603
+ deepPath = "." + ((matched === null || matched === void 0 ? void 0 : matched[1]) || "");
1604
+ moduleExpose = mainAppExposeCode[deepPath] || {};
1605
+ return [2 /*return*/, renderRuntimeCode(moduleExpose, currentManifestJson.value.appCode)];
1606
+ }
1607
+ return [2 /*return*/];
1608
+ });
1609
+ });
1610
+ },
1611
+ buildStart: function () {
1612
+ return __awaiter(this, void 0, void 0, function () {
1613
+ var _i, _a, _b, spec, r, id, fileName, fileNameWithoutExt;
1614
+ var _this = this;
1615
+ return __generator(this, function (_c) {
1616
+ switch (_c.label) {
1617
+ case 0:
1618
+ // 显式发射 runtime chunk,确保它被打包到根目录
1619
+ Object.keys(mainAppExposeCode).forEach(function (key) {
1620
+ var keyDir = key.replace(/\./g, "").replace(/\//g, "_");
1621
+ _this.emitFile({
1622
+ type: "chunk",
1623
+ id: path.join(RUNTIME_NAME, key),
1624
+ fileName: buildName.replace("{template}", keyDir),
1625
+ });
1626
+ });
1627
+ _i = 0, _a = Object.entries(options.exposes || {});
1628
+ _c.label = 1;
1629
+ case 1:
1630
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
1631
+ _b = _a[_i], _b[0], spec = _b[1];
1632
+ return [4 /*yield*/, this.resolve(spec)];
1633
+ case 2:
1634
+ r = _c.sent();
1635
+ if (r) {
1636
+ id = r === null || r === void 0 ? void 0 : r.id;
1637
+ fileName = path.relative(process.cwd() + "/src/", id);
1638
+ fileNameWithoutExt = getFilePathWithoutExt(fileName);
1639
+ this.emitFile({
1640
+ type: "chunk",
1641
+ id: r.id,
1642
+ name: fileNameWithoutExt,
1643
+ });
1644
+ }
1645
+ _c.label = 3;
1646
+ case 3:
1647
+ _i++;
1648
+ return [3 /*break*/, 1];
1649
+ case 4: return [2 /*return*/];
1650
+ }
1651
+ });
1652
+ });
1653
+ },
1654
+ generateBundle: function (_, bundles) {
1655
+ return __awaiter(this, void 0, void 0, function () {
1656
+ var chunks, resolveIdsMap, _loop_1, this_1, _a, _b, _c, _i, i;
1657
+ return __generator(this, function (_d) {
1658
+ switch (_d.label) {
1659
+ case 0:
1660
+ chunks = Object.values(bundles).filter(function (b) { return b.type === "chunk"; });
1661
+ resolveIdsMap = {};
1662
+ _loop_1 = function (i) {
1663
+ var moduleId, owner;
1664
+ return __generator(this, function (_e) {
1665
+ switch (_e.label) {
1666
+ case 0: return [4 /*yield*/, this_1.resolve(options.exposes[i])];
1667
+ case 1:
1668
+ moduleId = _e.sent();
1669
+ if (moduleId) {
1670
+ owner = chunks.find(function (c) { return c.modules && c.modules[moduleId.id]; });
1671
+ if (owner) {
1672
+ resolveIdsMap[i] = {
1673
+ path: owner.fileName,
1674
+ exports: owner.exports,
1675
+ };
1676
+ }
1677
+ }
1678
+ return [2 /*return*/];
1679
+ }
1680
+ });
1681
+ };
1682
+ this_1 = this;
1683
+ _a = options.exposes;
1684
+ _b = [];
1685
+ for (_c in _a)
1686
+ _b.push(_c);
1687
+ _i = 0;
1688
+ _d.label = 1;
1689
+ case 1:
1690
+ if (!(_i < _b.length)) return [3 /*break*/, 4];
1691
+ _c = _b[_i];
1692
+ if (!(_c in _a)) return [3 /*break*/, 3];
1693
+ i = _c;
1694
+ return [5 /*yield**/, _loop_1(i)];
1695
+ case 2:
1696
+ _d.sent();
1697
+ _d.label = 3;
1698
+ case 3:
1699
+ _i++;
1700
+ return [3 /*break*/, 1];
1701
+ case 4:
1702
+ currentManifestJson.setExposes(resolveIdsMap);
1703
+ return [2 /*return*/];
1704
+ }
1705
+ });
1706
+ });
1707
+ },
1708
+ },
1709
+ ];
1710
+ };
1711
+
1503
1712
  var createMpWeixinUniPlugin = function (options) {
1713
+ if (options === void 0) { options = {}; }
1504
1714
  var currentManifestJson = createManifestManager();
1715
+ options.exposes = options.exposes || {};
1505
1716
  return [
1506
1717
  {
1507
1718
  name: "@dd-code:genre-params",
@@ -1511,7 +1722,7 @@ var createMpWeixinUniPlugin = function (options) {
1511
1722
  return __generator(this, function (_a) {
1512
1723
  switch (_a.label) {
1513
1724
  case 0:
1514
- currentManifestJson.setEnv(config.mode);
1725
+ currentManifestJson.setEnv(config.mode || "dev");
1515
1726
  // 清空 PUBLISH_PATH 目录
1516
1727
  unlinkDeepDirOrFile(PUBLISH_PATH);
1517
1728
  return [4 /*yield*/, resetOutDir(currentManifestJson, config)];
@@ -1523,6 +1734,7 @@ var createMpWeixinUniPlugin = function (options) {
1523
1734
  });
1524
1735
  },
1525
1736
  },
1737
+ createExposesPlugin(options, currentManifestJson),
1526
1738
  createAppsAssetsPlugin(currentManifestJson),
1527
1739
  createManifestPlugin(currentManifestJson),
1528
1740
  createMainAppPlugin(currentManifestJson),
@@ -1543,7 +1755,7 @@ var createMpWeixinUniPlugin = function (options) {
1543
1755
  var index = (function (options) {
1544
1756
  var platform = getPlatform();
1545
1757
  return platform === EPlaform.MP_WEIXIN
1546
- ? createMpWeixinUniPlugin()
1758
+ ? createMpWeixinUniPlugin(options)
1547
1759
  : [];
1548
1760
  });
1549
1761
 
package/dist/index.mjs.js CHANGED
@@ -273,6 +273,11 @@ var unlinkDeepDirOrFile = function (filePath) {
273
273
  console.error("删除文件夹失败:", err);
274
274
  }
275
275
  };
276
+ var getFilePathWithoutExt = function (filePath) {
277
+ var dirName = path.dirname(filePath);
278
+ var baseName = path.basename(filePath, path.extname(filePath));
279
+ return path.join(dirName, baseName);
280
+ };
276
281
 
277
282
  var UniCdnManager = /** @class */ (function () {
278
283
  function UniCdnManager() {
@@ -659,6 +664,9 @@ var createManifestManager = function () {
659
664
  newManifest.hash = generateSHA256(JSON.stringify(__assign({}, newManifest)));
660
665
  writeFiles(filePath, JSON.stringify(newManifest, null, 2));
661
666
  },
667
+ setExposes: function (exposes) {
668
+ row.exposes = exposes || {};
669
+ },
662
670
  setEnv: function (mode) {
663
671
  var env = formatCliCommandConfig(mode);
664
672
  var mfeJson = getMfeJson();
@@ -680,6 +688,47 @@ var createManifestManager = function () {
680
688
  },
681
689
  };
682
690
  };
691
+ var getRootMainManifestJson = function (code, mode) { return __awaiter(void 0, void 0, void 0, function () {
692
+ var pageManifest, nodeModulesManifest, cdnUrl, manifestJson;
693
+ return __generator(this, function (_b) {
694
+ switch (_b.label) {
695
+ case 0:
696
+ try {
697
+ pageManifest = uniReadFile(path.join(process.env.UNI_OUTPUT_DIR, MANIFEST_NAME));
698
+ if (Object.keys(pageManifest).length !== 0) {
699
+ return [2 /*return*/, pageManifest];
700
+ }
701
+ }
702
+ catch (_c) { }
703
+ try {
704
+ nodeModulesManifest = uniReadFile(path.join(SAVE_CDN_FILE_PATH, mode, ROOT_APP_CODE, MANIFEST_NAME));
705
+ if (Object.keys(nodeModulesManifest).length !== 0) {
706
+ return [2 /*return*/, nodeModulesManifest];
707
+ }
708
+ }
709
+ catch (_d) { }
710
+ _b.label = 1;
711
+ case 1:
712
+ _b.trys.push([1, 3, , 4]);
713
+ cdnUrl = cdn.getManifestUrl({
714
+ code: code,
715
+ appCode: ROOT_APP_CODE,
716
+ mode: mode,
717
+ });
718
+ return [4 /*yield*/, fetchFileByPath(cdnUrl)];
719
+ case 2:
720
+ manifestJson = _b.sent();
721
+ if (Object.keys(manifestJson).length !== 0) {
722
+ return [2 /*return*/, manifestJson];
723
+ }
724
+ return [3 /*break*/, 4];
725
+ case 3:
726
+ _b.sent();
727
+ return [3 /*break*/, 4];
728
+ case 4: return [2 /*return*/, { exposes: {} }];
729
+ }
730
+ });
731
+ }); };
683
732
 
684
733
  var num = 5;
685
734
  var getMainProcess = function () { return __awaiter(void 0, void 0, void 0, function () {
@@ -1498,8 +1547,170 @@ var createMainAppPlugin = function (manifestJson) {
1498
1547
  ];
1499
1548
  };
1500
1549
 
1550
+ var buildName = "mfe_runtime/__mfe_{template}_runtime__.js";
1551
+ var RUNTIME_NAME = "@dd-code/runtime";
1552
+ // const runtimeDir = 'mfe_runtime'
1553
+ var RUNTIME_NAME_REGEXP = new RegExp(RUNTIME_NAME + "(/.*)?");
1554
+ var renderRuntimeCode = function (moduleExpose, appCode) {
1555
+ var deepPath = path.relative(path.join(appCode, path.dirname(buildName)), ".");
1556
+ var resultCode = "";
1557
+ var _a = moduleExpose || {}, _b = _a.exports, exportName = _b === void 0 ? [] : _b, filePath = _a.path;
1558
+ exportName.forEach(function (item) {
1559
+ resultCode += "export const {".concat(item, "} = require('").concat(deepPath, "/").concat(filePath, "');");
1560
+ });
1561
+ return resultCode;
1562
+ };
1563
+ var createExposesPlugin = function (options, currentManifestJson) {
1564
+ if (options === void 0) { options = {}; }
1565
+ options.exposes = options.exposes || {};
1566
+ var mainAppExposeCode = {};
1567
+ return [
1568
+ {
1569
+ name: "@chagee:uni-exposes",
1570
+ enforce: "post",
1571
+ configResolved: function (config) {
1572
+ return __awaiter(this, void 0, void 0, function () {
1573
+ var manifestJson;
1574
+ return __generator(this, function (_a) {
1575
+ switch (_a.label) {
1576
+ case 0: return [4 /*yield*/, getRootMainManifestJson(currentManifestJson.value.code, currentManifestJson.value.mode)];
1577
+ case 1:
1578
+ manifestJson = _a.sent();
1579
+ mainAppExposeCode = manifestJson.exposes || {};
1580
+ return [2 /*return*/];
1581
+ }
1582
+ });
1583
+ });
1584
+ },
1585
+ resolveId: function (id, importer) {
1586
+ return __awaiter(this, void 0, void 0, function () {
1587
+ return __generator(this, function (_a) {
1588
+ if (RUNTIME_NAME_REGEXP.test(id)) {
1589
+ return [2 /*return*/, id];
1590
+ }
1591
+ return [2 /*return*/];
1592
+ });
1593
+ });
1594
+ },
1595
+ load: function (id) {
1596
+ return __awaiter(this, void 0, void 0, function () {
1597
+ var matched, deepPath, moduleExpose;
1598
+ return __generator(this, function (_a) {
1599
+ if (RUNTIME_NAME_REGEXP.test(id)) {
1600
+ matched = id.match(RUNTIME_NAME_REGEXP);
1601
+ deepPath = "." + ((matched === null || matched === void 0 ? void 0 : matched[1]) || "");
1602
+ moduleExpose = mainAppExposeCode[deepPath] || {};
1603
+ return [2 /*return*/, renderRuntimeCode(moduleExpose, currentManifestJson.value.appCode)];
1604
+ }
1605
+ return [2 /*return*/];
1606
+ });
1607
+ });
1608
+ },
1609
+ buildStart: function () {
1610
+ return __awaiter(this, void 0, void 0, function () {
1611
+ var _i, _a, _b, spec, r, id, fileName, fileNameWithoutExt;
1612
+ var _this = this;
1613
+ return __generator(this, function (_c) {
1614
+ switch (_c.label) {
1615
+ case 0:
1616
+ // 显式发射 runtime chunk,确保它被打包到根目录
1617
+ Object.keys(mainAppExposeCode).forEach(function (key) {
1618
+ var keyDir = key.replace(/\./g, "").replace(/\//g, "_");
1619
+ _this.emitFile({
1620
+ type: "chunk",
1621
+ id: path.join(RUNTIME_NAME, key),
1622
+ fileName: buildName.replace("{template}", keyDir),
1623
+ });
1624
+ });
1625
+ _i = 0, _a = Object.entries(options.exposes || {});
1626
+ _c.label = 1;
1627
+ case 1:
1628
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
1629
+ _b = _a[_i], _b[0], spec = _b[1];
1630
+ return [4 /*yield*/, this.resolve(spec)];
1631
+ case 2:
1632
+ r = _c.sent();
1633
+ if (r) {
1634
+ id = r === null || r === void 0 ? void 0 : r.id;
1635
+ fileName = path.relative(process.cwd() + "/src/", id);
1636
+ fileNameWithoutExt = getFilePathWithoutExt(fileName);
1637
+ this.emitFile({
1638
+ type: "chunk",
1639
+ id: r.id,
1640
+ name: fileNameWithoutExt,
1641
+ });
1642
+ }
1643
+ _c.label = 3;
1644
+ case 3:
1645
+ _i++;
1646
+ return [3 /*break*/, 1];
1647
+ case 4: return [2 /*return*/];
1648
+ }
1649
+ });
1650
+ });
1651
+ },
1652
+ generateBundle: function (_, bundles) {
1653
+ return __awaiter(this, void 0, void 0, function () {
1654
+ var chunks, resolveIdsMap, _loop_1, this_1, _a, _b, _c, _i, i;
1655
+ return __generator(this, function (_d) {
1656
+ switch (_d.label) {
1657
+ case 0:
1658
+ chunks = Object.values(bundles).filter(function (b) { return b.type === "chunk"; });
1659
+ resolveIdsMap = {};
1660
+ _loop_1 = function (i) {
1661
+ var moduleId, owner;
1662
+ return __generator(this, function (_e) {
1663
+ switch (_e.label) {
1664
+ case 0: return [4 /*yield*/, this_1.resolve(options.exposes[i])];
1665
+ case 1:
1666
+ moduleId = _e.sent();
1667
+ if (moduleId) {
1668
+ owner = chunks.find(function (c) { return c.modules && c.modules[moduleId.id]; });
1669
+ if (owner) {
1670
+ resolveIdsMap[i] = {
1671
+ path: owner.fileName,
1672
+ exports: owner.exports,
1673
+ };
1674
+ }
1675
+ }
1676
+ return [2 /*return*/];
1677
+ }
1678
+ });
1679
+ };
1680
+ this_1 = this;
1681
+ _a = options.exposes;
1682
+ _b = [];
1683
+ for (_c in _a)
1684
+ _b.push(_c);
1685
+ _i = 0;
1686
+ _d.label = 1;
1687
+ case 1:
1688
+ if (!(_i < _b.length)) return [3 /*break*/, 4];
1689
+ _c = _b[_i];
1690
+ if (!(_c in _a)) return [3 /*break*/, 3];
1691
+ i = _c;
1692
+ return [5 /*yield**/, _loop_1(i)];
1693
+ case 2:
1694
+ _d.sent();
1695
+ _d.label = 3;
1696
+ case 3:
1697
+ _i++;
1698
+ return [3 /*break*/, 1];
1699
+ case 4:
1700
+ currentManifestJson.setExposes(resolveIdsMap);
1701
+ return [2 /*return*/];
1702
+ }
1703
+ });
1704
+ });
1705
+ },
1706
+ },
1707
+ ];
1708
+ };
1709
+
1501
1710
  var createMpWeixinUniPlugin = function (options) {
1711
+ if (options === void 0) { options = {}; }
1502
1712
  var currentManifestJson = createManifestManager();
1713
+ options.exposes = options.exposes || {};
1503
1714
  return [
1504
1715
  {
1505
1716
  name: "@dd-code:genre-params",
@@ -1509,7 +1720,7 @@ var createMpWeixinUniPlugin = function (options) {
1509
1720
  return __generator(this, function (_a) {
1510
1721
  switch (_a.label) {
1511
1722
  case 0:
1512
- currentManifestJson.setEnv(config.mode);
1723
+ currentManifestJson.setEnv(config.mode || "dev");
1513
1724
  // 清空 PUBLISH_PATH 目录
1514
1725
  unlinkDeepDirOrFile(PUBLISH_PATH);
1515
1726
  return [4 /*yield*/, resetOutDir(currentManifestJson, config)];
@@ -1521,6 +1732,7 @@ var createMpWeixinUniPlugin = function (options) {
1521
1732
  });
1522
1733
  },
1523
1734
  },
1735
+ createExposesPlugin(options, currentManifestJson),
1524
1736
  createAppsAssetsPlugin(currentManifestJson),
1525
1737
  createManifestPlugin(currentManifestJson),
1526
1738
  createMainAppPlugin(currentManifestJson),
@@ -1541,7 +1753,7 @@ var createMpWeixinUniPlugin = function (options) {
1541
1753
  var index = (function (options) {
1542
1754
  var platform = getPlatform();
1543
1755
  return platform === EPlaform.MP_WEIXIN
1544
- ? createMpWeixinUniPlugin()
1756
+ ? createMpWeixinUniPlugin(options)
1545
1757
  : [];
1546
1758
  });
1547
1759
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dd-code/uni-tools",
3
- "version": "1.0.13",
3
+ "version": "1.0.14",
4
4
  "description": "Universal tools for code utilities",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",