@ray-js/router-mp 1.4.0-alpha.1 → 1.4.0-alpha.11

Sign up to get free protection for your applications and to get access to all the features.
package/lib/Router.d.ts CHANGED
@@ -2,10 +2,18 @@ import { Router as TRouter, RouteOptions } from '@ray-js/types';
2
2
  import { RouterScheduler } from './RouterScheduler';
3
3
  export declare class Router implements TRouter {
4
4
  private urlPrefix;
5
+ /**
6
+ * 功能页模式为 functional,使用相对路径模式
7
+ * 如果是绝对路径,表示跳转到宿主小程序
8
+ *
9
+ * @private
10
+ */
11
+ private mode;
5
12
  /**
6
13
  * setUrlPrefix
7
14
  */
8
15
  setUrlPrefix(prefix: string): void;
16
+ setMode(mode: string): void;
9
17
  scheduler: RouterScheduler;
10
18
  /**
11
19
  * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
package/lib/Router.js CHANGED
@@ -1,192 +1,192 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/esm/createClass";
4
2
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
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";
8
- import "core-js/modules/es.regexp.exec.js";
9
3
  import "core-js/modules/es.string.replace.js";
10
- import "core-js/modules/es.regexp.constructor.js";
11
- import "core-js/modules/es.regexp.dot-all.js";
12
- import "core-js/modules/es.regexp.sticky.js";
13
- import "core-js/modules/es.regexp.to-string.js";
14
4
  import { url } from '@ray-js/library';
15
- import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync } from '@ray-js/api';
16
- import { isThing } from '@ray-js/env';
17
- import { RouterScheduler } from './RouterScheduler';
18
-
19
- function currentPage() {
20
- var pages = getCurrentPages();
21
-
22
- if (pages.length > 0) {
23
- return pages[pages.length - 1];
24
- } else {
25
- var _getLaunchOptionsSync = getLaunchOptionsSync(),
26
- path = _getLaunchOptionsSync.path,
27
- query = _getLaunchOptionsSync.query;
28
-
29
- return {
30
- route: path,
31
- options: query
32
- };
5
+ import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo } from '@ray-js/api';
6
+ import { RouterScheduler, currentPage } from './RouterScheduler';
7
+ function pathRelative(fromPath, toPath) {
8
+ const from = fromPath.split('/');
9
+ const to = toPath.split('/');
10
+ let i = 0;
11
+
12
+ // 找到两个路径第一个不同的目录
13
+ while (from[i] === to[i]) {
14
+ i++;
33
15
  }
34
- }
16
+ let result = '';
35
17
 
36
- export var Router = /*#__PURE__*/function () {
37
- function Router() {
38
- _classCallCheck(this, Router);
39
-
40
- _defineProperty(this, "urlPrefix", '');
41
-
42
- _defineProperty(this, "scheduler", new RouterScheduler());
18
+ // from 中添加 '..' 直到达到相同的目录
19
+ for (let j = i; j < from.length - 1; j++) {
20
+ result += '../';
43
21
  }
44
22
 
45
- _createClass(Router, [{
46
- key: "setUrlPrefix",
47
- value:
48
- /**
49
- * setUrlPrefix
50
- */
51
- function setUrlPrefix(prefix) {
52
- this.urlPrefix = prefix;
23
+ // 在 result 中添加 to 中剩余的目录
24
+ for (let j = i; j < to.length; j++) {
25
+ result += to[j];
26
+ if (j < to.length - 1) {
27
+ result += '/';
53
28
  }
54
- }, {
55
- key: "normalizeRoute",
56
- value:
29
+ }
30
+ return result;
31
+ }
32
+ export class Router {
33
+ constructor() {
34
+ // 只有作为其他小程序的子包时,才设置urlPrefix
35
+ _defineProperty(this, "urlPrefix", '');
57
36
  /**
58
- * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
37
+ * 功能页模式为 functional,使用相对路径模式
38
+ * 如果是绝对路径,表示跳转到宿主小程序
39
+ *
40
+ * @private
59
41
  */
60
- function normalizeRoute(params) {
61
- var _url$parse = url.parse(params.to),
62
- pathname = _url$parse.pathname,
63
- query = _url$parse.query,
64
- hash = _url$parse.hash;
65
-
66
- var subPackage = params.subpackage;
67
- var matchedPage = subPackage ? this.scheduler.getMatchedSubPackagePage(pathname, subPackage) : this.scheduler.getMatchedPage(pathname);
68
-
69
- if (!matchedPage) {
70
- return Promise.reject(undefined);
71
- } // 作为其他小程序时不能作为tabBar
72
-
73
-
74
- if (this.urlPrefix) {
75
- matchedPage.isTabBar = false;
76
- }
77
-
78
- var finalPath = matchedPage.path; // FIXME: tabBar.list里的页面不能有query,也不能有hash
79
-
80
- if (!matchedPage.isTabBar) {
81
- finalPath = url.format({
82
- pathname: matchedPage.path,
83
- query: _objectSpread(_objectSpread(_objectSpread({}, query), matchedPage.params), {}, {
84
- // 模式匹配得到的参数,如/xxx/:id 可以匹配路径 /xxx/123 得参数id: 123
85
- ____h_a_s_h____: hash.slice(1) // 小程序中不能传递hash参数,用query辅助传递
86
-
87
- })
88
- });
89
- }
90
- /**
91
- * 坑点之一
92
- * 微信小程序: app.json 里的页面配置不能以`/`开头,wx.navigatorTo 却需要
93
- * 涂鸦小程序: 都不能以`/`开头
94
- */
95
-
96
-
97
- if (this.urlPrefix) {
98
- finalPath = ('/' + this.urlPrefix + '/' + finalPath).replace(/\/+/g, '/');
99
- }
100
-
101
- if (isThing) {
102
- finalPath = finalPath.replace(/^\//, '');
42
+ _defineProperty(this, "mode", 'miniprogram');
43
+ _defineProperty(this, "scheduler", new RouterScheduler());
44
+ }
45
+ // functional
46
+ /**
47
+ * setUrlPrefix
48
+ */
49
+ setUrlPrefix(prefix) {
50
+ this.urlPrefix = prefix;
51
+ }
52
+ setMode(mode) {
53
+ this.mode = mode;
54
+ }
55
+ /**
56
+ * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
57
+ */
58
+ normalizeRoute(params) {
59
+ let {
60
+ pathname,
61
+ query,
62
+ hash
63
+ } = url.parse(params.to);
64
+ const subPackage = params.subpackage;
65
+ const currentRoute = this.scheduler.getCurrentRoute();
66
+ const isRelativePath = pathname.startsWith('.');
67
+ if (isRelativePath) {
68
+ const currentPathname = currentRoute.route;
69
+ // 如果是相对路径,修正为绝对路径
70
+ const absolutePathSegments = (currentPathname + '/' + pathname).split('/');
71
+ const normalizedPathSegments = [];
72
+ for (let i = 0; i < absolutePathSegments.length; i++) {
73
+ const segment = absolutePathSegments[i];
74
+ if (segment === '..') {
75
+ normalizedPathSegments.pop();
76
+ } else if (segment !== '.') {
77
+ normalizedPathSegments.push(segment);
78
+ }
103
79
  }
104
-
105
- matchedPage.path = finalPath;
106
- return Promise.resolve(matchedPage);
80
+ pathname = normalizedPathSegments.join('/');
107
81
  }
108
- }, {
109
- key: "push",
110
- value: function push(to, options) {
111
- var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
112
- this.normalizeRoute({
113
- to: to,
114
- subpackage: subpackage
115
- }).then(function (route) {
116
- var path = route.path;
117
82
 
118
- if (route.isTabBar) {
119
- switchTab({
120
- url: path
121
- });
122
- } else {
123
- navigateTo({
124
- url: path
125
- });
126
- }
83
+ // 如果是相对路径,修正为绝对路径
84
+
85
+ const matchedPage = subPackage ? this.scheduler.getMatchedSubPackagePage(pathname, subPackage) : this.scheduler.getMatchedPage(pathname);
86
+ if (!matchedPage) {
87
+ console.warn('try match page', {
88
+ pathname,
89
+ query,
90
+ hash
127
91
  });
92
+ return Promise.reject('can not find page by path: ' + params.to);
128
93
  }
129
- }, {
130
- key: "replace",
131
- value: function replace(to, options) {
132
- var subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
133
- this.normalizeRoute({
134
- to: to,
135
- subpackage: subpackage
136
- }).then(function (route) {
137
- if (route.isTabBar) {
138
- switchTab({
139
- url: route.path
140
- });
141
- } else {
142
- redirectTo({
143
- url: route.path
144
- });
145
- }
146
- });
94
+ // 作为其他小程序时不能作为tabBar
95
+ if (this.urlPrefix) {
96
+ matchedPage.isTabBar = false;
147
97
  }
148
- }, {
149
- key: "reload",
150
- value: function reload() {
151
- var page = currentPage();
152
- reLaunch({
153
- url: url.params(page.route, page.options)
98
+ let finalPath = matchedPage.path;
99
+ // FIXME: tabBar.list里的页面不能有query,也不能有hash
100
+ if (!matchedPage.isTabBar) {
101
+ finalPath = url.format({
102
+ pathname: matchedPage.path,
103
+ query: _objectSpread(_objectSpread(_objectSpread({}, query), matchedPage.params), {}, {
104
+ // 模式匹配得到的参数,如/xxx/:id 可以匹配路径 /xxx/123 得参数id: 123
105
+ ____h_a_s_h____: hash.slice(1) // 小程序中不能传递hash参数,用query辅助传递
106
+ })
154
107
  });
155
108
  }
156
- }, {
157
- key: "go",
158
- value: function go() {
159
- throw new Error('Method not implemented.');
109
+ /**
110
+ * 坑点之一
111
+ * 微信小程序: app.json 里的页面配置不能以`/`开头,wx.navigatorTo 却需要
112
+ */
113
+ if (this.urlPrefix) {
114
+ finalPath = ('/' + this.urlPrefix + '/' + finalPath).replace(/\/+/g, '/');
160
115
  }
161
- }, {
162
- key: "back",
163
- value: function back() {
164
- navigateBack({
165
- delta: 1
166
- });
167
- } // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
168
-
169
- }, {
170
- key: "href",
171
- get: function get() {
172
- var page = currentPage();
173
- var reg = RegExp('^' + this.urlPrefix + '/');
174
- var r = page.route.replace(reg, '');
175
- var path = url.params("/".concat(r), page.options);
116
+ matchedPage.path = finalPath;
176
117
 
177
- var _url$parse2 = url.parse(path),
178
- pathname = _url$parse2.pathname,
179
- query = _url$parse2.query,
180
- hash = _url$parse2.hash;
181
-
182
- return this.scheduler.getHrefByPath({
183
- path: pathname,
184
- query: query,
185
- hash: hash
186
- });
118
+ // 因为可能是功能页模式,必须使用相对路径进行跳转
119
+ if (isRelativePath) {
120
+ // 将路径基于 currentRoute 进行绝对路径转成相对路径
121
+ matchedPage.path = pathRelative(currentRoute.path, matchedPage.path);
187
122
  }
188
- }]);
189
-
190
- return Router;
191
- }();
192
- export var router = new Router();
123
+ return Promise.resolve(matchedPage);
124
+ }
125
+ push(to, options) {
126
+ const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
127
+ this.normalizeRoute({
128
+ to,
129
+ subpackage
130
+ }).then(route => {
131
+ const path = route.path;
132
+ if (route.isTabBar) {
133
+ switchTab({
134
+ url: path
135
+ });
136
+ } else {
137
+ navigateTo({
138
+ url: path
139
+ });
140
+ }
141
+ });
142
+ }
143
+ replace(to, options) {
144
+ const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
145
+ this.normalizeRoute({
146
+ to,
147
+ subpackage
148
+ }).then(route => {
149
+ if (route.isTabBar) {
150
+ switchTab({
151
+ url: route.path
152
+ });
153
+ } else {
154
+ redirectTo({
155
+ url: route.path
156
+ });
157
+ }
158
+ });
159
+ }
160
+ reload() {
161
+ const page = currentPage();
162
+ reLaunch({
163
+ url: url.params(page.route, page.options)
164
+ });
165
+ }
166
+ go() {
167
+ throw new Error('Method not implemented.');
168
+ }
169
+ back() {
170
+ navigateBack({
171
+ delta: 1
172
+ });
173
+ }
174
+ // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
175
+ get href() {
176
+ const page = currentPage();
177
+ const reg = RegExp('^' + this.urlPrefix + '/');
178
+ const r = page.route.replace(reg, '');
179
+ const path = url.params("/".concat(r), page.options);
180
+ const {
181
+ pathname,
182
+ query,
183
+ hash
184
+ } = url.parse(path);
185
+ return this.scheduler.getHrefByPath({
186
+ path: pathname,
187
+ query,
188
+ hash
189
+ });
190
+ }
191
+ }
192
+ export const router = new Router();
@@ -1,34 +1,30 @@
1
- import { RouterScheduler as IRouterScheduler, Routes, TabBar, SubPackages } from '@ray-js/types';
2
- export declare type Route = Routes[number] & {
1
+ import { RouterScheduler as IRouterScheduler, Routes, SubPackages, TabBar } from '@ray-js/types';
2
+ export type Route = Routes[number] & {
3
3
  isTabBar: boolean;
4
4
  };
5
- export declare type SubPackageRoute = {
5
+ export type SubPackageRoute = {
6
6
  root: string;
7
7
  route: string;
8
8
  path: string;
9
9
  };
10
+ export declare function currentPage(): any;
10
11
  /**
11
12
  * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
12
13
  */
13
14
  export declare class RouterScheduler extends IRouterScheduler<Route> {
14
15
  $entityMap: Route[];
15
- private $pathMap;
16
16
  $subPackageRoute: Record<string, SubPackageRoute[]>;
17
+ private $pathMap;
18
+ getCurrentRoute(): Route | undefined;
17
19
  /**
18
20
  * 将小程序地址栏转化为 web 地址
19
- * @param path - 小程序页面地址
21
+ * @param opts
20
22
  */
21
23
  getHrefByPath(opts: {
22
24
  path: string;
23
- query: object;
25
+ query: Record<string, unknown>;
24
26
  hash: string;
25
27
  }): string;
26
- /**
27
- * 以小程序地址进行匹配页面
28
- * @param path - 小程序页面地址 /pages/home/index
29
- * @returns
30
- */
31
- private matchPageByPath;
32
28
  registryPages(params: {
33
29
  routes: Routes;
34
30
  tabBar: TabBar;
@@ -40,6 +36,7 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
40
36
  params: object;
41
37
  pathname: string;
42
38
  };
39
+ getRouteByPath(pagePath: string): Route | undefined;
43
40
  addPage(route: Route): void;
44
41
  addSubPackagePage(subPackageRoute: SubPackageRoute): void;
45
42
  getMatchedSubPackagePage(pathname: string, subPackage: string): {
@@ -49,4 +46,10 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
49
46
  subPackage: string;
50
47
  path: string;
51
48
  } | undefined;
49
+ /**
50
+ * 以小程序地址进行匹配页面
51
+ * @param path - 小程序页面地址 /pages/home/index
52
+ * @returns
53
+ */
54
+ private matchPageByPath;
52
55
  }
@@ -1,184 +1,162 @@
1
1
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/esm/createClass";
4
- import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
5
- import _inherits from "@babel/runtime/helpers/esm/inherits";
6
- import _createSuper from "@babel/runtime/helpers/esm/createSuper";
7
2
  import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
8
- import "core-js/modules/es.array.concat.js";
9
- import "core-js/modules/es.object.to-string.js";
10
- import "core-js/modules/es.array.find.js";
11
- import "core-js/modules/web.dom-collections.for-each.js";
12
- import { match, compile, pathToRegexp } from 'path-to-regexp';
3
+ import "core-js/modules/web.dom-collections.iterator.js";
4
+ import { compile, match, pathToRegexp } from 'path-to-regexp';
13
5
  import { url } from '@ray-js/library';
14
6
  import slash from 'slash';
15
7
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
8
+ import { getLaunchOptionsSync } from '@ray-js/api';
9
+ export function currentPage() {
10
+ const pages = getCurrentPages();
11
+ if (pages.length > 0) {
12
+ return pages[pages.length - 1];
13
+ } else {
14
+ const {
15
+ path,
16
+ query
17
+ } = getLaunchOptionsSync();
18
+ return {
19
+ route: path,
20
+ options: query
21
+ };
22
+ }
23
+ }
16
24
 
17
25
  /**
18
26
  * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
19
27
  */
20
- export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
21
- _inherits(RouterScheduler, _IRouterScheduler);
22
-
23
- var _super = _createSuper(RouterScheduler);
24
-
25
- function RouterScheduler() {
26
- var _this;
27
-
28
- _classCallCheck(this, RouterScheduler);
29
-
30
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
31
- args[_key] = arguments[_key];
32
- }
33
-
34
- _this = _super.call.apply(_super, [this].concat(args));
35
-
36
- _defineProperty(_assertThisInitialized(_this), "$entityMap", []);
37
-
38
- _defineProperty(_assertThisInitialized(_this), "$pathMap", []);
39
-
40
- _defineProperty(_assertThisInitialized(_this), "$subPackageRoute", {});
41
-
42
- return _this;
28
+ export class RouterScheduler extends IRouterScheduler {
29
+ constructor() {
30
+ super(...arguments);
31
+ _defineProperty(this, "$entityMap", []);
32
+ _defineProperty(this, "$subPackageRoute", {});
33
+ _defineProperty(this, "$pathMap", []);
43
34
  }
44
-
45
- _createClass(RouterScheduler, [{
46
- key: "getHrefByPath",
47
- value:
48
- /**
49
- * 将小程序地址栏转化为 web 地址
50
- * @param path - 小程序页面地址
51
- */
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;
57
- var route = this.matchPageByPath(path);
58
-
59
- if (route) {
60
- var keys = [];
61
- pathToRegexp(route.route, keys);
62
-
63
- if (keys.length > 0) {
64
- var restQuery = _objectSpread({}, query);
65
-
66
- keys.forEach(function (_ref) {
67
- var name = _ref.name;
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 : '';
75
- });
76
- var href = compile(route.route, {
77
- validate: false
78
- })(query) + hash;
79
- return url.params(href, restQuery);
80
- }
81
-
82
- return url.params(route.route + hash, query);
83
- }
84
-
85
- return '';
35
+ // 以小程序路径作为键存储关系
36
+
37
+ getCurrentRoute() {
38
+ const {
39
+ route,
40
+ __pureRoute__,
41
+ __scope__
42
+ } = currentPage();
43
+ let normalizedRoute = __pureRoute__ || route;
44
+ // 小程序页面对象上的 route 不是以 / 开头的,需要补上
45
+ if (!normalizedRoute.startsWith('/')) {
46
+ normalizedRoute = '/' + normalizedRoute;
86
47
  }
87
- /**
88
- * 以小程序地址进行匹配页面
89
- * @param path - 小程序页面地址 /pages/home/index
90
- * @returns
91
- */
92
-
93
- }, {
94
- key: "matchPageByPath",
95
- value: function matchPageByPath(path) {
96
- var route = this.$pathMap.find(function (route) {
97
- return route.path === path;
98
- });
48
+ const matchedRoute = this.getRouteByPath(normalizedRoute);
49
+ return matchedRoute;
50
+ }
99
51
 
100
- if (!route) {
101
- console.warn('Not match route by:', path);
102
- return undefined;
52
+ /**
53
+ * 将小程序地址栏转化为 web 地址
54
+ * @param opts
55
+ */
56
+ getHrefByPath(opts) {
57
+ const {
58
+ query,
59
+ path,
60
+ hash = ''
61
+ } = opts;
62
+ const route = this.matchPageByPath(path);
63
+ if (route) {
64
+ const keys = [];
65
+ pathToRegexp(route.route, keys);
66
+ if (keys.length > 0) {
67
+ const restQuery = _objectSpread({}, query);
68
+ keys.forEach(_ref => {
69
+ let {
70
+ name
71
+ } = _ref;
72
+ delete restQuery[name];
73
+ });
74
+ // 兼容小程序tab页,因tab页不能携带参数
75
+ keys.forEach(k => {
76
+ var _query$k$name;
77
+ return query[k.name] = (_query$k$name = query[k.name]) !== null && _query$k$name !== void 0 ? _query$k$name : '';
78
+ });
79
+ const href = compile(route.route, {
80
+ validate: false
81
+ })(query) + hash;
82
+ return url.params(href, restQuery);
103
83
  }
104
-
105
- return route;
84
+ return url.params(route.route + hash, query);
106
85
  }
107
- }, {
108
- key: "registryPages",
109
- value: function registryPages(params) {
110
- var _this2 = this;
111
-
112
- var routes = params.routes,
113
- tabBar = params.tabBar,
114
- _params$subpackages = params.subpackages,
115
- subpackages = _params$subpackages === void 0 ? [] : _params$subpackages;
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({
121
- root: root,
122
- route: page.route || page.path,
123
- path: page.path
124
- });
125
- });
126
- });
127
- routes.forEach(function (r) {
128
- var isTabBar = tabBarList.some(function (item) {
129
- return r.path === item.pagePath;
86
+ return '';
87
+ }
88
+ registryPages(params) {
89
+ const {
90
+ routes,
91
+ tabBar,
92
+ subpackages = []
93
+ } = params;
94
+ const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
95
+ subpackages.forEach(subPackage => {
96
+ const root = subPackage.root;
97
+ subPackage.pages.forEach(page => {
98
+ this.addSubPackagePage({
99
+ root,
100
+ route: page.route || page.path,
101
+ path: page.path
130
102
  });
131
- var route = r.route || r.path;
132
-
133
- _this2.addPage(_objectSpread(_objectSpread({}, r), {}, {
134
- isTabBar: isTabBar,
135
- route: route
136
- }));
137
103
  });
104
+ });
105
+ routes.forEach(r => {
106
+ const isTabBar = tabBarList.some(item => r.path === item.pagePath);
107
+ const route = r.route || r.path;
108
+ this.addPage(_objectSpread(_objectSpread({}, r), {}, {
109
+ isTabBar,
110
+ route
111
+ }));
112
+ });
113
+ }
114
+ getMatchedPage(pathname) {
115
+ return this.matchPageByPathname(pathname);
116
+ }
117
+ getRouteByPath(pagePath) {
118
+ return this.$pathMap.find(item => item.path === pagePath);
119
+ }
120
+ addPage(route) {
121
+ this.$entityMap.push(route);
122
+ this.$pathMap.push(route);
123
+ }
124
+ addSubPackagePage(subPackageRoute) {
125
+ if (!this.$subPackageRoute[subPackageRoute.root]) {
126
+ this.$subPackageRoute[subPackageRoute.root] = [];
138
127
  }
139
- }, {
140
- key: "getMatchedPage",
141
- value: function getMatchedPage(pathname) {
142
- return this.matchPageByPathname(pathname);
143
- }
144
- }, {
145
- key: "addPage",
146
- value: function addPage(route) {
147
- this.$entityMap.push(route);
148
- this.$pathMap.push(route);
149
- }
150
- }, {
151
- key: "addSubPackagePage",
152
- value: function addSubPackagePage(subPackageRoute) {
153
- if (!this.$subPackageRoute[subPackageRoute.root]) {
154
- this.$subPackageRoute[subPackageRoute.root] = [];
128
+ this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
129
+ }
130
+ getMatchedSubPackagePage(pathname, subPackage) {
131
+ const subs = this.$subPackageRoute[subPackage] || [];
132
+ for (let index = 0; index < subs.length; index++) {
133
+ const item = subs[index];
134
+ const urlMatch = match(item.route, {
135
+ decode: decodeURIComponent
136
+ });
137
+ if (urlMatch(pathname)) {
138
+ return {
139
+ pathname,
140
+ route: item.route,
141
+ params: urlMatch(pathname)['params'],
142
+ subPackage,
143
+ path: slash('/' + subPackage + item.path)
144
+ };
155
145
  }
156
-
157
- this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
158
146
  }
159
- }, {
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];
166
- var urlMatch = match(item.route, {
167
- decode: decodeURIComponent
168
- });
147
+ }
169
148
 
170
- if (urlMatch(pathname)) {
171
- return {
172
- pathname: pathname,
173
- route: item.route,
174
- params: urlMatch(pathname)['params'],
175
- subPackage: subPackage,
176
- path: slash('/' + subPackage + item.path)
177
- };
178
- }
179
- }
149
+ /**
150
+ * 以小程序地址进行匹配页面
151
+ * @param path - 小程序页面地址 /pages/home/index
152
+ * @returns
153
+ */
154
+ matchPageByPath(path) {
155
+ const route = this.$pathMap.find(route => route.path === path);
156
+ if (!route) {
157
+ console.warn('Not match route by:', path);
158
+ return undefined;
180
159
  }
181
- }]);
182
-
183
- return RouterScheduler;
184
- }(IRouterScheduler);
160
+ return route;
161
+ }
162
+ }
package/lib/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { Routes, TabBar, SubPackages } from '@ray-js/types';
2
2
  import { router } from './Router';
3
3
  export declare function createRouter(params: {
4
+ mode?: string;
4
5
  routes: Routes;
5
6
  tabBar: TabBar;
6
7
  subpackages: SubPackages;
package/lib/index.js CHANGED
@@ -1,22 +1,21 @@
1
- import "core-js/modules/es.array.map.js";
2
1
  import { router } from './Router';
3
2
  import { normalizeTabBar } from '@ray-js/framework-shared';
4
3
  export function createRouter(params) {
5
- var _params$tabBar = params.tabBar,
6
- tabBar = _params$tabBar === void 0 ? {} : _params$tabBar,
7
- subpackages = params.subpackages;
8
- var routes = params.routes.map(function (item) {
4
+ const {
5
+ tabBar = {},
6
+ subpackages,
7
+ mode
8
+ } = params;
9
+ const routes = params.routes.map(item => {
9
10
  var _item$route;
10
-
11
11
  item.route = (_item$route = item.route) !== null && _item$route !== void 0 ? _item$route : item.path;
12
12
  return item;
13
13
  });
14
-
15
- var _tabBar = normalizeTabBar(tabBar, routes);
16
-
14
+ const _tabBar = normalizeTabBar(tabBar, routes);
15
+ router.setMode(mode);
17
16
  router.scheduler.registryPages({
18
- routes: routes,
19
- subpackages: subpackages,
17
+ routes,
18
+ subpackages,
20
19
  tabBar: _tabBar
21
20
  });
22
21
  return router;
package/package.json CHANGED
@@ -1,42 +1,42 @@
1
1
  {
2
2
  "name": "@ray-js/router-mp",
3
- "version": "1.4.0-alpha.1",
3
+ "version": "1.4.0-alpha.11",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
7
7
  ],
8
+ "repository": {},
8
9
  "license": "MIT",
10
+ "maintainers": [
11
+ {
12
+ "name": "tuyafe",
13
+ "email": "tuyafe@tuya.com"
14
+ }
15
+ ],
9
16
  "main": "lib/index.js",
10
17
  "files": [
11
18
  "lib"
12
19
  ],
13
- "publishConfig": {
14
- "access": "public",
15
- "registry": "https://registry.npmjs.org"
16
- },
17
20
  "scripts": {
18
- "clean": "rm -rf lib",
19
21
  "build": "ray build --type=component --transform-mode=pure",
22
+ "clean": "rm -rf lib",
20
23
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
24
  },
22
25
  "dependencies": {
23
- "@ray-js/api": "^1.4.0-alpha.1",
24
- "@ray-js/env": "^1.4.0-alpha.1",
25
- "@ray-js/library": "^1.4.0-alpha.1",
26
- "@ray-js/types": "^1.4.0-alpha.1",
27
- "@react-navigation/core": "^6.1.0",
28
- "path-to-regexp": "^6.2.0",
29
- "slash": "^5.0.0"
26
+ "@ray-js/api": "^1.4.0-alpha.11",
27
+ "@ray-js/env": "^1.4.0-alpha.11",
28
+ "@ray-js/library": "^1.4.0-alpha.11",
29
+ "@ray-js/types": "^1.4.0-alpha.11",
30
+ "@react-navigation/core": "^6.4.9",
31
+ "path-to-regexp": "^6.2.1",
32
+ "slash": "^5.1.0"
30
33
  },
31
34
  "devDependencies": {
32
- "@ray-js/cli": "^1.4.0-alpha.1"
35
+ "@ray-js/cli": "^1.4.0-alpha.11"
33
36
  },
34
- "maintainers": [
35
- {
36
- "name": "tuyafe",
37
- "email": "tuyafe@tuya.com"
38
- }
39
- ],
40
- "gitHead": "4340240aafe16c03853241e2356134610e65443e",
41
- "repository": {}
37
+ "publishConfig": {
38
+ "access": "public",
39
+ "registry": "https://registry.npmjs.org"
40
+ },
41
+ "gitHead": "5e9c8e1d288eeaf707d34515399784d98ec13a1e"
42
42
  }