'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const { mainPkgName } = require('../constant');
 
function analyzeGoDirection (usageMap, appJson, emitFileMap) {
  const copyComponentsForNormalPkgMap = {};
  const copyComponentsForMainPkg = new Set();
  const globalComponents = appJson.usingComponents || {};
  for (const [originalWxComponentPath, usageInfo] of usageMap) {
    const [pkgSet, pageOrComponentPaths] = usageInfo;
    // 被主包用到 或者 被多个分包用到,则组件放置主包中
    if (pkgSet.has(mainPkgName) || pkgSet.size > 1) {
      copyComponentsForMainPkg.add(originalWxComponentPath);
      continue;
    }
 
    // 到这里说明仅仅被一个普通分包使用,则将该组件复制到该普通分包中去
    const pkgRoot = [...pkgSet][0];
    const newComponentPath = `/${pkgRoot}${originalWxComponentPath}`;
 
    if (!copyComponentsForNormalPkgMap[pkgRoot]) {
      copyComponentsForNormalPkgMap[pkgRoot] = new Set();
    }
    copyComponentsForNormalPkgMap[pkgRoot].add(originalWxComponentPath);
 
    // 当前组件是否是全局组件
    const componentTagInGlobal = Object.keys(globalComponents).find(compoName => originalWxComponentPath === globalComponents[compoName]);
 
    // 该组件 originalWxComponentPath 可能是以全局方式引入有可能是在json文件中声明引用
    // 甚至可能是两种方式都存在,只是 tag 不一样
    (pageOrComponentPaths || []).forEach(jsonFilePath => {
      const jsonFileInfo = emitFileMap.get(jsonFilePath);
 
      // 以全局方式引入该组件
      if (componentTagInGlobal) {
        delete globalComponents[componentTagInGlobal]; // 从全局组件配置中删除
        jsonFileInfo.usingComponents[componentTagInGlobal] = newComponentPath; // 更新当前引用路径为分包路径
      }
 
      // 以json文件声明方式引入,则需要更新json文件声明的路径
      const usingComponents = jsonFileInfo.usingComponents;
      const componentTagInPage = Object.keys(usingComponents).find(compoName => originalWxComponentPath === usingComponents[compoName]);
      if (componentTagInPage) {
        jsonFileInfo.usingComponents[componentTagInPage] = newComponentPath;
      }
 
      if (componentTagInPage || componentTagInGlobal) {
        const replaceInfo = `jsonFilePath: ${jsonFilePath}, originalWxComponentPath: ${originalWxComponentPath}, newComponentPath: ${newComponentPath}`;
        // console.log(`replace componentPath used only by normal package, ${replaceInfo}`);
      }
 
    });
  }
  return { copyForNormal: copyComponentsForNormalPkgMap, copyForMain: { [mainPkgName]: copyComponentsForMainPkg } };
}
 
module.exports = analyzeGoDirection;