@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.
@@ -1,4 +1,5 @@
1
1
  import { requestAnimationFrame, container, SERVICE_IDENTIFIER, eventCenter, Current, createPageConfig, stringify } from '@tarojs/runtime';
2
+ import { initTabBarApis } from '@tarojs/taro';
2
3
 
3
4
  function _extends() {
4
5
  _extends = Object.assign || function (target) {
@@ -78,7 +79,7 @@ class Stacks {
78
79
  }
79
80
  getLastIndex(pathname) {
80
81
  const list = [...this.stacks].reverse();
81
- return list.findIndex(page => { var _a; return ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
82
+ return list.findIndex((page, i) => { var _a; return i !== 0 && ((_a = page.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname; });
82
83
  }
83
84
  getDelta(pathname) {
84
85
  if (this.backDelta >= 1) {
@@ -103,21 +104,65 @@ class Stacks {
103
104
  }
104
105
  const stacks = new Stacks();
105
106
 
106
- let routesAlias = {};
107
- function setRoutesAlias(alias) {
108
- routesAlias = alias;
109
- }
110
107
  function addLeadingSlash(path) {
111
108
  if (path == null) {
112
109
  return '';
113
110
  }
114
111
  return path.charAt(0) === '/' ? path : '/' + path;
115
112
  }
113
+ class RoutesAlias {
114
+ constructor() {
115
+ this.conf = [];
116
+ this.getConfig = (url) => {
117
+ const customRoute = this.conf.filter((arr) => {
118
+ return arr.includes(url);
119
+ });
120
+ return customRoute[0];
121
+ };
122
+ this.getOrigin = (url) => {
123
+ var _a;
124
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[0]) || url;
125
+ };
126
+ this.getAlias = (url) => {
127
+ var _a;
128
+ return ((_a = this.getConfig(url)) === null || _a === void 0 ? void 0 : _a[1]) || url;
129
+ };
130
+ this.getAll = (url) => {
131
+ return this.conf.filter((arr) => {
132
+ return arr.includes(url);
133
+ }).reduce((p, a) => {
134
+ p.push(a[1]);
135
+ return p;
136
+ }, [url]);
137
+ };
138
+ }
139
+ set(customRoutes = {}) {
140
+ for (let key in customRoutes) {
141
+ const path = customRoutes[key];
142
+ key = addLeadingSlash(key);
143
+ if (typeof path === 'string') {
144
+ this.conf.push([key, addLeadingSlash(path)]);
145
+ }
146
+ else if ((path === null || path === void 0 ? void 0 : path.length) > 0) {
147
+ this.conf.push(...path.map(p => [key, addLeadingSlash(p)]));
148
+ }
149
+ }
150
+ }
151
+ }
152
+ const routesAlias = new RoutesAlias();
116
153
  const isTabBar = (config) => {
117
154
  var _a, _b;
118
155
  const { customRoutes = {}, basename = '', pathname } = config.router;
119
156
  const routePath = stripBasename(pathname, basename);
120
- const pagePath = ((_a = Object.entries(customRoutes).find(([, target]) => target === routePath)) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
157
+ const pagePath = ((_a = Object.entries(customRoutes).find(([, target]) => {
158
+ if (typeof target === 'string') {
159
+ return target === routePath;
160
+ }
161
+ else if ((target === null || target === void 0 ? void 0 : target.length) > 0) {
162
+ return target.includes(routePath);
163
+ }
164
+ return false;
165
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
121
166
  return !!pagePath && (((_b = config.tabBar) === null || _b === void 0 ? void 0 : _b.list) || []).some(t => t.pagePath === pagePath);
122
167
  };
123
168
 
@@ -125,11 +170,7 @@ function processNavigateUrl(option) {
125
170
  var _a;
126
171
  const pathPieces = J(option.url);
127
172
  // 处理自定义路由
128
- Object.keys(routesAlias).forEach(key => {
129
- if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
130
- pathPieces.pathname = routesAlias[key];
131
- }
132
- });
173
+ pathPieces.pathname = routesAlias.getAlias(addLeadingSlash(pathPieces.pathname));
133
174
  // 处理相对路径
134
175
  if ((_a = pathPieces === null || pathPieces === void 0 ? void 0 : pathPieces.pathname) === null || _a === void 0 ? void 0 : _a.includes('./')) {
135
176
  const parts = history.location.pathname.split('/');
@@ -153,9 +194,10 @@ async function navigate(option, method) {
153
194
  return new Promise((resolve, reject) => {
154
195
  const { success, complete, fail } = option;
155
196
  const unListen = history.listen(() => {
156
- success === null || success === void 0 ? void 0 : success();
157
- complete === null || complete === void 0 ? void 0 : complete();
158
- resolve();
197
+ const res = { errMsg: `${method}: ok` };
198
+ success === null || success === void 0 ? void 0 : success(res);
199
+ complete === null || complete === void 0 ? void 0 : complete(res);
200
+ resolve(res);
159
201
  unListen();
160
202
  });
161
203
  try {
@@ -165,7 +207,11 @@ async function navigate(option, method) {
165
207
  if (method === 'navigateTo') {
166
208
  history.push(pathPieces, state);
167
209
  }
168
- else if (method === 'redirectTo') {
210
+ else if (method === 'redirectTo' || method === 'switchTab') {
211
+ history.replace(pathPieces, state);
212
+ }
213
+ else if (method === 'reLaunch') {
214
+ stacks.delta = stacks.length;
169
215
  history.replace(pathPieces, state);
170
216
  }
171
217
  }
@@ -175,9 +221,10 @@ async function navigate(option, method) {
175
221
  }
176
222
  }
177
223
  catch (error) {
178
- fail === null || fail === void 0 ? void 0 : fail(error);
179
- complete === null || complete === void 0 ? void 0 : complete();
180
- reject(error);
224
+ const res = { errMsg: `${method}: fail ${error.message || error}` };
225
+ fail === null || fail === void 0 ? void 0 : fail(res);
226
+ complete === null || complete === void 0 ? void 0 : complete(res);
227
+ reject(res);
181
228
  }
182
229
  });
183
230
  }
@@ -194,15 +241,14 @@ function navigateBack(options = { delta: 1 }) {
194
241
  return navigate(options, 'navigateBack');
195
242
  }
196
243
  function switchTab(option) {
197
- // TODO: 清除掉所有的栈去目标页面
198
- return redirectTo(option);
244
+ return navigate(option, 'switchTab');
199
245
  }
200
246
  function reLaunch(option) {
201
- // TODO: 清除掉所有的栈去目标页面
202
- return redirectTo(option);
247
+ return navigate(option, 'reLaunch');
203
248
  }
204
249
  function getCurrentPages() {
205
- return stacks.get();
250
+ const pages = stacks.get();
251
+ return pages.map(e => (Object.assign(Object.assign({}, e), { route: e.path || '' })));
206
252
  }
207
253
 
208
254
  /**
@@ -848,14 +894,14 @@ var UniversalRouter = function () {
848
894
 
849
895
  UniversalRouter.pathToRegexp = pathToRegexp_1;
850
896
 
851
- const Taro = require('@tarojs/taro-h5');
897
+ // @ts-nocheck
852
898
  function initTabbar(config) {
853
899
  if (config.tabBar == null) {
854
900
  return;
855
901
  }
856
902
  // TODO: 找到 tabbar 的类型
857
903
  const tabbar = document.createElement('taro-tabbar');
858
- const homePage = config.pages ? config.pages[0] : '';
904
+ const homePage = config.entryPagePath || (config.pages ? config.pages[0] : '');
859
905
  tabbar.conf = config.tabBar;
860
906
  tabbar.conf.homePage = history.location.pathname === '/' ? homePage : history.location.pathname;
861
907
  const routerConfig = config.router;
@@ -874,7 +920,7 @@ function initTabbar(config) {
874
920
  const container = document.getElementById('container');
875
921
  // eslint-disable-next-line no-unused-expressions
876
922
  container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
877
- Taro.initTabBarApis(config);
923
+ initTabBarApis(config);
878
924
  }
879
925
 
880
926
  const routerConfig = Object.create(null);
@@ -1595,14 +1641,13 @@ function createRouter(app, config, framework) {
1595
1641
  var _a;
1596
1642
  init(config);
1597
1643
  const routes = [];
1598
- const alias = (_a = config.router.customRoutes) !== null && _a !== void 0 ? _a : {};
1599
1644
  const runtimeHooks = container.get(SERVICE_IDENTIFIER.Hooks);
1600
- setRoutesAlias(alias);
1645
+ routesAlias.set(config.router.customRoutes);
1601
1646
  for (let i = 0; i < config.routes.length; i++) {
1602
1647
  const route = config.routes[i];
1603
1648
  const path = addLeadingSlash(route.path);
1604
1649
  routes.push({
1605
- path: alias[path] || path,
1650
+ path: routesAlias.getAll(path),
1606
1651
  action: route.load
1607
1652
  });
1608
1653
  }
@@ -1629,8 +1674,10 @@ function createRouter(app, config, framework) {
1629
1674
  if (!element)
1630
1675
  return;
1631
1676
  const pageConfig = config.routes.find(r => {
1677
+ var _a;
1632
1678
  const path = addLeadingSlash(r.path);
1633
- return path === location.pathname || alias[path] === location.pathname;
1679
+ const urlPath = stripBasename(location.pathname, routerConfig.router.basename);
1680
+ return path === urlPath || ((_a = routesAlias.getConfig(path)) === null || _a === void 0 ? void 0 : _a.includes(urlPath));
1634
1681
  });
1635
1682
  let enablePullDownRefresh = false;
1636
1683
  eventCenter.trigger('__taroRouterChange', {
@@ -1671,7 +1718,9 @@ function createRouter(app, config, framework) {
1671
1718
  }
1672
1719
  }
1673
1720
  else {
1674
- unloadPage(currentPage);
1721
+ const delta = stacks.getDelta(pathname);
1722
+ // NOTE: 页面路由记录并不会清空,只是移除掉缓存的 stack 以及页面
1723
+ unloadPage(currentPage, delta);
1675
1724
  }
1676
1725
  shouldLoad = true;
1677
1726
  }
@@ -1686,7 +1735,7 @@ function createRouter(app, config, framework) {
1686
1735
  }
1687
1736
  };
1688
1737
  if (history.location.pathname === '/') {
1689
- history.replace(prependBasename(routes[0].path + history.location.search));
1738
+ history.replace(prependBasename((config.entryPagePath || ((_a = routes[0].path) === null || _a === void 0 ? void 0 : _a[0])) + history.location.search));
1690
1739
  }
1691
1740
  render({ location: history.location, action: r.Push });
1692
1741
  app.onShow(qs(stacks.length));