@tarojs/router 3.3.8 → 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.
package/dist/index.js CHANGED
@@ -52,7 +52,7 @@ function setHistoryMode(mode, base) {
52
52
  exports.history = createHashHistory(options);
53
53
  }
54
54
  }
55
- function parsePath(url) {
55
+ function prependBasename(url) {
56
56
  if ( url === void 0 ) url = '';
57
57
 
58
58
  return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
@@ -87,36 +87,53 @@ var throttle = function (fn, threshold) {
87
87
  lastTime = now;
88
88
  }
89
89
  };
90
+ };
91
+ var stripBasename = function (path, basename) { return path.startsWith(basename) ? path.replace(basename, '') : path; };
92
+ var isTabBar = function (config) {
93
+ var _a, _b;
94
+ var ref = config.router;
95
+ var customRoutes = ref.customRoutes; if ( customRoutes === void 0 ) customRoutes = {};
96
+ var basename = ref.basename; if ( basename === void 0 ) basename = '';
97
+ var pathname = ref.pathname;
98
+ var routePath = stripBasename(pathname, basename);
99
+ var pagePath = ((_a = Object.entries(customRoutes).find(function (ref) {
100
+ var target = ref[1];
101
+
102
+ return target === routePath;
103
+ })) === null || _a === void 0 ? void 0 : _a[0]) || routePath;
104
+ return !!pagePath && (((_b = config.tabBar) === null || _b === void 0 ? void 0 : _b.list) || []).some(function (t) { return t.pagePath === pagePath; });
90
105
  };
91
106
 
92
107
  function processNavigateUrl(option) {
93
- var url = option.url;
94
- var matches = option.url.match(/[?&?].*/);
95
- var parameters = '';
96
- if (matches && matches.length) {
97
- parameters = matches[0];
98
- url = url.replace(parameters, '');
99
- }
108
+ var pathPieces = J(option.url);
109
+ // 处理自定义路由
100
110
  Object.keys(routesAlias).forEach(function (key) {
101
- if (addLeadingSlash(key) === addLeadingSlash(url)) {
102
- option.url = routesAlias[key] + parameters;
111
+ if (addLeadingSlash(key) === addLeadingSlash(pathPieces.pathname)) {
112
+ pathPieces.pathname = routesAlias[key];
103
113
  }
104
114
  });
115
+ // 处理 basename
116
+ pathPieces.pathname = prependBasename(pathPieces.pathname);
117
+ // hack fix history v5 bug: https://github.com/remix-run/history/issues/814
118
+ if (!pathPieces.search)
119
+ { pathPieces.search = ''; }
120
+ return pathPieces;
105
121
  }
106
122
  function navigate(option, method) {
107
123
  var success = option.success;
108
124
  var complete = option.complete;
109
125
  var fail = option.fail;
110
126
  var failReason;
111
- if (option.url) {
112
- processNavigateUrl(option);
113
- }
114
127
  try {
115
- if (method === 'navigateTo') {
116
- exports.history.push(parsePath(option.url), { timestamp: Date.now() });
117
- }
118
- else if (method === 'redirectTo') {
119
- exports.history.replace(parsePath(option.url), { timestamp: Date.now() });
128
+ if ('url' in option) {
129
+ var pathPieces = processNavigateUrl(option);
130
+ var state = { timestamp: Date.now() };
131
+ if (method === 'navigateTo') {
132
+ exports.history.push(pathPieces, state);
133
+ }
134
+ else if (method === 'redirectTo') {
135
+ exports.history.replace(pathPieces, state);
136
+ }
120
137
  }
121
138
  else if (method === 'navigateBack') {
122
139
  setHistoryBackDelta(option.delta);
@@ -156,9 +173,11 @@ function navigateBack(options) {
156
173
  return navigate(options, 'navigateBack');
157
174
  }
158
175
  function switchTab(option) {
159
- return navigateTo(option);
176
+ // TODO: 清除掉所有的栈去目标页面
177
+ return redirectTo(option);
160
178
  }
161
179
  function reLaunch(option) {
180
+ // TODO: 清除掉所有的栈去目标页面
162
181
  return redirectTo(option);
163
182
  }
164
183
  function getCurrentPages() {
@@ -828,6 +847,9 @@ function initTabbar(config) {
828
847
  tabbar.conf.custom = false;
829
848
  tabbar.conf.customRoutes = {};
830
849
  }
850
+ if (typeof routerConfig.basename !== 'undefined') {
851
+ tabbar.conf.basename = routerConfig.basename;
852
+ }
831
853
  var container = document.getElementById('container');
832
854
  // eslint-disable-next-line no-unused-expressions
833
855
  container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
@@ -1629,21 +1651,37 @@ function createRouter(app, config, framework) {
1629
1651
  shouldLoad = true;
1630
1652
  }
1631
1653
  else if (action === 'REPLACE') {
1632
- unloadPage(runtime.Current.page);
1654
+ if (isTabBar(config)) {
1655
+ hidePage(runtime.Current.page);
1656
+ var pathname = stripBasename(config.router.pathname, basename);
1657
+ var prevIndex$1 = stacks.findIndex(function (r) {
1658
+ var _a;
1659
+ return ((_a = r.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname;
1660
+ });
1661
+ if (prevIndex$1 > -1) {
1662
+ // tabbar 页且之前出现过,直接复用
1663
+ var prev$1 = stacks[prevIndex$1];
1664
+ return showPage(prev$1, pageConfig, prevIndex$1);
1665
+ }
1666
+ }
1667
+ else {
1668
+ unloadPage(runtime.Current.page);
1669
+ }
1633
1670
  shouldLoad = true;
1634
1671
  }
1635
1672
  if (shouldLoad) {
1636
1673
  var el = (_c = element.default) !== null && _c !== void 0 ? _c : element;
1637
- var config$1 = Object.assign({}, pageConfig);
1638
- delete config$1['path'];
1639
- delete config$1['load'];
1640
- var pathname = basename ? location.pathname.replace(basename, '') : location.pathname;
1641
- var page = runtime.createPageConfig(enablePullDownRefresh ? (_d = runtimeHooks.createPullDownComponent) === null || _d === void 0 ? void 0 : _d.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname + runtime.stringify(qs(stacks.length)), {}, config$1);
1642
- loadPage(page, pageConfig, stacks.length);
1674
+ var loadConfig = Object.assign({}, pageConfig);
1675
+ delete loadConfig['path'];
1676
+ delete loadConfig['load'];
1677
+ var pathname$1 = stripBasename(config.router.pathname, basename);
1678
+ var routerIndex = stacks.length;
1679
+ var page = runtime.createPageConfig(enablePullDownRefresh ? (_d = runtimeHooks.createPullDownComponent) === null || _d === void 0 ? void 0 : _d.call(runtimeHooks, el, location.pathname, framework, routerConfig.PullDownRefresh) : el, pathname$1 + runtime.stringify(qs(routerIndex)), {}, loadConfig);
1680
+ loadPage(page, pageConfig, routerIndex);
1643
1681
  }
1644
1682
  }, 500);
1645
1683
  if (exports.history.location.pathname === '/') {
1646
- exports.history.replace(parsePath(routes[0].path + exports.history.location.search));
1684
+ exports.history.replace(prependBasename(routes[0].path + exports.history.location.search));
1647
1685
  }
1648
1686
  render({ location: exports.history.location, action: r.Push });
1649
1687
  app.onShow(qs(stacks.length));