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

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,31 @@ 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];
67
69
  });
68
70
  var href = compile(route.route, {
69
71
  validate: true
70
- })(query);
72
+ })(query) + hash;
71
73
  return url.params(href, restQuery);
72
74
  }
73
75
 
74
- return url.params(route.route, query);
76
+ return url.params(route.route + hash, query);
75
77
  }
76
78
 
77
79
  return '';
@@ -85,17 +87,16 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
85
87
  }, {
86
88
  key: "matchPageByPath",
87
89
  value: function matchPageByPath(path) {
88
- var depth = this.slashDepth(path);
89
- var routeList = this.$pathMap[depth];
90
+ var route = this.$pathMap.find(function (route) {
91
+ return route.path === path;
92
+ });
90
93
 
91
- if (!routeList || routeList.length < 0) {
94
+ if (!route) {
92
95
  console.warn('Not match route by:', path);
93
96
  return undefined;
94
97
  }
95
98
 
96
- return routeList.find(function (route) {
97
- return route.path === path;
98
- });
99
+ return route;
99
100
  }
100
101
  }, {
101
102
  key: "registryPages",
@@ -106,30 +107,26 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
106
107
  tabBar = params.tabBar,
107
108
  _params$subpackages = params.subpackages,
108
109
  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({
110
+ var tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
111
+ subpackages.forEach(function (subPackage) {
112
+ var root = subPackage.root;
113
+ subPackage.pages.forEach(function (page) {
114
+ _this2.addSubPackagePage({
114
115
  root: root,
115
- route: page.route,
116
+ route: page.route || page.path,
116
117
  path: page.path
117
118
  });
118
119
  });
119
120
  });
120
- routes.forEach(function (route) {
121
+ routes.forEach(function (r) {
121
122
  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;
123
+ return r.path === item.pagePath;
129
124
  });
125
+ var route = r.route || r.path;
130
126
 
131
- _this2.addPage(_objectSpread(_objectSpread({}, route), {}, {
132
- isTabBar: isTabBar
127
+ _this2.addPage(_objectSpread(_objectSpread({}, r), {}, {
128
+ isTabBar: isTabBar,
129
+ route: route
133
130
  }));
134
131
  });
135
132
  }
@@ -141,49 +138,39 @@ export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
141
138
  }, {
142
139
  key: "addPage",
143
140
  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);
141
+ this.$entityMap.push(route);
142
+ this.$pathMap.push(route);
157
143
  }
158
144
  }, {
159
- key: "addSubpackagePage",
160
- value: function addSubpackagePage(subpackgeRoute) {
161
- if (!this.$subpackageRoute[subpackgeRoute.root]) {
162
- this.$subpackageRoute[subpackgeRoute.root] = [];
145
+ key: "addSubPackagePage",
146
+ value: function addSubPackagePage(subPackageRoute) {
147
+ if (!this.$subPackageRoute[subPackageRoute.root]) {
148
+ this.$subPackageRoute[subPackageRoute.root] = [];
163
149
  }
164
150
 
165
- this.$subpackageRoute[subpackgeRoute.root].push(subpackgeRoute);
151
+ this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
166
152
  }
167
153
  }, {
168
- key: "getMatchedSubpackagePage",
169
- value: function getMatchedSubpackagePage(pathname, subpackage) {
170
- var subpackageRoute;
171
- this.$subpackageRoute[subpackage].forEach(function (item) {
154
+ key: "getMatchedSubPackagePage",
155
+ value: function getMatchedSubPackagePage(pathname, subPackage) {
156
+ var subs = this.$subPackageRoute[subPackage] || [];
157
+
158
+ for (var index = 0; index < subs.length; index++) {
159
+ var item = subs[index];
172
160
  var urlMatch = match(item.route, {
173
161
  decode: decodeURIComponent
174
162
  });
175
163
 
176
164
  if (urlMatch(pathname)) {
177
- subpackageRoute = {
165
+ return {
178
166
  pathname: pathname,
179
167
  route: item.route,
180
168
  params: urlMatch(pathname)['params'],
181
- subpackage: subpackage,
182
- path: '/' + subpackage + item.path
169
+ subPackage: subPackage,
170
+ path: slash('/' + subPackage + item.path)
183
171
  };
184
172
  }
185
- });
186
- return subpackageRoute;
173
+ }
187
174
  }
188
175
  }]);
189
176
 
package/lib/index.js CHANGED
@@ -1,14 +1,31 @@
1
+ import "core-js/modules/es.array.map.js";
1
2
  import { router } from './Router';
3
+ import { normalizeTabBar } from '@ray-js/framework-shared'; // import location from '@ray-js/location'
4
+ // import { url } from '@ray-js/library'
5
+
2
6
  export function createRouter(params) {
3
- var routes = params.routes,
4
- _params$tabBar = params.tabBar,
7
+ var _params$tabBar = params.tabBar,
5
8
  tabBar = _params$tabBar === void 0 ? {} : _params$tabBar,
6
9
  subpackages = params.subpackages;
10
+ var routes = params.routes.map(function (item) {
11
+ var _item$route;
12
+
13
+ item.route = (_item$route = item.route) !== null && _item$route !== void 0 ? _item$route : item.path;
14
+ return item;
15
+ });
16
+
17
+ var _tabBar = normalizeTabBar(tabBar, routes);
18
+
19
+ // console.log('>>>>>>>>> mini app config', finalCfg)
7
20
  router.scheduler.registryPages({
8
21
  routes: routes,
9
- tabBar: tabBar,
10
- subpackages: subpackages
22
+ subpackages: subpackages,
23
+ tabBar: _tabBar
11
24
  });
25
+ global.router = router; // router.parse = url.parse
26
+ // router.format = url.format
27
+ // console.log('>>>>>>>>>.location0', location)
28
+
12
29
  return router;
13
30
  }
14
31
  export default router;
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-1",
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-1",
24
+ "@ray-js/env": "^0.6.22-beta-1",
25
+ "@ray-js/library": "^0.6.22-beta-1",
26
+ "@ray-js/types": "^0.6.22-beta-1",
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-1"
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": "2e905a1820d0adfc0a6ca8cf3ecf1f3a584a0d44",
40
41
  "repository": {}
41
42
  }