index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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.normalizeUniAppXAppConfig = exports.normalizeUniAppXAppPagesJson = exports.checkPagesJson = exports.parseUniXSplashScreen = exports.parseUniXFlexDirection = void 0;
  7. const fs_1 = __importDefault(require("fs"));
  8. const path_1 = __importDefault(require("path"));
  9. const shared_1 = require("@vue/shared");
  10. const jsonc_parser_1 = require("jsonc-parser");
  11. const json_1 = require("../json");
  12. const pages_1 = require("../pages");
  13. const utils_1 = require("../../utils");
  14. const uniRoutes_1 = require("../app/pages/uniRoutes");
  15. const uniConfig_1 = require("./uniConfig");
  16. const utils_2 = require("../../vite/plugins/vitejs/utils");
  17. const preprocess_1 = require("../../preprocess");
  18. var manifest_1 = require("./manifest");
  19. Object.defineProperty(exports, "parseUniXFlexDirection", { enumerable: true, get: function () { return manifest_1.parseUniXFlexDirection; } });
  20. Object.defineProperty(exports, "parseUniXSplashScreen", { enumerable: true, get: function () { return manifest_1.parseUniXSplashScreen; } });
  21. function checkPagesJson(jsonStr, inputDir) {
  22. if (!inputDir) {
  23. return false;
  24. }
  25. const errors = [];
  26. const root = (0, jsonc_parser_1.parseTree)(jsonStr, errors);
  27. if (!root) {
  28. if (errors.length) {
  29. for (const error of errors) {
  30. const { line, column } = (0, utils_2.offsetToLineColumn)(jsonStr, error.offset);
  31. throw {
  32. name: 'SyntaxError',
  33. code: error.error,
  34. message: (0, jsonc_parser_1.printParseErrorCode)(error.error),
  35. loc: {
  36. start: { line, column },
  37. },
  38. offsetStart: error.offset,
  39. offsetEnd: error.offset + error.length,
  40. };
  41. }
  42. }
  43. return false;
  44. }
  45. const pagePathNodes = walkNodes(findRootNode(root, ['pages']));
  46. findRootNode(root, ['subPackages', 'subpackages']).forEach((node) => {
  47. const subPackageRoot = findSubPackageRoot(node);
  48. if (subPackageRoot) {
  49. findRootNode(node, ['pages']).forEach((subNode) => {
  50. walkNodes(subNode.children ?? []).forEach((node) => {
  51. pagePathNodes.push({
  52. ...node,
  53. value: (0, utils_1.normalizePath)(path_1.default.join(subPackageRoot, node.value)),
  54. });
  55. });
  56. });
  57. }
  58. });
  59. if (pagePathNodes.length) {
  60. for (const node of pagePathNodes) {
  61. const pagePath = node.value ?? '';
  62. if (pageExistsWithCaseSync(path_1.default.join(inputDir, pagePath))) {
  63. continue;
  64. }
  65. const { line, column } = (0, utils_2.offsetToLineColumn)(jsonStr, node.offset);
  66. throw {
  67. name: 'CompilerError',
  68. code: 'CompilerError',
  69. message: `The page path "${pagePath}" does not exist`,
  70. loc: {
  71. start: { line, column },
  72. },
  73. offsetStart: node.offset,
  74. offsetEnd: node.offset + node.length,
  75. };
  76. }
  77. }
  78. return true;
  79. }
  80. exports.checkPagesJson = checkPagesJson;
  81. function pageExistsWithCaseSync(pagePath) {
  82. try {
  83. const files = fs_1.default.readdirSync(path_1.default.dirname(pagePath));
  84. const basename = path_1.default.basename(pagePath);
  85. const uvuePage = basename + '.uvue';
  86. const vuePage = basename + '.vue';
  87. return files.some((file) => file === uvuePage || file === vuePage);
  88. }
  89. catch (e) {
  90. return false;
  91. }
  92. }
  93. function findSubPackageRoot(node) {
  94. const child = node.children?.find((child) => child.type === 'property' &&
  95. child.children &&
  96. child.children.find((child) => child.type === 'string' && child.value === 'root'));
  97. if (child && child.children?.length === 2) {
  98. return child.children[1].value;
  99. }
  100. return '';
  101. }
  102. function findRootNode(node, property) {
  103. const { type, children } = node;
  104. if (type === 'object' && children) {
  105. const child = children.find((child) => child.type === 'property' &&
  106. child.children &&
  107. child.children.find((child) => child.type === 'string' && property.includes(child.value)));
  108. if (child) {
  109. const node = child.children.find((child) => child.type === 'array');
  110. return node?.children ?? [];
  111. }
  112. }
  113. return [];
  114. }
  115. function walkNodes(node) {
  116. const pagePathNodes = [];
  117. node.forEach((node) => walkNode(node, pagePathNodes));
  118. return pagePathNodes;
  119. }
  120. function walkNode(node, pagePathNodes) {
  121. const { type, children } = node;
  122. if (type === 'property' && children && children.length === 2) {
  123. const maybePagePathNode = children[0];
  124. const maybePagePathValueNode = children[1];
  125. if (maybePagePathNode.type === 'string' &&
  126. maybePagePathNode.value === 'path' &&
  127. maybePagePathValueNode.type === 'string' &&
  128. (0, shared_1.isString)(maybePagePathValueNode.value)) {
  129. pagePathNodes.push(maybePagePathValueNode);
  130. }
  131. }
  132. if (children) {
  133. children.forEach((node) => walkNode(node, pagePathNodes));
  134. }
  135. }
  136. function normalizeUniAppXAppPagesJson(jsonStr) {
  137. // 先条件编译
  138. jsonStr = (0, preprocess_1.preUVueJson)(jsonStr);
  139. checkPagesJson(jsonStr, process.env.UNI_INPUT_DIR);
  140. const pagesJson = {
  141. pages: [],
  142. globalStyle: {},
  143. };
  144. let userPagesJson = {
  145. pages: [],
  146. globalStyle: {},
  147. };
  148. try {
  149. // 此处不需要条件编译了
  150. userPagesJson = (0, json_1.parseJson)(jsonStr, false);
  151. }
  152. catch (e) {
  153. console.error(`[vite] Error: pages.json parse failed.\n`, jsonStr, e);
  154. }
  155. // pages
  156. (0, pages_1.validatePages)(userPagesJson, jsonStr);
  157. userPagesJson.subPackages =
  158. userPagesJson.subPackages || userPagesJson.subpackages;
  159. // subPackages
  160. if (userPagesJson.subPackages) {
  161. userPagesJson.pages.push(...normalizeSubPackages(userPagesJson.subPackages));
  162. }
  163. pagesJson.pages = userPagesJson.pages;
  164. // pageStyle
  165. normalizePages(pagesJson.pages);
  166. // globalStyle
  167. pagesJson.globalStyle = normalizePageStyle(userPagesJson.globalStyle);
  168. // tabBar
  169. if (userPagesJson.tabBar) {
  170. pagesJson.tabBar = userPagesJson.tabBar;
  171. }
  172. // condition
  173. if (userPagesJson.condition) {
  174. pagesJson.condition = userPagesJson.condition;
  175. }
  176. // uniIdRouter
  177. if (userPagesJson.uniIdRouter) {
  178. pagesJson.uniIdRouter = userPagesJson.uniIdRouter;
  179. }
  180. // 是否应该用 process.env.UNI_UTS_PLATFORM
  181. (0, pages_1.filterPlatformPages)(process.env.UNI_PLATFORM, pagesJson);
  182. return pagesJson;
  183. }
  184. exports.normalizeUniAppXAppPagesJson = normalizeUniAppXAppPagesJson;
  185. function normalizeSubPackages(subPackages) {
  186. const pages = [];
  187. if ((0, shared_1.isArray)(subPackages)) {
  188. subPackages.forEach(({ root, pages: subPages }) => {
  189. if (root && subPages.length) {
  190. subPages.forEach((subPage) => {
  191. subPage.path = (0, utils_1.normalizePath)(path_1.default.join(root, subPage.path));
  192. subPage.style = subPage.style;
  193. pages.push(subPage);
  194. });
  195. }
  196. });
  197. }
  198. return pages;
  199. }
  200. function normalizePages(pages) {
  201. pages.forEach((page) => {
  202. page.style = normalizePageStyle(page.style);
  203. });
  204. }
  205. function normalizePageStyle(pageStyle) {
  206. if (pageStyle) {
  207. (0, shared_1.extend)(pageStyle, pageStyle['app']);
  208. (0, pages_1.removePlatformStyle)(pageStyle);
  209. return pageStyle;
  210. }
  211. return {};
  212. }
  213. /**
  214. * TODO 应该闭包,通过globalThis赋值?
  215. * @param pagesJson
  216. * @param manifestJson
  217. * @returns
  218. */
  219. function normalizeUniAppXAppConfig(pagesJson, manifestJson) {
  220. return `const __uniConfig = ${(0, uniConfig_1.normalizeAppXUniConfig)(pagesJson, manifestJson)};
  221. const __uniRoutes = ${(0, uniRoutes_1.normalizeAppUniRoutes)(pagesJson)}.map(uniRoute=>(uniRoute.meta.route=uniRoute.path,__uniConfig.pages.push(uniRoute.path),uniRoute.path='/'+uniRoute.path,uniRoute));
  222. `;
  223. }
  224. exports.normalizeUniAppXAppConfig = normalizeUniAppXAppConfig;