jsonFile.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.findMiniProgramUsingComponents = exports.isMiniProgramUsingComponent = exports.addMiniProgramUsingComponents = exports.addMiniProgramComponentJson = exports.addMiniProgramPageJson = exports.addMiniProgramAppJson = exports.findChangedJsonFiles = exports.normalizeJsonFilename = exports.findUsingComponents = exports.findJsonFile = exports.getComponentJsonFilenames = exports.hasJsonFile = exports.isMiniProgramPageSfcFile = exports.isMiniProgramPageFile = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const shared_1 = require("@vue/shared");
  9. const utils_1 = require("../../utils");
  10. const resolve_1 = require("../../resolve");
  11. const utils_2 = require("../../vue/utils");
  12. let appJsonCache = {};
  13. const jsonFilesCache = new Map();
  14. const jsonPagesCache = new Map();
  15. const jsonComponentsCache = new Map();
  16. const jsonUsingComponentsCache = new Map();
  17. function isMiniProgramPageFile(file, inputDir) {
  18. if (inputDir && path_1.default.isAbsolute(file)) {
  19. file = (0, utils_1.normalizePath)(path_1.default.relative(inputDir, file));
  20. }
  21. return jsonPagesCache.has((0, utils_1.removeExt)(file));
  22. }
  23. exports.isMiniProgramPageFile = isMiniProgramPageFile;
  24. function isMiniProgramPageSfcFile(file, inputDir) {
  25. return (0, utils_2.isVueSfcFile)(file) && isMiniProgramPageFile(file, inputDir);
  26. }
  27. exports.isMiniProgramPageSfcFile = isMiniProgramPageSfcFile;
  28. function hasJsonFile(filename) {
  29. return (filename === 'app' ||
  30. jsonPagesCache.has(filename) ||
  31. jsonComponentsCache.has(filename));
  32. }
  33. exports.hasJsonFile = hasJsonFile;
  34. function getComponentJsonFilenames() {
  35. return [...jsonComponentsCache.keys()];
  36. }
  37. exports.getComponentJsonFilenames = getComponentJsonFilenames;
  38. function findJsonFile(filename) {
  39. if (filename === 'app') {
  40. return appJsonCache;
  41. }
  42. return jsonPagesCache.get(filename) || jsonComponentsCache.get(filename);
  43. }
  44. exports.findJsonFile = findJsonFile;
  45. function findUsingComponents(filename) {
  46. return jsonUsingComponentsCache.get(filename);
  47. }
  48. exports.findUsingComponents = findUsingComponents;
  49. function normalizeJsonFilename(filename) {
  50. return (0, utils_1.normalizeNodeModules)(filename);
  51. }
  52. exports.normalizeJsonFilename = normalizeJsonFilename;
  53. function findChangedJsonFiles(supportGlobalUsingComponents = true) {
  54. const changedJsonFiles = new Map();
  55. function findChangedFile(filename, json) {
  56. const newJson = JSON.parse(JSON.stringify(json));
  57. if (!newJson.usingComponents) {
  58. newJson.usingComponents = {};
  59. }
  60. (0, shared_1.extend)(newJson.usingComponents, jsonUsingComponentsCache.get(filename));
  61. // 格式化为相对路径,这样作为分包也可以直接运行
  62. // app.json mp-baidu 在 win 不支持相对路径。所有平台改用绝对路径
  63. if (filename !== 'app') {
  64. let usingComponents = newJson.usingComponents;
  65. // 如果小程序不支持 global 的 usingComponents
  66. if (!supportGlobalUsingComponents) {
  67. // 从取全局的 usingComponents 并补充到子组件 usingComponents 中
  68. const globalUsingComponents = appJsonCache?.usingComponents || {};
  69. const globalComponents = findUsingComponents('app') || {};
  70. usingComponents = {
  71. ...globalUsingComponents,
  72. ...globalComponents,
  73. ...newJson.usingComponents,
  74. };
  75. }
  76. Object.keys(usingComponents).forEach((name) => {
  77. const componentFilename = usingComponents[name];
  78. if (componentFilename.startsWith('/')) {
  79. usingComponents[name] = (0, resolve_1.relativeFile)(filename, componentFilename.slice(1));
  80. }
  81. });
  82. newJson.usingComponents = usingComponents;
  83. }
  84. const jsonStr = JSON.stringify(newJson, null, 2);
  85. if (jsonFilesCache.get(filename) !== jsonStr) {
  86. changedJsonFiles.set(filename, jsonStr);
  87. jsonFilesCache.set(filename, jsonStr);
  88. }
  89. }
  90. function findChangedFiles(jsonsCache) {
  91. for (const name of jsonsCache.keys()) {
  92. findChangedFile(name, jsonsCache.get(name));
  93. }
  94. }
  95. findChangedFile('app', appJsonCache);
  96. findChangedFiles(jsonPagesCache);
  97. findChangedFiles(jsonComponentsCache);
  98. return changedJsonFiles;
  99. }
  100. exports.findChangedJsonFiles = findChangedJsonFiles;
  101. function addMiniProgramAppJson(appJson) {
  102. appJsonCache = appJson;
  103. }
  104. exports.addMiniProgramAppJson = addMiniProgramAppJson;
  105. function addMiniProgramPageJson(filename, json) {
  106. jsonPagesCache.set(filename, json);
  107. }
  108. exports.addMiniProgramPageJson = addMiniProgramPageJson;
  109. function addMiniProgramComponentJson(filename, json) {
  110. jsonComponentsCache.set(filename, json);
  111. }
  112. exports.addMiniProgramComponentJson = addMiniProgramComponentJson;
  113. function addMiniProgramUsingComponents(filename, json) {
  114. jsonUsingComponentsCache.set(filename, json);
  115. }
  116. exports.addMiniProgramUsingComponents = addMiniProgramUsingComponents;
  117. function isMiniProgramUsingComponent(name, options) {
  118. return !!findMiniProgramUsingComponents(options)[name];
  119. }
  120. exports.isMiniProgramUsingComponent = isMiniProgramUsingComponent;
  121. function findMiniProgramUsingComponents({ filename, inputDir, componentsDir, }) {
  122. const globalUsingComponents = appJsonCache && appJsonCache.usingComponents;
  123. const miniProgramComponents = {};
  124. if (globalUsingComponents) {
  125. (0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(globalUsingComponents, componentsDir));
  126. }
  127. const jsonFile = findJsonFile((0, utils_1.removeExt)((0, utils_1.normalizeMiniProgramFilename)(filename, inputDir)));
  128. if (jsonFile) {
  129. if (jsonFile.usingComponents) {
  130. (0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(jsonFile.usingComponents, componentsDir));
  131. }
  132. // mp-baidu 特有
  133. if (jsonFile.usingSwanComponents) {
  134. (0, shared_1.extend)(miniProgramComponents, findMiniProgramUsingComponent(jsonFile.usingSwanComponents, componentsDir));
  135. }
  136. }
  137. return miniProgramComponents;
  138. }
  139. exports.findMiniProgramUsingComponents = findMiniProgramUsingComponents;
  140. function findMiniProgramUsingComponent(usingComponents, componentsDir) {
  141. return Object.keys(usingComponents).reduce((res, name) => {
  142. const path = usingComponents[name];
  143. if (path.includes('plugin://')) {
  144. // mp-weixin & mp-alipay
  145. res[name] = 'plugin';
  146. }
  147. else if (path.includes('dynamicLib://')) {
  148. // mp-baidu
  149. res[name] = 'dynamicLib';
  150. }
  151. else if (path.includes('ext://')) {
  152. // mp-toutiao
  153. res[name] = 'ext';
  154. }
  155. else if (componentsDir && path.includes(componentsDir + '/')) {
  156. res[name] = 'component';
  157. }
  158. return res;
  159. }, {});
  160. }