uni-read-pages-v3.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true,
  4. });
  5. const fs = require('fs');
  6. import stripJsonComments from './strip-json-comments';
  7. import { isArray, isEmpty } from 'lodash';
  8. class TransformPages {
  9. constructor({ includes, pagesJsonDir }) {
  10. this.includes = includes;
  11. this.uniPagesJSON = JSON.parse(stripJsonComments(fs.readFileSync(pagesJsonDir, 'utf-8')));
  12. this.routes = this.getPagesRoutes().concat(this.getSubPackagesRoutes());
  13. this.tabbar = this.getTabbarRoutes();
  14. this.routesMap = this.transformPathToKey(this.routes);
  15. }
  16. /**
  17. * 通过读取pages.json文件 生成直接可用的routes
  18. */
  19. getPagesRoutes(pages = this.uniPagesJSON.pages, rootPath = null) {
  20. let routes = [];
  21. for (let i = 0; i < pages.length; i++) {
  22. const item = pages[i];
  23. let route = {};
  24. for (let j = 0; j < this.includes.length; j++) {
  25. const key = this.includes[j];
  26. let value = item[key];
  27. if (key === 'path') {
  28. value = rootPath ? `/${rootPath}/${value}` : `/${value}`;
  29. }
  30. if (key === 'aliasPath' && i == 0 && rootPath == null) {
  31. route[key] = route[key] || '/';
  32. } else if (value !== undefined) {
  33. route[key] = value;
  34. }
  35. }
  36. routes.push(route);
  37. }
  38. return routes;
  39. }
  40. /**
  41. * 解析小程序分包路径
  42. */
  43. getSubPackagesRoutes() {
  44. if (!(this.uniPagesJSON && this.uniPagesJSON.subPackages)) {
  45. return [];
  46. }
  47. const subPackages = this.uniPagesJSON.subPackages;
  48. let routes = [];
  49. for (let i = 0; i < subPackages.length; i++) {
  50. const subPages = subPackages[i].pages;
  51. const root = subPackages[i].root;
  52. const subRoutes = this.getPagesRoutes(subPages, root);
  53. routes = routes.concat(subRoutes);
  54. }
  55. return routes;
  56. }
  57. getTabbarRoutes() {
  58. if (!(this.uniPagesJSON && this.uniPagesJSON.tabBar && this.uniPagesJSON.tabBar.list)) {
  59. return [];
  60. }
  61. const tabbar = this.uniPagesJSON.tabBar.list;
  62. let tabbarMap = [];
  63. tabbar.forEach((bar) => {
  64. tabbarMap.push('/' + bar.pagePath);
  65. });
  66. return tabbarMap;
  67. }
  68. transformPathToKey(list) {
  69. if (!isArray(list) || isEmpty(list)) {
  70. return [];
  71. }
  72. let map = {};
  73. list.forEach((i) => {
  74. map[i.path] = i;
  75. });
  76. return map;
  77. }
  78. }
  79. function uniReadPagesV3Plugin({ pagesJsonDir, includes }) {
  80. let defaultIncludes = ['path', 'aliasPath', 'name'];
  81. includes = [...defaultIncludes, ...includes];
  82. let pages = new TransformPages({
  83. pagesJsonDir,
  84. includes,
  85. });
  86. return {
  87. name: 'uni-read-pages-v3',
  88. config(config) {
  89. return {
  90. define: {
  91. ROUTES: pages.routes,
  92. ROUTES_MAP: pages.routesMap,
  93. TABBAR: pages.tabbar,
  94. },
  95. };
  96. },
  97. };
  98. }
  99. exports.default = uniReadPagesV3Plugin;