@tarojs/router 3.3.16 → 3.3.17

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/dist/index.js CHANGED
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
5
  var runtime = require('@tarojs/runtime');
6
+ var taro = require('@tarojs/taro');
6
7
 
7
8
  function _extends() {
8
9
  _extends = Object.assign || function (target) {
@@ -88,7 +89,7 @@ Stacks.prototype.getItem = function getItem (index) {
88
89
  };
89
90
  Stacks.prototype.getLastIndex = function getLastIndex (pathname) {
90
91
  var list = [].concat( this.stacks ).reverse();
91
- return list.findIndex(function (page) { var _a; return ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
92
+ return list.findIndex(function (page, i) { var _a; return i !== 0 && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
92
93
  };
93
94
  Stacks.prototype.getDelta = function getDelta (pathname) {
94
95
  if (this.backDelta >= 1) {
@@ -114,16 +115,58 @@ Stacks.prototype.push = function push (page) {
114
115
  Object.defineProperties( Stacks.prototype, prototypeAccessors );
115
116
  var stacks = new Stacks();
116
117
 
117
- var routesAlias = {};
118
- function setRoutesAlias(alias) {
119
- routesAlias = alias;
120
- }
121
118
  function addLeadingSlash(path) {
122
119
  if (path == null) {
123
120
  return '';
124
121
  }
125
122
  return path.charAt(0) === '/' ? path : '/' + path;
126
123
  }
124
+ var RoutesAlias = function RoutesAlias() {
125
+ var this$1 = this;
126
+
127
+ this.conf = [];
128
+ this.getConfig = function (url) {
129
+ var customRoute = this$1.conf.filter(function (arr) {
130
+ return arr.includes(url);
131
+ });
132
+ return customRoute[0];
133
+ };
134
+ this.getOrigin = function (url) {
135
+ var _a;
136
+ return ((_a = this$1.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
137
+ };
138
+ this.getAlias = function (url) {
139
+ var _a;
140
+ return ((_a = this$1.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
141
+ };
142
+ this.getAll = function (url) {
143
+ return this$1.conf.filter(function (arr) {
144
+ return arr.includes(url);
145
+ }).reduce(function (p, a) {
146
+ p.push(a[1]);
147
+ return p;
148
+ }, [url]);
149
+ };
150
+ };
151
+ RoutesAlias.prototype.set = function set (customRoutes) {
152
+ var this$1 = this;
153
+ var ref;
154
+
155
+ if ( customRoutes === void 0 ) customRoutes = {};
156
+ var loop = function ( key ) {
157
+ var path = customRoutes[key];
158
+ key = addLeadingSlash(key);
159
+ if (typeof path === 'string') {
160
+ this$1.conf.push([key, addLeadingSlash(path)]);
161
+ }
162
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
163
+ (ref = this$1.conf).push.apply(ref, path.map(function (p) { return [key, addLeadingSlash(p)]; }));
164
+ }
165
+ };
166
+
167
+ for (var key in customRoutes) loop( key );
168
+ };
169
+ var routesAlias = new RoutesAlias();
127
170
  var isTabBar = function (config) {
128
171
  var _a, _b;
129
172
  var ref = config.router;
@@ -133,8 +176,14 @@ var isTabBar = function (config) {
133
176
  var routePath = stripBasename(pathname, basename);
134
177
  var pagePath = ((_a = Object.entries(customRoutes).find(function (ref) {
135
178
  var target = ref[1];
136
-
137
- return target === routePath;
179
+
180
+ if (typeof target === 'string') {
181
+ return target === routePath;
182
+ }
183
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
184
+ return target.includes(routePath);
185
+ }
186
+ return false;
138
187
  })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
139
188
  return !!pagePath && (((_b = config.tabBar) === null || _b === void 0 ? void 0 : _b.list) || []).some(function (t) { return t.pagePath === pagePath; });
140
189
  };
@@ -143,11 +192,7 @@ function processNavigateUrl(option) {
143
192
  var _a;
144
193
  var pathPieces = J(option.url);
145
194
  // 处理自定义路由
146
- Object.keys(routesAlias).forEach(function (key) {
147
- if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
148
- pathPieces.pathname = routesAlias[key];
149
- }
150
- });
195
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
151
196
  // 处理相对路径
152
197
  if ((_a = pathPieces === null || pathPieces === void 0 ? void 0 : pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
153
198
  var parts = exports.history.location.pathname.split('/');
@@ -173,9 +218,10 @@ async function navigate(option, method) {
173
218
  var complete = option.complete;
174
219
  var fail = option.fail;
175
220
  var unListen = exports.history.listen(function () {
176
- success === null || success === void 0 ? void 0 : success();
177
- complete === null || complete === void 0 ? void 0 : complete();
178
- resolve();
221
+ var res = { errMsg: (method + ": ok") };
222
+ success === null || success === void 0 ? void 0 : success(res);
223
+ complete === null || complete === void 0 ? void 0 : complete(res);
224
+ resolve(res);
179
225
  unListen();
180
226
  });
181
227
  try {
@@ -185,7 +231,11 @@ async function navigate(option, method) {
185
231
  if (method === 'navigateTo') {
186
232
  exports.history.push(pathPieces, state);
187
233
  }
188
- else if (method === 'redirectTo') {
234
+ else if (method === 'redirectTo' || method === 'switchTab') {
235
+ exports.history.replace(pathPieces, state);
236
+ }
237
+ else if (method === 'reLaunch') {
238
+ stacks.delta = stacks.length;
189
239
  exports.history.replace(pathPieces, state);
190
240
  }
191
241
  }
@@ -195,9 +245,10 @@ async function navigate(option, method) {
195
245
  }
196
246
  }
197
247
  catch (error) {
198
- fail === null || fail === void 0 ? void 0 : fail(error);
199
- complete === null || complete === void 0 ? void 0 : complete();
200
- reject(error);
248
+ var res = { errMsg: (method + ": fail " + (error.message || error)) };
249
+ fail === null || fail === void 0 ? void 0 : fail(res);
250
+ complete === null || complete === void 0 ? void 0 : complete(res);
251
+ reject(res);
201
252
  }
202
253
  });
203
254
  }
@@ -216,15 +267,14 @@ function navigateBack(options) {
216
267
  return navigate(options, 'navigateBack');
217
268
  }
218
269
  function switchTab(option) {
219
- // TODO: 清除掉所有的栈去目标页面
220
- return redirectTo(option);
270
+ return navigate(option, 'switchTab');
221
271
  }
222
272
  function reLaunch(option) {
223
- // TODO: 清除掉所有的栈去目标页面
224
- return redirectTo(option);
273
+ return navigate(option, 'reLaunch');
225
274
  }
226
275
  function getCurrentPages() {
227
- return stacks.get();
276
+ var pages = stacks.get();
277
+ return pages.map(function (e) { return (Object.assign(Object.assign({}, e), { route: e.path || '' })); });
228
278
  }
229
279
 
230
280
  /**
@@ -870,14 +920,14 @@ var UniversalRouter = function () {
870
920
 
871
921
  UniversalRouter.pathToRegexp = pathToRegexp_1;
872
922
 
873
- var Taro = require('@tarojs/taro-h5');
923
+ // @ts-nocheck
874
924
  function initTabbar(config) {
875
925
  if (config.tabBar == null) {
876
926
  return;
877
927
  }
878
928
  // TODO: 找到 tabbar 的类型
879
929
  var tabbar = document.createElement('taro-tabbar');
880
- var homePage = config.pages ? config.pages[0] : '';
930
+ var homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
881
931
  tabbar.conf = config.tabBar;
882
932
  tabbar.conf.homePage = exports.history.location.pathname === '/' ? homePage : exports.history.location.pathname;
883
933
  var routerConfig = config.router;
@@ -896,7 +946,7 @@ function initTabbar(config) {
896
946
  var container = document.getElementById('container');
897
947
  // eslint-disable-next-line no-unused-expressions
898
948
  container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
899
- Taro.initTabBarApis(config);
949
+ taro.initTabBarApis(config);
900
950
  }
901
951
 
902
952
  var routerConfig = Object.create(null);
@@ -1634,14 +1684,13 @@ function createRouter(app, config, framework) {
1634
1684
  var _a;
1635
1685
  init(config);
1636
1686
  var routes = [];
1637
- var alias = (_a = config.router.customRoutes) !== null && _a !== void 0 ? _a : {};
1638
1687
  var runtimeHooks = runtime.container.get(runtime.SERVICE_IDENTIFIER.Hooks);
1639
- setRoutesAlias(alias);
1688
+ routesAlias.set(config.router.customRoutes);
1640
1689
  for (var i = 0; i < config.routes.length; i++) {
1641
1690
  var route = config.routes[i];
1642
1691
  var path = addLeadingSlash(route.path);
1643
1692
  routes.push({
1644
- path: alias[path] || path,
1693
+ path: routesAlias.getAll(path),
1645
1694
  action: route.load
1646
1695
  });
1647
1696
  }
@@ -1671,8 +1720,10 @@ function createRouter(app, config, framework) {
1671
1720
  if (!element)
1672
1721
  { return; }
1673
1722
  var pageConfig = config.routes.find(function (r) {
1723
+ var _a;
1674
1724
  var path = addLeadingSlash(r.path);
1675
- return path === location.pathname || alias[path] === location.pathname;
1725
+ var urlPath = stripBasename(location.pathname, routerConfig.router.basename);
1726
+ return path === urlPath || ((_a = routesAlias.getConfig(path)) === null || _a === void 0 ? void 0 : _a.includes(urlPath));
1676
1727
  });
1677
1728
  var enablePullDownRefresh = false;
1678
1729
  runtime.eventCenter.trigger('__taroRouterChange', {
@@ -1713,7 +1764,9 @@ function createRouter(app, config, framework) {
1713
1764
  }
1714
1765
  }
1715
1766
  else {
1716
- unloadPage(currentPage);
1767
+ var delta$1 = stacks.getDelta(pathname);
1768
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1769
+ unloadPage(currentPage, delta$1);
1717
1770
  }
1718
1771
  shouldLoad = true;
1719
1772
  }
@@ -1728,7 +1781,7 @@ function createRouter(app, config, framework) {
1728
1781
  }
1729
1782
  };
1730
1783
  if (exports.history.location.pathname === '/') {
1731
- exports.history.replace(prependBasename(routes[0].path + exports.history.location.search));
1784
+ exports.history.replace(prependBasename((config.entryPagePath || ((_a = routes[0].path) === null || _a === void 0 ? void 0 : _a[0])) + exports.history.location.search));
1732
1785
  }
1733
1786
  render({ location: exports.history.location, action: r.Push });
1734
1787
  app.onShow(qs(stacks.length));