@ray-js/router 0.4.10-beta-1 → 0.4.12-beta-0

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,135 +1,67 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
- import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
3
- import _createClass from "@babel/runtime/helpers/esm/createClass";
4
- import _regeneratorRuntime from "@babel/runtime/regenerator";
5
- import "core-js/modules/es.string.starts-with.js";
6
- import "core-js/modules/es.regexp.exec.js";
7
- import "core-js/modules/es.string.search.js";
8
- import "core-js/modules/es.string.replace.js";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
9
10
  import { url } from '@ray-js/library';
10
11
  import { history } from './history';
11
- export var Router = /*#__PURE__*/function () {
12
- /**
13
- * 页面路由调度器
14
- */
15
-
16
- /**
17
- * 当前环境地址栏
18
- */
19
- function Router(options) {
20
- _classCallCheck(this, Router);
21
-
22
- this.scheduler = options.scheduler;
23
- }
24
-
25
- _createClass(Router, [{
26
- key: "normalizeRoute",
27
- value: function () {
28
- var _normalizeRoute = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(params) {
29
- var to, _url$parse, pathname, search, hash;
30
-
31
- return _regeneratorRuntime.wrap(function (_context) {
32
- while (1) {
33
- switch (_context.prev = _context.next) {
34
- case 0:
35
- to = params.to; // FIXME: 绝对路径是否需要支持?
36
-
37
- if (!(to.startsWith('//') || to.startsWith('http'))) {
38
- _context.next = 3;
39
- break;
40
- }
41
-
42
- return _context.abrupt("return", {
43
- to: to
44
- });
45
-
46
- case 3:
47
- if (this.scheduler.basename) {
48
- to = this.scheduler.basename + to;
49
- } // TODO: Web 应用下重复进入一个路由
50
-
51
-
52
- if (this.$href) {
53
- _url$parse = url.parse(this.$href), pathname = _url$parse.pathname, search = _url$parse.search, hash = _url$parse.hash;
54
-
55
- if (url.format({
56
- pathname: pathname,
57
- search: search,
58
- hash: hash
59
- }) === to) {
12
+ export class Router {
13
+ constructor(options) {
14
+ this.scheduler = options.scheduler;
15
+ }
16
+ normalizeRoute(params) {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ let { to } = params;
19
+ // FIXME: 绝对路径是否需要支持?
20
+ if (to.startsWith('//') || to.startsWith('http')) {
21
+ return { to };
22
+ }
23
+ if (this.scheduler.basename) {
24
+ to = this.scheduler.basename + to;
25
+ }
26
+ // TODO: Web 应用下重复进入一个路由
27
+ if (this.$href) {
28
+ const { pathname, search, hash } = url.parse(this.$href);
29
+ if (url.format({ pathname, search, hash }) === to) {
60
30
  console.warn('duplicate route to:', to);
61
- }
62
31
  }
63
-
64
- return _context.abrupt("return", {
65
- to: to
66
- });
67
-
68
- case 6:
69
- case "end":
70
- return _context.stop();
71
32
  }
72
- }
73
- }, _callee, this);
74
- }));
75
-
76
- function normalizeRoute() {
77
- return _normalizeRoute.apply(this, arguments);
78
- }
79
-
80
- return normalizeRoute;
81
- }()
82
- }, {
83
- key: "push",
84
- value: function push(to) {
85
- this.normalizeRoute({
86
- to: to
87
- }).then(function (_ref) {
88
- var to = _ref.to;
89
- history.push(to);
90
- });
33
+ return { to };
34
+ });
91
35
  }
92
- }, {
93
- key: "replace",
94
- value: function replace(to) {
95
- this.normalizeRoute({
96
- to: to
97
- }).then(function (_ref2) {
98
- var to = _ref2.to;
99
- history.replace(to);
100
- });
36
+ push(to) {
37
+ this.normalizeRoute({ to }).then(({ to }) => {
38
+ history.push(to);
39
+ });
40
+ }
41
+ replace(to) {
42
+ this.normalizeRoute({ to }).then(({ to }) => {
43
+ history.replace(to);
44
+ });
101
45
  }
102
- }, {
103
- key: "go",
104
- value: function go(delta) {
105
- // 在应用堆栈内进行跳转
106
- history.go(delta);
46
+ go(delta) {
47
+ // 在应用堆栈内进行跳转
48
+ history.go(delta);
107
49
  }
108
- }, {
109
- key: "back",
110
- value: function back() {
111
- history.goBack();
50
+ back() {
51
+ history.goBack();
112
52
  }
113
- }, {
114
- key: "reload",
115
- value: function reload(to) {
116
- if (to) {
117
- this.normalizeRoute({
118
- to: to
119
- }).then(function (_ref3) {
120
- var to = _ref3.to;
121
- window.location.href = to;
122
- });
123
- } else {
124
- window.location.reload();
125
- }
53
+ reload(to) {
54
+ if (to) {
55
+ this.normalizeRoute({ to }).then(({ to }) => {
56
+ window.location.href = to;
57
+ });
58
+ }
59
+ else {
60
+ window.location.reload();
61
+ }
126
62
  }
127
- }, {
128
- key: "hostHref",
129
- get: function get() {
130
- return window.location.href;
63
+ get hostHref() {
64
+ return window.location.href;
131
65
  }
132
- }]);
133
-
134
- return Router;
135
- }();
66
+ }
67
+ //# sourceMappingURL=Router.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Router.js","sourceRoot":"","sources":["../src/Router.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAA;AAGrC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAA;AAGnC,MAAM,OAAO,MAAM;IAUjB,YAAY,OAAuC;QACjD,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;IACpC,CAAC;IAEa,cAAc,CAAC,MAAsB;;YACjD,IAAI,EAAE,EAAE,EAAE,GAAG,MAAM,CAAA;YACnB,qBAAqB;YACrB,IAAI,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,UAAU,CAAC,MAAM,CAA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CAAC,EAAE,EAAE,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE;gBAC1C,MAAM,CAAC,QAAQ,CAAC,IAAI,GAAG,EAAE,CAAA;YAC3B,CAAC,CAAC,CAAA;SACH;aAAM;YACL,MAAM,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAA;SACzB;IACH,CAAC;IAED,IAAI,QAAQ;QACV,OAAO,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAA;IAC7B,CAAC;CACF"}
@@ -1,145 +1,82 @@
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.array.splice.js";
10
- import "core-js/modules/es.array.index-of.js";
11
- import "core-js/modules/es.object.to-string.js";
12
- import "core-js/modules/web.dom-collections.for-each.js";
13
1
  import { RouterScheduler as IRouterScheduler } from '@ray-js/types';
14
-
15
2
  /**
16
3
  * web 环境下的路由协调器
17
4
  */
18
- export var RouterScheduler = /*#__PURE__*/function (_IRouterScheduler) {
19
- _inherits(RouterScheduler, _IRouterScheduler);
20
-
21
- var _super = _createSuper(RouterScheduler);
22
-
23
- function RouterScheduler() {
24
- var _this;
25
-
26
- _classCallCheck(this, RouterScheduler);
27
-
28
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
29
- args[_key] = arguments[_key];
5
+ export class RouterScheduler extends IRouterScheduler {
6
+ constructor() {
7
+ super(...arguments);
8
+ this.$currentRoute = '';
9
+ this.$listeners = {};
10
+ this.$entityMap = {};
11
+ this.basename = '';
30
12
  }
31
-
32
- _this = _super.call.apply(_super, [this].concat(args));
33
-
34
- _defineProperty(_assertThisInitialized(_this), "$currentRoute", '');
35
-
36
- _defineProperty(_assertThisInitialized(_this), "$listeners", {});
37
-
38
- _defineProperty(_assertThisInitialized(_this), "$entityMap", {});
39
-
40
- _defineProperty(_assertThisInitialized(_this), "basename", '');
41
-
42
- return _this;
43
- }
44
-
45
- _createClass(RouterScheduler, [{
46
- key: "currentRoute",
47
- get:
48
13
  /**
49
14
  * 当前调度器的 route 地址
50
15
  */
51
- function get() {
52
- return this.$currentRoute;
53
- },
54
- set: function set(val) {
55
- if (val !== this.currentRoute) {
56
- this.$currentRoute = val;
57
- this.emit('routeChange', this.currentRoute);
58
- }
16
+ get currentRoute() {
17
+ return this.$currentRoute;
59
18
  }
60
- }, {
61
- key: "on",
62
- value: function on(event, fn) {
63
- if (!this.$listeners[event]) {
64
- this.$listeners[event] = [];
65
- }
66
-
67
- var events = this.$listeners[event];
68
- events.push(fn);
69
- return function () {
70
- events.splice(events.indexOf(fn), 1);
71
- };
72
- } // TabBar 监听,用于处理当前高亮状态
73
-
74
- }, {
75
- key: "emit",
76
- value: function emit(event) {
77
- for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {
78
- args[_key2 - 1] = arguments[_key2];
79
- }
80
-
81
- var fns = this.$listeners[event];
82
-
83
- if (Array.isArray(fns)) {
84
- fns.forEach(function (fn) {
85
- fn.apply(void 0, args);
86
- });
87
- }
19
+ set currentRoute(val) {
20
+ if (val !== this.currentRoute) {
21
+ this.$currentRoute = val;
22
+ this.emit('routeChange', this.currentRoute);
23
+ }
24
+ }
25
+ on(event, fn) {
26
+ if (!this.$listeners[event]) {
27
+ this.$listeners[event] = [];
28
+ }
29
+ const events = this.$listeners[event];
30
+ events.push(fn);
31
+ return () => {
32
+ events.splice(events.indexOf(fn), 1);
33
+ };
34
+ }
35
+ // TabBar 监听,用于处理当前高亮状态
36
+ emit(event, ...args) {
37
+ const fns = this.$listeners[event];
38
+ if (Array.isArray(fns)) {
39
+ fns.forEach((fn) => {
40
+ fn(...args);
41
+ });
42
+ }
88
43
  }
89
44
  /**
90
45
  * 初始化路由,注册应用声明的路由,并处理映射关系
91
46
  */
92
-
93
- }, {
94
- key: "registryPages",
95
- value: function registryPages(params) {
96
- var _this2 = this;
97
-
98
- var pages = params.pages,
99
- tabBar = params.tabBar;
100
- var tabBarList = tabBar.list || [];
101
- pages.forEach(function (page) {
102
- var isTabBar = tabBarList.some(function (item) {
103
- if (item.route) {
104
- return page.route === item.route;
105
- } else if (item.id) {
106
- return page.id === item.id;
107
- }
108
-
109
- return false;
47
+ registryPages(params) {
48
+ const { pages, tabBar } = params;
49
+ const tabBarList = tabBar.list || [];
50
+ pages.forEach((page) => {
51
+ const isTabBar = tabBarList.some((item) => {
52
+ if (item.route) {
53
+ return page.route === item.route;
54
+ }
55
+ else if (item.id) {
56
+ return page.id === item.id;
57
+ }
58
+ return false;
59
+ });
60
+ page.route = this.basename + page.route;
61
+ page.originalRoute = page.route;
62
+ this.addPage(Object.assign(Object.assign({}, page), { isTabBar }));
110
63
  });
111
- page.route = _this2.basename + page.route;
112
- page.originalRoute = page.route;
113
-
114
- _this2.addPage(_objectSpread(_objectSpread({}, page), {}, {
115
- isTabBar: isTabBar
116
- }));
117
- });
118
64
  }
119
- }, {
120
- key: "getMatchedPage",
121
- value: function getMatchedPage(pathname) {
122
- // 适配 `domain.com/basename/` & `domain.com/basename`
123
- // 注册首页使用的是 `/` 需预处理
124
- if (this.basename === pathname) {
125
- pathname = pathname + '/';
126
- }
127
-
128
- this.currentRoute = pathname;
129
- return this.matchPageByPathname(pathname);
65
+ getMatchedPage(pathname) {
66
+ // 适配 `domain.com/basename/` & `domain.com/basename`
67
+ // 注册首页使用的是 `/` 需预处理
68
+ if (this.basename === pathname) {
69
+ pathname = pathname + '/';
70
+ }
71
+ this.currentRoute = pathname;
72
+ return this.matchPageByPathname(pathname);
130
73
  }
131
- }, {
132
- key: "addPage",
133
- value: function addPage(page) {
134
- var depth = this.slashDepth(page.route);
135
-
136
- if (!this.$entityMap[depth]) {
137
- this.$entityMap[depth] = [];
138
- }
139
-
140
- this.$entityMap[depth].push(page);
74
+ addPage(page) {
75
+ const depth = this.slashDepth(page.route);
76
+ if (!this.$entityMap[depth]) {
77
+ this.$entityMap[depth] = [];
78
+ }
79
+ this.$entityMap[depth].push(page);
141
80
  }
142
- }]);
143
-
144
- return RouterScheduler;
145
- }(IRouterScheduler);
81
+ }
82
+ //# sourceMappingURL=RouterScheduler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RouterScheduler.js","sourceRoot":"","sources":["../src/RouterScheduler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,eAAe,IAAI,gBAAgB,EAAkB,MAAM,eAAe,CAAA;AAIhG;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,gBAAsB;IAA3D;;QACU,kBAAa,GAAW,EAAE,CAAA;QAC1B,eAAU,GAA+B,EAAE,CAAA;QACnD,eAAU,GAA2B,EAAE,CAAA;QAChC,aAAQ,GAAW,EAAE,CAAA;IA4E9B,CAAC;IA1EC;;OAEG;IACH,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,aAAa,CAAA;IAC3B,CAAC;IAED,IAAI,YAAY,CAAC,GAAW;QAC1B,IAAI,GAAG,KAAK,IAAI,CAAC,YAAY,EAAE;YAC7B,IAAI,CAAC,aAAa,GAAG,GAAG,CAAA;YACxB,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,IAAI,CAAC,YAAY,CAAC,CAAA;SAC5C;IACH,CAAC;IAEM,EAAE,CAAC,KAAmB,EAAE,EAAY;QACzC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;YAC3B,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,CAAA;SAC5B;QACD,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,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CAAM,IAAI,KAAE,QAAQ,IAAG,CAAA;QACrC,CAAC,CAAC,CAAA;IACJ,CAAC;IACM,cAAc,CAAC,QAAgB;QACpC,oDAAoD;QACpD,oBAAoB;QACpB,IAAI,IAAI,CAAC,QAAQ,KAAK,QAAQ,EAAE;YAC9B,QAAQ,GAAG,QAAQ,GAAG,GAAG,CAAA;SAC1B;QAED,IAAI,CAAC,YAAY,GAAG,QAAQ,CAAA;QAE5B,OAAO,IAAI,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAA;IAC3C,CAAC;IACD,OAAO,CAAC,IAAU;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QACzC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,EAAE;YAC3B,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,CAAA;SAC5B;QACD,IAAI,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAA;IACnC,CAAC;CACF"}
@@ -1,87 +1,55 @@
1
- import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
2
- import _regeneratorRuntime from "@babel/runtime/regenerator";
3
- import "core-js/modules/es.object.to-string.js";
4
- import "core-js/modules/es.promise.js";
1
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
5
10
  import { createBrowserHistory } from 'history';
6
11
  import React from 'react';
7
- export var history;
8
- var changeKey = '';
12
+ export let history;
13
+ let changeKey = '';
9
14
  export function createHistory(options) {
10
- var scheduler = options.scheduler;
11
- history = createBrowserHistory(options);
12
- history.listen( /*#__PURE__*/function () {
13
- var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(historyLocation) {
14
- var key, pathname, matchedPage, page, NotFound;
15
- return _regeneratorRuntime.wrap(function (_context) {
16
- while (1) {
17
- switch (_context.prev = _context.next) {
18
- case 0:
19
- key = historyLocation.key, pathname = historyLocation.pathname;
20
-
21
- if (!(key && key === changeKey)) {
22
- _context.next = 3;
23
- break;
24
- }
25
-
26
- return _context.abrupt("return");
27
-
28
- case 3:
29
- changeKey = key;
30
- matchedPage = scheduler.getMatchedPage(pathname);
31
-
32
- if (!matchedPage) {
33
- // 兜底通配页面
34
- matchedPage = scheduler.getMatchedPage('*');
35
- }
36
-
37
- if (!matchedPage) {
38
- _context.next = 13;
39
- break;
40
- }
41
-
42
- _context.next = 9;
43
- return matchedPage.component();
44
-
45
- case 9:
46
- page = _context.sent;
47
- options.onChange(page, {
48
- page: matchedPage
49
- });
50
- _context.next = 16;
51
- break;
52
-
53
- case 13:
54
- console.warn('Not match any route!'); // 兜底 404 页面
55
-
56
- NotFound = function () {
57
- return /*#__PURE__*/React.createElement('div', null, 'Page Not Found');
58
- };
59
-
60
- options.onChange(NotFound, {
15
+ const { scheduler } = options;
16
+ history = createBrowserHistory(options);
17
+ history.listen((historyLocation) => __awaiter(this, void 0, void 0, function* () {
18
+ const { key, pathname } = historyLocation;
19
+ if (key && key === changeKey) {
20
+ return;
21
+ }
22
+ changeKey = key;
23
+ let matchedPage = scheduler.getMatchedPage(pathname);
24
+ if (!matchedPage) {
25
+ // 兜底通配页面
26
+ matchedPage = scheduler.getMatchedPage('*');
27
+ }
28
+ if (matchedPage) {
29
+ const page = yield matchedPage.component();
30
+ options.onChange(page, { page: matchedPage });
31
+ }
32
+ else {
33
+ console.warn('Not match any route!');
34
+ // 兜底 404 页面
35
+ const NotFound = function NotFound() {
36
+ return React.createElement('div', null, 'Page Not Found');
37
+ };
38
+ options.onChange(NotFound, {
61
39
  page: {
62
- id: pathname,
63
- pathname: pathname,
64
- route: '*',
65
- path: '*',
66
- config: {},
67
- params: {},
68
- component: function component() {
69
- return Promise.resolve(NotFound);
70
- }
71
- }
72
- });
73
-
74
- case 16:
75
- case "end":
76
- return _context.stop();
77
- }
40
+ id: pathname,
41
+ pathname: pathname,
42
+ route: '*',
43
+ path: '*',
44
+ config: {},
45
+ params: {},
46
+ component: () => {
47
+ return Promise.resolve(NotFound);
48
+ },
49
+ },
50
+ });
78
51
  }
79
- }, _callee);
80
52
  }));
81
-
82
- return function () {
83
- return _ref.apply(this, arguments);
84
- };
85
- }());
86
- return history;
87
- }
53
+ return history;
54
+ }
55
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/history/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,oBAAoB,EAAW,MAAM,SAAS,CAAA;AACvD,OAAO,KAAK,MAAM,OAAO,CAAA;AAIzB,MAAM,CAAC,IAAI,OAAgB,CAAA;AAE3B,IAAI,SAAS,GAAG,EAAE,CAAA;AAClB,MAAM,UAAU,aAAa,CAAC,OAI7B;IACC,MAAM,EAAE,SAAS,EAAE,GAAG,OAAO,CAAA;IAE7B,OAAO,GAAG,oBAAoB,CAAC,OAAO,CAAC,CAAA;IAEvC,OAAO,CAAC,MAAM,CAAC,CAAO,eAAe,EAAE,EAAE;QACvC,MAAM,EAAE,GAAG,EAAE,QAAQ,EAAE,GAAG,eAAe,CAAA;QAEzC,IAAI,GAAG,IAAI,GAAG,KAAK,SAAS,EAAE;YAC5B,OAAM;SACP;QACD,SAAS,GAAG,GAAG,CAAA;QAEf,IAAI,WAAW,GAAG,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAA;QAEpD,IAAI,CAAC,WAAW,EAAE;YAChB,SAAS;YACT,WAAW,GAAG,SAAS,CAAC,cAAc,CAAC,GAAG,CAAC,CAAA;SAC5C;QAED,IAAI,WAAW,EAAE;YACf,MAAM,IAAI,GAAG,MAAM,WAAW,CAAC,SAAS,EAAE,CAAA;YAC1C,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,WAAW,EAAE,CAAC,CAAA;SAC9C;aAAM;YACL,OAAO,CAAC,IAAI,CAAC,sBAAsB,CAAC,CAAA;YACpC,YAAY;YACZ,MAAM,QAAQ,GAAa,SAAS,QAAQ;gBAC1C,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,EAAE,IAAI,EAAE,gBAAgB,CAAC,CAAA;YAC3D,CAAC,CAAA;YAED,OAAO,CAAC,QAAQ,CAAC,QAAQ,EAAE;gBACzB,IAAI,EAAE;oBACJ,EAAE,EAAE,QAAQ;oBACZ,QAAQ,EAAE,QAAQ;oBAClB,KAAK,EAAE,GAAG;oBACV,IAAI,EAAE,GAAG;oBACT,MAAM,EAAE,EAAE;oBACV,MAAM,EAAE,EAAE;oBACV,SAAS,EAAE,GAAG,EAAE;wBACd,OAAO,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAA;oBAClC,CAAC;iBACF;aACF,CAAC,CAAA;SACH;IACH,CAAC,CAAA,CAAC,CAAA;IAEF,OAAO,OAAO,CAAA;AAChB,CAAC"}
package/lib/index.js CHANGED
@@ -1,45 +1,28 @@
1
- import "core-js/modules/es.regexp.exec.js";
2
- import "core-js/modules/es.string.search.js";
3
- import "core-js/modules/es.string.replace.js";
4
1
  import { createHistory } from './history';
5
2
  import { Router } from './Router';
6
3
  import { RouterScheduler } from './RouterScheduler';
7
- var scheduler = window.__ray__routerScheduler__ = new RouterScheduler();
8
- var router = window.__ray__router__ = new Router({
9
- scheduler: scheduler
10
- });
4
+ const scheduler = (window.__ray__routerScheduler__ = new RouterScheduler());
5
+ const router = (window.__ray__router__ = new Router({ scheduler }));
11
6
  export function createRouter(options) {
12
- var basename = options.basename,
13
- _onChange = options.onChange,
14
- routes = options.routes,
15
- tabBar = options.tabBar,
16
- pages = options.pages;
17
- var history = createHistory({
18
- basename: basename,
19
- scheduler: scheduler,
20
- onChange: function onChange() {
21
- router.$href = window.location.href;
22
-
23
- _onChange.apply(void 0, arguments);
7
+ const { basename, onChange, routes, tabBar, pages } = options;
8
+ const history = createHistory({
9
+ basename,
10
+ scheduler,
11
+ onChange: (...args) => {
12
+ router.$href = window.location.href;
13
+ onChange(...args);
14
+ },
15
+ });
16
+ // 设置路由基准路径
17
+ if (basename) {
18
+ router.scheduler.basename = basename;
24
19
  }
25
- }); // 设置路由基准路径
26
-
27
- if (basename) {
28
- router.scheduler.basename = basename;
29
- } // 调度器注册页面
30
-
31
-
32
- router.scheduler.registryPages({
33
- routes: routes,
34
- tabBar: tabBar,
35
- pages: pages
36
- });
37
- var _history$location = history.location,
38
- pathname = _history$location.pathname,
39
- search = _history$location.search,
40
- hash = _history$location.hash; // 首次触发 history change
41
-
42
- history.replace(pathname + search + hash);
43
- return router;
20
+ // 调度器注册页面
21
+ router.scheduler.registryPages({ routes, tabBar, pages });
22
+ const { pathname, search, hash } = history.location;
23
+ // 首次触发 history change
24
+ history.replace(pathname + search + hash);
25
+ return router;
44
26
  }
45
- export default router;
27
+ export default router;
28
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAA;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AACjC,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AAEnD,MAAM,SAAS,GAAG,CAAC,MAAM,CAAC,wBAAwB,GAAG,IAAI,eAAe,EAAE,CAAC,CAAA;AAE3E,MAAM,MAAM,GAAG,CAAC,MAAM,CAAC,eAAe,GAAG,IAAI,MAAM,CAAC,EAAE,SAAS,EAAE,CAAC,CAAC,CAAA;AAEnE,MAAM,UAAU,YAAY,CAAC,OAM5B;IACC,MAAM,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,OAAO,CAAA;IAC7D,MAAM,OAAO,GAAG,aAAa,CAAC;QAC5B,QAAQ;QACR,SAAS;QACT,QAAQ,EAAE,CAAC,GAAG,IAAI,EAAE,EAAE;YACpB,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,QAAQ,CAAC,IAAI,CAAA;YACnC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAA;QACnB,CAAC;KACF,CAAC,CAAA;IAEF,WAAW;IACX,IAAI,QAAQ,EAAE;QACZ,MAAM,CAAC,SAAS,CAAC,QAAQ,GAAG,QAAQ,CAAA;KACrC;IAED,UAAU;IACV,MAAM,CAAC,SAAS,CAAC,aAAa,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAA;IAEzD,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,OAAO,CAAC,QAAQ,CAAA;IACnD,sBAAsB;IACtB,OAAO,CAAC,OAAO,CAAC,QAAQ,GAAG,MAAM,GAAG,IAAI,CAAC,CAAA;IAEzC,OAAO,MAAM,CAAA;AACf,CAAC;AAED,eAAe,MAAM,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ray-js/router",
3
- "version": "0.4.10-beta-1",
3
+ "version": "0.4.12-beta-0",
4
4
  "description": "Ray Core",
5
5
  "keywords": [
6
6
  "ray"
@@ -20,12 +20,12 @@
20
20
  "watch": "tsc -p ./tsconfig.build.json --module esnext --outDir lib --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@ray-js/library": "^0.4.10-beta-1",
24
- "@ray-js/types": "^0.4.10-beta-1",
23
+ "@ray-js/library": "^0.4.12-beta-0",
24
+ "@ray-js/types": "^0.4.12-beta-0",
25
25
  "history": "4.x"
26
26
  },
27
27
  "devDependencies": {
28
- "@ray-js/cli": "^0.4.10-beta-1",
28
+ "@ray-js/cli": "^0.4.12-beta-0",
29
29
  "@types/history": "^4.7.9"
30
30
  },
31
31
  "maintainers": [
@@ -34,6 +34,6 @@
34
34
  "email": "tuyafe@tuya.com"
35
35
  }
36
36
  ],
37
- "gitHead": "be25152688d6b4eddfec6a33f3f4a6dcee67408d",
37
+ "gitHead": "522a92a23084e3195cdea186caf37f0c3492110b",
38
38
  "repository": {}
39
39
  }