@tarojs/router 3.3.11 → 3.3.12

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.
@@ -45,7 +45,7 @@ function setHistoryMode(mode, base = '/') {
45
45
  history = createHashHistory(options);
46
46
  }
47
47
  }
48
- function parsePath(url = '') {
48
+ function prependBasename(url = '') {
49
49
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
50
50
  }
51
51
 
@@ -78,34 +78,44 @@ const throttle = (fn, threshold) => {
78
78
  lastTime = now;
79
79
  }
80
80
  };
81
+ };
82
+ const stripBasename = (path, basename) => path.startsWith(basename) ? path.replace(basename, '') : path;
83
+ const isTabBar = (config) => {
84
+ var _a, _b;
85
+ const { customRoutes = {}, basename = '', pathname } = config.router;
86
+ const routePath = stripBasename(pathname, basename);
87
+ const pagePath = ((_a = Object.entries(customRoutes).find(([, target]) => target === routePath)) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
88
+ return !!pagePath && (((_b = config.tabBar) === null || _b === void 0 ? void 0 : _b.list) || []).some(t => t.pagePath === pagePath);
81
89
  };
82
90
 
83
91
  function processNavigateUrl(option) {
84
- let url = option.url;
85
- const matches = option.url.match(/[?&?].*/);
86
- let parameters = '';
87
- if (matches && matches.length) {
88
- parameters = matches[0];
89
- url = url.replace(parameters, '');
90
- }
92
+ const pathPieces = J(option.url);
93
+ // 处理自定义路由
91
94
  Object.keys(routesAlias).forEach(key => {
92
- if (addLeadingSlash(key) === addLeadingSlash(url)) {
93
- option.url = routesAlias[key] + parameters;
95
+ if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
96
+ pathPieces.pathname = routesAlias[key];
94
97
  }
95
98
  });
99
+ // 处理 basename
100
+ pathPieces.pathname = prependBasename(pathPieces.pathname);
101
+ // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
102
+ if (!pathPieces.search)
103
+ pathPieces.search = '';
104
+ return pathPieces;
96
105
  }
97
106
  function navigate(option, method) {
98
107
  const { success, complete, fail } = option;
99
108
  let failReason;
100
- if (option.url) {
101
- processNavigateUrl(option);
102
- }
103
109
  try {
104
- if (method === 'navigateTo') {
105
- history.push(parsePath(option.url), { timestamp: Date.now() });
106
- }
107
- else if (method === 'redirectTo') {
108
- history.replace(parsePath(option.url), { timestamp: Date.now() });
110
+ if ('url' in option) {
111
+ const pathPieces = processNavigateUrl(option);
112
+ const state = { timestamp: Date.now() };
113
+ if (method === 'navigateTo') {
114
+ history.push(pathPieces, state);
115
+ }
116
+ else if (method === 'redirectTo') {
117
+ history.replace(pathPieces, state);
118
+ }
109
119
  }
110
120
  else if (method === 'navigateBack') {
111
121
  setHistoryBackDelta(option.delta);
@@ -143,9 +153,11 @@ function navigateBack(options = { delta: 1 }) {
143
153
  return navigate(options, 'navigateBack');
144
154
  }
145
155
  function switchTab(option) {
146
- return navigateTo(option);
156
+ // TODO: 清除掉所有的栈去目标页面
157
+ return redirectTo(option);
147
158
  }
148
159
  function reLaunch(option) {
160
+ // TODO: 清除掉所有的栈去目标页面
149
161
  return redirectTo(option);
150
162
  }
151
163
  function getCurrentPages() {
@@ -815,6 +827,9 @@ function initTabbar(config) {
815
827
  tabbar.conf.custom = false;
816
828
  tabbar.conf.customRoutes = {};
817
829
  }
830
+ if (typeof routerConfig.basename !== 'undefined') {
831
+ tabbar.conf.basename = routerConfig.basename;
832
+ }
818
833
  const container = document.getElementById('container');
819
834
  // eslint-disable-next-line no-unused-expressions
820
835
  container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
@@ -1538,7 +1553,7 @@ function createRouter(app, config, framework) {
1538
1553
  const router = new UniversalRouter(routes, { baseUrl: basename || '' });
1539
1554
  app.onLaunch();
1540
1555
  const render = throttle(async ({ location, action }) => {
1541
- var _a, _b, _c, _d, _e;
1556
+ var _a, _b, _c, _d;
1542
1557
  routerConfig.router.pathname = location.pathname;
1543
1558
  let element;
1544
1559
  try {
@@ -1600,33 +1615,37 @@ function createRouter(app, config, framework) {
1600
1615
  shouldLoad = true;
1601
1616
  }
1602
1617
  else if (action === 'REPLACE') {
1603
- unloadPage(Current.page);
1604
- shouldLoad = true;
1605
- }
1606
- if (shouldLoad) {
1607
- const el = (_c = element.default) !== null && _c !== void 0 ? _c : element;
1608
- const config = Object.assign({}, pageConfig);
1609
- delete config['path'];
1610
- delete config['load'];
1611
- const pathname = basename ? location.pathname.replace(basename, '') : location.pathname;
1612
- let routerIndex;
1613
- const isTabBar = (_d = routerConfig.tabBar) === null || _d === void 0 ? void 0 : _d.list.some((TabBarItem) => {
1614
- var _a;
1615
- return ((_a = TabBarItem.pagePath) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname;
1616
- });
1617
- if ((stacks === null || stacks === void 0 ? void 0 : stacks.length) > 0 && isTabBar) {
1618
- routerIndex = stacks.findIndex((r) => {
1618
+ if (isTabBar(config)) {
1619
+ hidePage(Current.page);
1620
+ const pathname = stripBasename(config.router.pathname, basename);
1621
+ const prevIndex = stacks.findIndex((r) => {
1619
1622
  var _a;
1620
1623
  return ((_a = r.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname;
1621
1624
  });
1625
+ if (prevIndex > -1) {
1626
+ // tabbar 页且之前出现过,直接复用
1627
+ const prev = stacks[prevIndex];
1628
+ return showPage(prev, pageConfig, prevIndex);
1629
+ }
1630
+ }
1631
+ else {
1632
+ unloadPage(Current.page);
1622
1633
  }
1623
- routerIndex = ((routerIndex || routerIndex === 0) && routerIndex !== -1) ? routerIndex : stacks.length;
1624
- const page = createPageConfig(enablePullDownRefresh ? (_e = runtimeHooks.createPullDownComponent) === null || _e === void 0 ? void 0 : _e.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname + stringify(qs(routerIndex)), {}, config);
1634
+ shouldLoad = true;
1635
+ }
1636
+ if (shouldLoad) {
1637
+ const el = (_c = element.default) !== null && _c !== void 0 ? _c : element;
1638
+ const loadConfig = Object.assign({}, pageConfig);
1639
+ delete loadConfig['path'];
1640
+ delete loadConfig['load'];
1641
+ const pathname = stripBasename(config.router.pathname, basename);
1642
+ const routerIndex = stacks.length;
1643
+ const page = createPageConfig(enablePullDownRefresh ? (_d = runtimeHooks.createPullDownComponent) === null || _d === void 0 ? void 0 : _d.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname + stringify(qs(routerIndex)), {}, loadConfig);
1625
1644
  loadPage(page, pageConfig, routerIndex);
1626
1645
  }
1627
1646
  }, 500);
1628
1647
  if (history.location.pathname === '/') {
1629
- history.replace(parsePath(routes[0].path + history.location.search));
1648
+ history.replace(prependBasename(routes[0].path + history.location.search));
1630
1649
  }
1631
1650
  render({ location: history.location, action: r.Push });
1632
1651
  app.onShow(qs(stacks.length));