@ray-js/router-mp 0.6.21 → 0.6.22-beta-2

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/lib/Router.d.ts CHANGED
@@ -11,7 +11,6 @@ export declare class Router implements TRouter {
11
11
  reload(): void;
12
12
  go(delta: number): void;
13
13
  back(): void;
14
- get hostHref(): string;
15
- get hostPath(): string;
14
+ get href(): string;
16
15
  }
17
16
  export declare const router: Router;
package/lib/Router.js CHANGED
@@ -1,15 +1,16 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
2
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
3
  import _createClass from "@babel/runtime/helpers/esm/createClass";
4
4
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
5
- import _regeneratorRuntime from "@babel/runtime/regenerator";
5
+ import "core-js/modules/es.object.to-string.js";
6
+ import "core-js/modules/es.promise.js";
7
+ import "core-js/modules/es.array.slice.js";
6
8
  import "core-js/modules/es.regexp.exec.js";
7
- import "core-js/modules/es.string.search.js";
8
9
  import "core-js/modules/es.string.replace.js";
9
10
  import { url } from '@ray-js/library';
10
11
  import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync } from '@ray-js/api';
11
- import { RouterScheduler } from './RouterScheduler';
12
12
  import { isThing } from '@ray-js/env';
13
+ import { RouterScheduler } from './RouterScheduler';
13
14
 
14
15
  function currentPage() {
15
16
  var pages = getCurrentPages();
@@ -41,66 +42,41 @@ export var Router = /*#__PURE__*/function () {
41
42
  /**
42
43
  * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
43
44
  */
44
- function () {
45
- var _normalizeRoute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
46
- var _url$parse, pathname, search, hash, matchedPage, pathUrl, _pathUrl;
47
-
48
- return _regeneratorRuntime.wrap(function (_context) {
49
- while (1) {
50
- switch (_context.prev = _context.next) {
51
- case 0:
52
- _url$parse = url.parse(params.to), pathname = _url$parse.pathname, search = _url$parse.search, hash = _url$parse.hash;
53
-
54
- if (params.subpackage) {
55
- matchedPage = this.scheduler.getMatchedSubpackagePage(pathname, params.subpackage);
56
-
57
- if (matchedPage) {
58
- pathUrl = url.format({
59
- pathname: matchedPage.path,
60
- search: search,
61
- hash: hash
62
- }); // params 的优先级高于 search 参数,此处进行覆盖处理
63
-
64
- matchedPage.path = url.params(pathUrl, matchedPage.params);
65
- }
66
- } else {
67
- matchedPage = this.scheduler.getMatchedPage(pathname);
68
-
69
- if (matchedPage) {
70
- // FIXME: 自主实现 tabBar UI 小程序体系下对 tabBar 页面不能传 query
71
- if (!matchedPage.isTabBar) {
72
- _pathUrl = url.format({
73
- pathname: matchedPage.path,
74
- search: search,
75
- hash: hash
76
- }); // params 的优先级高于 search 参数,此处进行覆盖处理
77
-
78
- matchedPage.path = url.params(_pathUrl, matchedPage.params);
79
- }
80
- } // FIXME: 智能小程序对路由 path 的跳转未支持绝对路径
81
-
82
-
83
- if (isThing) {
84
- matchedPage.path = matchedPage.path.replace(/^\//, '');
85
- }
86
- }
87
-
88
- return _context.abrupt("return", matchedPage);
89
-
90
- case 3:
91
- case "end":
92
- return _context.stop();
93
- }
94
- }
95
- }, _callee, this);
96
- }));
97
-
98
- function normalizeRoute() {
99
- return _normalizeRoute.apply(this, arguments);
45
+ function normalizeRoute(params) {
46
+ var _url$parse = url.parse(params.to),
47
+ pathname = _url$parse.pathname,
48
+ query = _url$parse.query,
49
+ hash = _url$parse.hash;
50
+
51
+ var subPackage = params.subpackage;
52
+ var matchedPage = subPackage ? this.scheduler.getMatchedSubPackagePage(pathname, subPackage) : this.scheduler.getMatchedPage(pathname);
53
+
54
+ if (!matchedPage) {
55
+ return Promise.reject(undefined);
56
+ }
57
+
58
+ var finalPath = matchedPage.path; // FIXME: tabBar.list里的页面不能有query,也不能有hash
59
+
60
+ if (!matchedPage.isTabBar) {
61
+ finalPath = url.format({
62
+ pathname: matchedPage.path,
63
+ query: _objectSpread(_objectSpread(_objectSpread({}, query), matchedPage.params), {}, {
64
+ // 模式匹配得到的参数,如/xxx/:id 可以匹配路径 /xxx/123 得参数id: 123
65
+ ____h_a_s_h____: hash.slice(1) // 小程序中不能传递hash参数,用query辅助传递
66
+
67
+ })
68
+ });
100
69
  }
70
+ /**
71
+ * 坑点之一
72
+ * 微信小程序: app.json 里的页面配置不能以`/`开头,wx.navigatorTo 却需要
73
+ * 涂药小程序: 都不能以`/`开头
74
+ */
101
75
 
102
- return normalizeRoute;
103
- }()
76
+
77
+ matchedPage.path = isThing ? finalPath.replace(/^\//, '') : finalPath;
78
+ return Promise.resolve(matchedPage);
79
+ }
104
80
  }, {
105
81
  key: "push",
106
82
  value: function push(to, options) {
@@ -163,23 +139,21 @@ export var Router = /*#__PURE__*/function () {
163
139
  } // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
164
140
 
165
141
  }, {
166
- key: "hostHref",
142
+ key: "href",
167
143
  get: function get() {
168
- var _url$parse2 = url.parse(this.hostPath),
144
+ var page = currentPage();
145
+ var path = url.params("/".concat(page.route), page.options);
146
+
147
+ var _url$parse2 = url.parse(path),
169
148
  pathname = _url$parse2.pathname,
170
- query = _url$parse2.query;
149
+ query = _url$parse2.query,
150
+ hash = _url$parse2.hash;
171
151
 
172
- var href = this.scheduler.getHrefByPath({
152
+ return this.scheduler.getHrefByPath({
173
153
  path: pathname,
174
- query: query
154
+ query: query,
155
+ hash: hash
175
156
  });
176
- return href;
177
- }
178
- }, {
179
- key: "hostPath",
180
- get: function get() {
181
- var page = currentPage();
182
- return url.params("/".concat(page.route), page.options);
183
157
  }
184
158
  }]);
185
159
 
@@ -1,8 +1,8 @@
1
1
  import { RouterScheduler as IRouterScheduler, Routes, TabBar, SubPackages } from '@ray-js/types';
2
- export declare type Route = Routes[0] & {
2
+ export declare type Route = Routes[number] & {
3
3
  isTabBar: boolean;
4
4
  };
5
- export declare type SubpackgeRoute = {
5
+ export declare type SubPackageRoute = {
6
6
  root: string;
7
7
  route: string;
8
8
  path: string;
@@ -11,16 +11,17 @@ export declare type SubpackgeRoute = {
11
11
  * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
12
12
  */
13
13
  export declare class RouterScheduler extends IRouterScheduler<Route> {
14
- $entityMap: Record<string, Route[]>;
14
+ $entityMap: Route[];
15
15
  private $pathMap;
16
- $subpackageRoute: Record<string, SubpackgeRoute[]>;
16
+ $subPackageRoute: Record<string, SubPackageRoute[]>;
17
17
  /**
18
18
  * 将小程序地址栏转化为 web 地址
19
19
  * @param path - 小程序页面地址
20
20
  */
21
- getHrefByPath(params: {
21
+ getHrefByPath(opts: {
22
22
  path: string;
23
23
  query: object;
24
+ hash: string;
24
25
  }): string;
25
26
  /**
26
27
  * 以小程序地址进行匹配页面
@@ -40,6 +41,12 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
40
41
  pathname: string;
41
42
  };
42
43
  addPage(route: Route): void;
43
- addSubpackagePage(subpackgeRoute: SubpackgeRoute): void;
44
- getMatchedSubpackagePage(pathname: string, subpackage: string): any;
44
+ addSubPackagePage(subPackageRoute: SubPackageRoute): void;
45
+ getMatchedSubPackagePage(pathname: string, subPackage: string): {
46
+ pathname: string;
47
+ route: string;
48
+ params: any;
49
+ subPackage: string;
50
+ path: string;
51
+ } | undefined;
45
52
  }
@@ -9,9 +9,9 @@ import "core-js/modules/es.array.concat.js";
9
9
  import "core-js/modules/es.object.to-string.js";
10
10
  import "core-js/modules/es.array.find.js";
11
11
  import "core-js/modules/web.dom-collections.for-each.js";
12
- import { compile, pathToRegexp } from 'path-to-regexp';
13
- import { match } from 'path-to-regexp';
12
+ import { match, compile, pathToRegexp } from 'path-to-regexp';
14
13
  import { url } from '@ray-js/library';
14
+ import slash from 'slash';
15
15
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
16
16
 
17
17
  /**
@@ -33,11 +33,11 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
33
33
 
34
34
  _this = _super.call.apply(_super, [this].concat(args));
35
35
 
36
- _defineProperty(_assertThisInitialized(_this), "$entityMap", {});
36
+ _defineProperty(_assertThisInitialized(_this), "$entityMap", []);
37
37
 
38
- _defineProperty(_assertThisInitialized(_this), "$pathMap", {});
38
+ _defineProperty(_assertThisInitialized(_this), "$pathMap", []);
39
39
 
40
- _defineProperty(_assertThisInitialized(_this), "$subpackageRoute", {});
40
+ _defineProperty(_assertThisInitialized(_this), "$subPackageRoute", {});
41
41
 
42
42
  return _this;
43
43
  }
@@ -49,29 +49,37 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
49
49
  * 将小程序地址栏转化为 web 地址
50
50
  * @param path - 小程序页面地址
51
51
  */
52
- function getHrefByPath(params) {
53
- var query = params.query,
54
- path = params.path;
52
+ function getHrefByPath(opts) {
53
+ var query = opts.query,
54
+ path = opts.path,
55
+ _opts$hash = opts.hash,
56
+ hash = _opts$hash === void 0 ? '' : _opts$hash;
55
57
  var route = this.matchPageByPath(path);
56
58
 
57
59
  if (route) {
58
60
  var keys = [];
59
61
  pathToRegexp(route.route, keys);
60
62
 
61
- var restQuery = _objectSpread({}, query);
62
-
63
63
  if (keys.length > 0) {
64
+ var restQuery = _objectSpread({}, query);
65
+
64
66
  keys.forEach(function (_ref) {
65
67
  var name = _ref.name;
66
68
  delete restQuery[name];
69
+ }); // 兼容小程序tab页,因tab页不能携带参数
70
+
71
+ keys.forEach(function (k) {
72
+ var _query$k$name;
73
+
74
+ return query[k.name] = (_query$k$name = query[k.name]) !== null && _query$k$name !== void 0 ? _query$k$name : '';
67
75
  });
68
76
  var href = compile(route.route, {
69
- validate: true
70
- })(query);
77
+ validate: false
78
+ })(query) + hash;
71
79
  return url.params(href, restQuery);
72
80
  }
73
81
 
74
- return url.params(route.route, query);
82
+ return url.params(route.route + hash, query);
75
83
  }
76
84
 
77
85
  return '';
@@ -85,17 +93,16 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
85
93
  }, {
86
94
  key: "matchPageByPath",
87
95
  value: function matchPageByPath(path) {
88
- var depth = this.slashDepth(path);
89
- var routeList = this.$pathMap[depth];
96
+ var route = this.$pathMap.find(function (route) {
97
+ return route.path === path;
98
+ });
90
99
 
91
- if (!routeList || routeList.length < 0) {
100
+ if (!route) {
92
101
  console.warn('Not match route by:', path);
93
102
  return undefined;
94
103
  }
95
104
 
96
- return routeList.find(function (route) {
97
- return route.path === path;
98
- });
105
+ return route;
99
106
  }
100
107
  }, {
101
108
  key: "registryPages",
@@ -106,30 +113,26 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
106
113
  tabBar = params.tabBar,
107
114
  _params$subpackages = params.subpackages,
108
115
  subpackages = _params$subpackages === void 0 ? [] : _params$subpackages;
109
- var tabBarList = tabBar.list || [];
110
- subpackages.forEach(function (subpackage) {
111
- var root = subpackage.root;
112
- subpackage.pages.forEach(function (page) {
113
- _this2.addSubpackagePage({
116
+ var tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
117
+ subpackages.forEach(function (subPackage) {
118
+ var root = subPackage.root;
119
+ subPackage.pages.forEach(function (page) {
120
+ _this2.addSubPackagePage({
114
121
  root: root,
115
- route: page.route,
122
+ route: page.route || page.path,
116
123
  path: page.path
117
124
  });
118
125
  });
119
126
  });
120
- routes.forEach(function (route) {
127
+ routes.forEach(function (r) {
121
128
  var isTabBar = tabBarList.some(function (item) {
122
- if (item.route) {
123
- return route.route === item.route;
124
- } else if (item.id) {
125
- return route.id === item.id;
126
- }
127
-
128
- return false;
129
+ return r.path === item.pagePath;
129
130
  });
131
+ var route = r.route || r.path;
130
132
 
131
- _this2.addPage(_objectSpread(_objectSpread({}, route), {}, {
132
- isTabBar: isTabBar
133
+ _this2.addPage(_objectSpread(_objectSpread({}, r), {}, {
134
+ isTabBar: isTabBar,
135
+ route: route
133
136
  }));
134
137
  });
135
138
  }
@@ -141,49 +144,39 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
141
144
  }, {
142
145
  key: "addPage",
143
146
  value: function addPage(route) {
144
- var routeDepth = this.slashDepth(route.route);
145
- var pathDepth = this.slashDepth(route.path);
146
-
147
- if (!this.$entityMap[routeDepth]) {
148
- this.$entityMap[routeDepth] = [];
149
- }
150
-
151
- if (!this.$pathMap[pathDepth]) {
152
- this.$pathMap[pathDepth] = [];
153
- }
154
-
155
- this.$entityMap[routeDepth].push(route);
156
- this.$pathMap[pathDepth].push(route);
147
+ this.$entityMap.push(route);
148
+ this.$pathMap.push(route);
157
149
  }
158
150
  }, {
159
- key: "addSubpackagePage",
160
- value: function addSubpackagePage(subpackgeRoute) {
161
- if (!this.$subpackageRoute[subpackgeRoute.root]) {
162
- this.$subpackageRoute[subpackgeRoute.root] = [];
151
+ key: "addSubPackagePage",
152
+ value: function addSubPackagePage(subPackageRoute) {
153
+ if (!this.$subPackageRoute[subPackageRoute.root]) {
154
+ this.$subPackageRoute[subPackageRoute.root] = [];
163
155
  }
164
156
 
165
- this.$subpackageRoute[subpackgeRoute.root].push(subpackgeRoute);
157
+ this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
166
158
  }
167
159
  }, {
168
- key: "getMatchedSubpackagePage",
169
- value: function getMatchedSubpackagePage(pathname, subpackage) {
170
- var subpackageRoute;
171
- this.$subpackageRoute[subpackage].forEach(function (item) {
160
+ key: "getMatchedSubPackagePage",
161
+ value: function getMatchedSubPackagePage(pathname, subPackage) {
162
+ var subs = this.$subPackageRoute[subPackage] || [];
163
+
164
+ for (var index = 0; index < subs.length; index++) {
165
+ var item = subs[index];
172
166
  var urlMatch = match(item.route, {
173
167
  decode: decodeURIComponent
174
168
  });
175
169
 
176
170
  if (urlMatch(pathname)) {
177
- subpackageRoute = {
171
+ return {
178
172
  pathname: pathname,
179
173
  route: item.route,
180
174
  params: urlMatch(pathname)['params'],
181
- subpackage: subpackage,
182
- path: '/' + subpackage + item.path
175
+ subPackage: subPackage,
176
+ path: slash('/' + subPackage + item.path)
183
177
  };
184
178
  }
185
- });
186
- return subpackageRoute;
179
+ }
187
180
  }
188
181
  }]);
189
182
 
package/lib/index.js CHANGED
@@ -1,13 +1,23 @@
1
+ import "core-js/modules/es.array.map.js";
1
2
  import { router } from './Router';
3
+ import { normalizeTabBar } from '@ray-js/framework-shared';
2
4
  export function createRouter(params) {
3
- var routes = params.routes,
4
- _params$tabBar = params.tabBar,
5
+ var _params$tabBar = params.tabBar,
5
6
  tabBar = _params$tabBar === void 0 ? {} : _params$tabBar,
6
7
  subpackages = params.subpackages;
8
+ var routes = params.routes.map(function (item) {
9
+ var _item$route;
10
+
11
+ item.route = (_item$route = item.route) !== null && _item$route !== void 0 ? _item$route : item.path;
12
+ return item;
13
+ });
14
+
15
+ var _tabBar = normalizeTabBar(tabBar, routes);
16
+
7
17
  router.scheduler.registryPages({
8
18
  routes: routes,
9
- tabBar: tabBar,
10
- subpackages: subpackages
19
+ subpackages: subpackages,
20
+ tabBar: _tabBar
11
21
  });
12
22
  return router;
13
23
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router-mp",
3
- "version": "0.6.21",
3
+ "version": "0.6.22-beta-2",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
@@ -20,15 +20,16 @@
20
20
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@ray-js/api": "^0.6.21",
24
- "@ray-js/env": "^0.6.21",
25
- "@ray-js/library": "^0.6.21",
26
- "@ray-js/types": "^0.6.21",
23
+ "@ray-js/api": "^0.6.22-beta-2",
24
+ "@ray-js/env": "^0.6.22-beta-2",
25
+ "@ray-js/library": "^0.6.22-beta-2",
26
+ "@ray-js/types": "^0.6.22-beta-2",
27
27
  "@react-navigation/core": "^6.1.0",
28
- "path-to-regexp": "^6.2.0"
28
+ "path-to-regexp": "^6.2.0",
29
+ "slash": "^5.0.0"
29
30
  },
30
31
  "devDependencies": {
31
- "@ray-js/cli": "^0.6.21"
32
+ "@ray-js/cli": "^0.6.22-beta-2"
32
33
  },
33
34
  "maintainers": [
34
35
  {
@@ -36,6 +37,6 @@
36
37
  "email": "tuyafe@tuya.com"
37
38
  }
38
39
  ],
39
- "gitHead": "cbb81571d068e6997e9b1fe95198c1b0275112e7",
40
+ "gitHead": "5d621bf1561047175b967cdf3aa35446f7d7198a",
40
41
  "repository": {}
41
42
  }