@ray-js/router-mp 1.1.1 → 1.2.0-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.js CHANGED
@@ -1,192 +1,110 @@
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 _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
- 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
1
  import { url } from '@ray-js/library';
15
- import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync } from '@ray-js/api';
2
+ import { navigateBack, navigateTo, reLaunch, switchTab, redirectTo, getLaunchOptionsSync, } from '@ray-js/api';
16
3
  import { isThing } from '@ray-js/env';
17
4
  import { RouterScheduler } from './RouterScheduler';
18
-
19
5
  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
- };
33
- }
6
+ const pages = getCurrentPages();
7
+ if (pages.length > 0) {
8
+ return pages[pages.length - 1];
9
+ }
10
+ else {
11
+ const { path, query } = getLaunchOptionsSync();
12
+ return { route: path, options: query };
13
+ }
34
14
  }
35
-
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());
43
- }
44
-
45
- _createClass(Router, [{
46
- key: "setUrlPrefix",
47
- value:
15
+ export class Router {
16
+ constructor() {
17
+ // 只有作为其他小程序的子包时,才设置urlPrefix
18
+ this.urlPrefix = '';
19
+ this.scheduler = new RouterScheduler();
20
+ }
48
21
  /**
49
22
  * setUrlPrefix
50
23
  */
51
- function setUrlPrefix(prefix) {
52
- this.urlPrefix = prefix;
24
+ setUrlPrefix(prefix) {
25
+ this.urlPrefix = prefix;
53
26
  }
54
- }, {
55
- key: "normalizeRoute",
56
- value:
57
27
  /**
58
28
  * 标准化路由路径,将 web 标准的地址栏转化到 小程序地址
59
29
  */
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
- })
30
+ normalizeRoute(params) {
31
+ const { pathname, query, hash } = url.parse(params.to);
32
+ const subPackage = params.subpackage;
33
+ const matchedPage = subPackage
34
+ ? this.scheduler.getMatchedSubPackagePage(pathname, subPackage)
35
+ : this.scheduler.getMatchedPage(pathname);
36
+ if (!matchedPage) {
37
+ return Promise.reject(undefined);
38
+ }
39
+ // 作为其他小程序时不能作为tabBar
40
+ if (this.urlPrefix) {
41
+ matchedPage.isTabBar = false;
42
+ }
43
+ let finalPath = matchedPage.path;
44
+ // FIXME: tabBar.list里的页面不能有query,也不能有hash
45
+ if (!matchedPage.isTabBar) {
46
+ finalPath = url.format({
47
+ pathname: matchedPage.path,
48
+ query: Object.assign(Object.assign(Object.assign({}, query), matchedPage.params), { ____h_a_s_h____: hash.slice(1) }),
49
+ });
50
+ }
51
+ /**
52
+ * 坑点之一
53
+ * 微信小程序: app.json 里的页面配置不能以`/`开头,wx.navigatorTo 却需要
54
+ * 涂鸦小程序: 都不能以`/`开头
55
+ */
56
+ if (this.urlPrefix) {
57
+ finalPath = ('/' + this.urlPrefix + '/' + finalPath).replace(/\/+/g, '/');
58
+ }
59
+ if (isThing) {
60
+ finalPath = finalPath.replace(/^\//, '');
61
+ }
62
+ matchedPage.path = finalPath;
63
+ return Promise.resolve(matchedPage);
64
+ }
65
+ push(to, options) {
66
+ const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
67
+ this.normalizeRoute({ to, subpackage }).then((route) => {
68
+ const path = route.path;
69
+ if (route.isTabBar) {
70
+ switchTab({ url: path });
71
+ }
72
+ else {
73
+ navigateTo({ url: path });
74
+ }
88
75
  });
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(/^\//, '');
103
- }
104
-
105
- matchedPage.path = finalPath;
106
- return Promise.resolve(matchedPage);
107
76
  }
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
-
118
- if (route.isTabBar) {
119
- switchTab({
120
- url: path
121
- });
122
- } else {
123
- navigateTo({
124
- url: path
125
- });
126
- }
127
- });
77
+ replace(to, options) {
78
+ const subpackage = options === null || options === void 0 ? void 0 : options.subpackage;
79
+ this.normalizeRoute({ to, subpackage }).then((route) => {
80
+ if (route.isTabBar) {
81
+ switchTab({ url: route.path });
82
+ }
83
+ else {
84
+ redirectTo({ url: route.path });
85
+ }
86
+ });
128
87
  }
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
- });
88
+ reload() {
89
+ const page = currentPage();
90
+ reLaunch({
91
+ url: url.params(page.route, page.options),
92
+ });
147
93
  }
148
- }, {
149
- key: "reload",
150
- value: function reload() {
151
- var page = currentPage();
152
- reLaunch({
153
- url: url.params(page.route, page.options)
154
- });
94
+ go(delta) {
95
+ throw new Error('Method not implemented.');
155
96
  }
156
- }, {
157
- key: "go",
158
- value: function go() {
159
- throw new Error('Method not implemented.');
97
+ back() {
98
+ navigateBack({ delta: 1 });
160
99
  }
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);
176
-
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
- });
100
+ // 获取宿主环境上的 href ,小程序端需要通过 path 进行转换
101
+ get href() {
102
+ const page = currentPage();
103
+ const reg = RegExp('^' + this.urlPrefix + '/');
104
+ const r = page.route.replace(reg, '');
105
+ const path = url.params(`/${r}`, page.options);
106
+ const { pathname, query, hash } = url.parse(path);
107
+ return this.scheduler.getHrefByPath({ path: pathname, query, hash });
187
108
  }
188
- }]);
189
-
190
- return Router;
191
- }();
192
- export var router = new Router();
109
+ }
110
+ export const router = new Router();
@@ -48,5 +48,5 @@ export declare class RouterScheduler extends IRouterScheduler<Route> {
48
48
  params: any;
49
49
  subPackage: string;
50
50
  path: string;
51
- } | undefined;
51
+ };
52
52
  }
@@ -1,184 +1,102 @@
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
- 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
1
  import { match, compile, pathToRegexp } from 'path-to-regexp';
13
2
  import { url } from '@ray-js/library';
14
3
  import slash from 'slash';
15
4
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
16
-
17
5
  /**
18
6
  * 小程序路由别名机制,将标准化的 web pathname 转化为小程序的 path
19
7
  */
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];
8
+ export class RouterScheduler extends IRouterScheduler {
9
+ constructor() {
10
+ super(...arguments);
11
+ this.$entityMap = [];
12
+ this.$pathMap = []; // 以小程序路径作为键存储关系
13
+ this.$subPackageRoute = {};
32
14
  }
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;
43
- }
44
-
45
- _createClass(RouterScheduler, [{
46
- key: "getHrefByPath",
47
- value:
48
15
  /**
49
16
  * 将小程序地址栏转化为 web 地址
50
17
  * @param path - 小程序页面地址
51
18
  */
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);
19
+ getHrefByPath(opts) {
20
+ const { query, path, hash = '' } = opts;
21
+ const route = this.matchPageByPath(path);
22
+ if (route) {
23
+ let keys = [];
24
+ pathToRegexp(route.route, keys);
25
+ if (keys.length > 0) {
26
+ const restQuery = Object.assign({}, query);
27
+ keys.forEach(({ name }) => {
28
+ delete restQuery[name];
29
+ });
30
+ // 兼容小程序tab页,因tab页不能携带参数
31
+ keys.forEach((k) => { var _a; return (query[k.name] = (_a = query[k.name]) !== null && _a !== void 0 ? _a : ''); });
32
+ const href = compile(route.route, { validate: false })(query) + hash;
33
+ return url.params(href, restQuery);
34
+ }
35
+ return url.params(route.route + hash, query);
80
36
  }
81
-
82
- return url.params(route.route + hash, query);
83
- }
84
-
85
- return '';
37
+ return '';
86
38
  }
87
39
  /**
88
40
  * 以小程序地址进行匹配页面
89
41
  * @param path - 小程序页面地址 /pages/home/index
90
42
  * @returns
91
43
  */
92
-
93
- }, {
94
- key: "matchPageByPath",
95
- value: function matchPageByPath(path) {
96
- var route = this.$pathMap.find(function (route) {
97
- return route.path === path;
98
- });
99
-
100
- if (!route) {
101
- console.warn('Not match route by:', path);
102
- return undefined;
103
- }
104
-
105
- return route;
44
+ matchPageByPath(path) {
45
+ const route = this.$pathMap.find((route) => route.path === path);
46
+ if (!route) {
47
+ console.warn('Not match route by:', path);
48
+ return undefined;
49
+ }
50
+ return route;
106
51
  }
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
- });
52
+ registryPages(params) {
53
+ const { routes, tabBar, subpackages = [] } = params;
54
+ const tabBarList = (tabBar === null || tabBar === void 0 ? void 0 : tabBar.list) || [];
55
+ subpackages.forEach((subPackage) => {
56
+ const root = subPackage.root;
57
+ subPackage.pages.forEach((page) => {
58
+ this.addSubPackagePage({
59
+ root,
60
+ route: page.route || page.path,
61
+ path: page.path,
62
+ });
63
+ });
125
64
  });
126
- });
127
- routes.forEach(function (r) {
128
- var isTabBar = tabBarList.some(function (item) {
129
- return r.path === item.pagePath;
65
+ routes.forEach((r) => {
66
+ const isTabBar = tabBarList.some((item) => r.path === item.pagePath);
67
+ const route = r.route || r.path;
68
+ this.addPage(Object.assign(Object.assign({}, r), { isTabBar, route }));
130
69
  });
131
- var route = r.route || r.path;
132
-
133
- _this2.addPage(_objectSpread(_objectSpread({}, r), {}, {
134
- isTabBar: isTabBar,
135
- route: route
136
- }));
137
- });
138
70
  }
139
- }, {
140
- key: "getMatchedPage",
141
- value: function getMatchedPage(pathname) {
142
- return this.matchPageByPathname(pathname);
71
+ getMatchedPage(pathname) {
72
+ return this.matchPageByPathname(pathname);
143
73
  }
144
- }, {
145
- key: "addPage",
146
- value: function addPage(route) {
147
- this.$entityMap.push(route);
148
- this.$pathMap.push(route);
74
+ addPage(route) {
75
+ this.$entityMap.push(route);
76
+ this.$pathMap.push(route);
149
77
  }
150
- }, {
151
- key: "addSubPackagePage",
152
- value: function addSubPackagePage(subPackageRoute) {
153
- if (!this.$subPackageRoute[subPackageRoute.root]) {
154
- this.$subPackageRoute[subPackageRoute.root] = [];
155
- }
156
-
157
- this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
78
+ addSubPackagePage(subPackageRoute) {
79
+ if (!this.$subPackageRoute[subPackageRoute.root]) {
80
+ this.$subPackageRoute[subPackageRoute.root] = [];
81
+ }
82
+ this.$subPackageRoute[subPackageRoute.root].push(subPackageRoute);
158
83
  }
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
- });
169
-
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
- };
84
+ getMatchedSubPackagePage(pathname, subPackage) {
85
+ const subs = this.$subPackageRoute[subPackage] || [];
86
+ for (let index = 0; index < subs.length; index++) {
87
+ const item = subs[index];
88
+ const urlMatch = match(item.route, {
89
+ decode: decodeURIComponent,
90
+ });
91
+ if (urlMatch(pathname)) {
92
+ return {
93
+ pathname,
94
+ route: item.route,
95
+ params: urlMatch(pathname)['params'],
96
+ subPackage,
97
+ path: slash('/' + subPackage + item.path),
98
+ };
99
+ }
178
100
  }
179
- }
180
101
  }
181
- }]);
182
-
183
- return RouterScheduler;
184
- }(IRouterScheduler);
102
+ }
package/lib/index.js CHANGED
@@ -1,24 +1,15 @@
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) {
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
-
17
- router.scheduler.registryPages({
18
- routes: routes,
19
- subpackages: subpackages,
20
- tabBar: _tabBar
21
- });
22
- return router;
4
+ const { tabBar = {}, subpackages } = params;
5
+ const routes = params.routes.map((item) => {
6
+ var _a;
7
+ item.route = (_a = item.route) !== null && _a !== void 0 ? _a : item.path;
8
+ return item;
9
+ });
10
+ const _tabBar = normalizeTabBar(tabBar, routes);
11
+ const finalCfg = { routes, subpackages, tabBar: _tabBar };
12
+ router.scheduler.registryPages(finalCfg);
13
+ return router;
23
14
  }
24
- export default router;
15
+ export default router;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router-mp",
3
- "version": "1.1.1",
3
+ "version": "1.2.0-beta.1",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
@@ -20,16 +20,16 @@
20
20
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@ray-js/api": "^1.1.1",
24
- "@ray-js/env": "^1.1.1",
25
- "@ray-js/library": "^1.1.1",
26
- "@ray-js/types": "^1.1.1",
23
+ "@ray-js/api": "^1.2.0-beta.1",
24
+ "@ray-js/env": "^1.2.0-beta.1",
25
+ "@ray-js/library": "^1.2.0-beta.1",
26
+ "@ray-js/types": "^1.2.0-beta.1",
27
27
  "@react-navigation/core": "^6.1.0",
28
28
  "path-to-regexp": "^6.2.0",
29
29
  "slash": "^5.0.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@ray-js/cli": "^1.1.1"
32
+ "@ray-js/cli": "^1.2.0-beta.1"
33
33
  },
34
34
  "maintainers": [
35
35
  {
@@ -37,6 +37,6 @@
37
37
  "email": "tuyafe@tuya.com"
38
38
  }
39
39
  ],
40
- "gitHead": "07feb5c2fada4b55bde8fb37d37c56578fe6ce03",
40
+ "gitHead": "2b34058549219b57afaa6b6f80c1b896e1602d53",
41
41
  "repository": {}
42
42
  }