@tarojs/router 3.3.9 → 3.3.13

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
@@ -27,14 +27,14 @@ function _extends() {
27
27
  var r,B=r||(r={});B.Pop="POP";B.Push="PUSH";B.Replace="REPLACE";var C="production"!==process.env.NODE_ENV?function(b){return Object.freeze(b)}:function(b){return b};function D(b,h){if(!b){"undefined"!==typeof console&&console.warn(h);try{throw Error(h);}catch(k){}}}function E(b){b.preventDefault();b.returnValue="";}
28
28
  function F(){var b=[];return {get length(){return b.length},push:function(h){b.push(h);return function(){b=b.filter(function(k){return k!==h});}},call:function(h){b.forEach(function(k){return k&&k(h)});}}}function H(){return Math.random().toString(36).substr(2,8)}function I(b){var h=b.pathname,k=b.search;b=b.hash;return (void 0===h?"/":h)+(void 0===k?"":k)+(void 0===b?"":b)}
29
29
  function J(b){var h={};if(b){var k=b.indexOf("#");0<=k&&(h.hash=b.substr(k),b=b.substr(0,k));k=b.indexOf("?");0<=k&&(h.search=b.substr(k),b=b.substr(0,k));b&&(h.pathname=b);}return h}
30
- function createBrowserHistory(b){function h(){var c=p.location,a=m.state||{};return [a.idx,C({pathname:c.pathname,search:c.search,hash:c.hash,state:a.usr||null,key:a.key||"default"})]}function k(c){return "string"===typeof c?c:I(c)}function x(c,a){void 0===a&&(a=null);return C(_extends({},q,"string"===typeof c?J(c):c,{state:a,key:H()}))}function z(c){t=c;c=h();v=c[0];q=c[1];d.call({action:t,location:q});}function A(c,a){function e(){A(c,a);}var l=r.Push,g=x(c,a);if(!f.length||(f.call({action:l,
31
- location:g,retry:e}),!1)){var n=[{usr:g.state,key:g.key,idx:v+1},k(g)];g=n[0];n=n[1];try{m.pushState(g,"",n);}catch(G){p.location.assign(n);}z(l);}}function y(c,a){function e(){y(c,a);}var l=r.Replace,g=x(c,a);f.length&&(f.call({action:l,location:g,retry:e}),1)||(g=[{usr:g.state,key:g.key,idx:v},k(g)],m.replaceState(g[0],"",g[1]),z(l));}function w(c){m.go(c);}void 0===b&&(b={});b=b.window;var p=void 0===b?document.defaultView:b,m=p.history,u=null;p.addEventListener("popstate",function(){if(u){ f.call(u),
32
- u=null; }else {var c=r.Pop,a=h(),e=a[0];a=a[1];if(f.length){ if(null!=e){var l=v-e;l&&(u={action:c,location:a,retry:function(){w(-1*l);}},w(l));}else { "production"!==process.env.NODE_ENV?D(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."):void 0; } }else { z(c); }}});var t=
33
- r.Pop;b=h();var v=b[0],q=b[1],d=F(),f=F();null==v&&(v=0,m.replaceState(_extends({},m.state,{idx:v}),""));return {get action(){return t},get location(){return q},createHref:k,push:A,replace:y,go:w,back:function(){w(-1);},forward:function(){w(1);},listen:function(c){return d.push(c)},block:function(c){var a=f.push(c);1===f.length&&p.addEventListener("beforeunload",E);return function(){a();f.length||p.removeEventListener("beforeunload",E);}}}}function createHashHistory(b){function h(){var a=J(m.location.hash.substr(1)),e=a.pathname,l=a.search;a=a.hash;var g=u.state||{};return [g.idx,C({pathname:void 0===e?"/":e,search:void 0===l?"":l,hash:void 0===a?"":a,state:g.usr||null,key:g.key||"default"})]}function k(){if(t){ c.call(t),t=null; }else {var a=r.Pop,e=h(),l=e[0];e=e[1];if(c.length){ if(null!=l){var g=q-l;g&&(t={action:a,location:e,retry:function(){p(-1*g);}},p(g));}else { "production"!==process.env.NODE_ENV?D(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."):
34
- void 0; } }else { A(a); }}}function x(a){var e=document.querySelector("base"),l="";e&&e.getAttribute("href")&&(e=m.location.href,l=e.indexOf("#"),l=-1===l?e:e.slice(0,l));return l+"#"+("string"===typeof a?a:I(a))}function z(a,e){void 0===e&&(e=null);return C(_extends({},d,"string"===typeof a?J(a):a,{state:e,key:H()}))}function A(a){v=a;a=h();q=a[0];d=a[1];f.call({action:v,location:d});}function y(a,e){function l(){y(a,e);}var g=r.Push,n=z(a,e);"production"!==process.env.NODE_ENV?D("/"===n.pathname.charAt(0),
35
- "Relative pathnames are not supported in hash history.push("+JSON.stringify(a)+")"):void 0;if(!c.length||(c.call({action:g,location:n,retry:l}),!1)){var G=[{usr:n.state,key:n.key,idx:q+1},x(n)];n=G[0];G=G[1];try{u.pushState(n,"",G);}catch(K){m.location.assign(G);}A(g);}}function w(a,e){function l(){w(a,e);}var g=r.Replace,n=z(a,e);"production"!==process.env.NODE_ENV?D("/"===n.pathname.charAt(0),"Relative pathnames are not supported in hash history.replace("+JSON.stringify(a)+")"):void 0;c.length&&(c.call({action:g,
36
- location:n,retry:l}),1)||(n=[{usr:n.state,key:n.key,idx:q},x(n)],u.replaceState(n[0],"",n[1]),A(g));}function p(a){u.go(a);}void 0===b&&(b={});b=b.window;var m=void 0===b?document.defaultView:b,u=m.history,t=null;m.addEventListener("popstate",k);m.addEventListener("hashchange",function(){var a=h()[1];I(a)!==I(d)&&k();});var v=r.Pop;b=h();var q=b[0],d=b[1],f=F(),c=F();null==q&&(q=0,u.replaceState(_extends({},u.state,{idx:q}),""));return {get action(){return v},get location(){return d},createHref:x,push:y,
37
- replace:w,go:p,back:function(){p(-1);},forward:function(){p(1);},listen:function(a){return f.push(a)},block:function(a){var e=c.push(a);1===c.length&&m.addEventListener("beforeunload",E);return function(){e();c.length||m.removeEventListener("beforeunload",E);}}}}
30
+ function createBrowserHistory(b){function h(){var c=p.location,a=m.state||{};return [a.idx,C({pathname:c.pathname,search:c.search,hash:c.hash,state:a.usr||null,key:a.key||"default"})]}function k(c){return "string"===typeof c?c:I(c)}function x(c,a){void 0===a&&(a=null);return C(_extends({pathname:q.pathname,hash:"",search:""},"string"===typeof c?J(c):c,{state:a,key:H()}))}function z(c){t=c;c=h();v=c[0];q=c[1];d.call({action:t,location:q});}function A(c,a){function e(){A(c,a);}var l=r.Push,g=x(c,
31
+ a);if(!f.length||(f.call({action:l,location:g,retry:e}),!1)){var n=[{usr:g.state,key:g.key,idx:v+1},k(g)];g=n[0];n=n[1];try{m.pushState(g,"",n);}catch(G){p.location.assign(n);}z(l);}}function y(c,a){function e(){y(c,a);}var l=r.Replace,g=x(c,a);f.length&&(f.call({action:l,location:g,retry:e}),1)||(g=[{usr:g.state,key:g.key,idx:v},k(g)],m.replaceState(g[0],"",g[1]),z(l));}function w(c){m.go(c);}void 0===b&&(b={});b=b.window;var p=void 0===b?document.defaultView:b,m=p.history,u=null;p.addEventListener("popstate",
32
+ function(){if(u){ f.call(u),u=null; }else {var c=r.Pop,a=h(),e=a[0];a=a[1];if(f.length){ if(null!=e){var l=v-e;l&&(u={action:c,location:a,retry:function(){w(-1*l);}},w(l));}else { "production"!==process.env.NODE_ENV?D(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."):
33
+ void 0; } }else { z(c); }}});var t=r.Pop;b=h();var v=b[0],q=b[1],d=F(),f=F();null==v&&(v=0,m.replaceState(_extends({},m.state,{idx:v}),""));return {get action(){return t},get location(){return q},createHref:k,push:A,replace:y,go:w,back:function(){w(-1);},forward:function(){w(1);},listen:function(c){return d.push(c)},block:function(c){var a=f.push(c);1===f.length&&p.addEventListener("beforeunload",E);return function(){a();f.length||p.removeEventListener("beforeunload",E);}}}}function createHashHistory(b){function h(){var a=J(m.location.hash.substr(1)),e=a.pathname,l=a.search;a=a.hash;var g=u.state||{};return [g.idx,C({pathname:void 0===e?"/":e,search:void 0===l?"":l,hash:void 0===a?"":a,state:g.usr||null,key:g.key||"default"})]}function k(){if(t){ c.call(t),t=null; }else {var a=r.Pop,e=h(),l=e[0];e=e[1];if(c.length){ if(null!=l){var g=q-l;g&&(t={action:a,location:e,retry:function(){p(-1*g);}},p(g));}else { "production"!==process.env.NODE_ENV?D(!1,"You are trying to block a POP navigation to a location that was not created by the history library. The block will fail silently in production, but in general you should do all navigation with the history library (instead of using window.history.pushState directly) to avoid this situation."):
34
+ void 0; } }else { A(a); }}}function x(a){var e=document.querySelector("base"),l="";e&&e.getAttribute("href")&&(e=m.location.href,l=e.indexOf("#"),l=-1===l?e:e.slice(0,l));return l+"#"+("string"===typeof a?a:I(a))}function z(a,e){void 0===e&&(e=null);return C(_extends({pathname:d.pathname,hash:"",search:""},"string"===typeof a?J(a):a,{state:e,key:H()}))}function A(a){v=a;a=h();q=a[0];d=a[1];f.call({action:v,location:d});}function y(a,e){function l(){y(a,e);}var g=r.Push,n=z(a,e);"production"!==process.env.NODE_ENV?
35
+ D("/"===n.pathname.charAt(0),"Relative pathnames are not supported in hash history.push("+JSON.stringify(a)+")"):void 0;if(!c.length||(c.call({action:g,location:n,retry:l}),!1)){var G=[{usr:n.state,key:n.key,idx:q+1},x(n)];n=G[0];G=G[1];try{u.pushState(n,"",G);}catch(K){m.location.assign(G);}A(g);}}function w(a,e){function l(){w(a,e);}var g=r.Replace,n=z(a,e);"production"!==process.env.NODE_ENV?D("/"===n.pathname.charAt(0),"Relative pathnames are not supported in hash history.replace("+JSON.stringify(a)+
36
+ ")"):void 0;c.length&&(c.call({action:g,location:n,retry:l}),1)||(n=[{usr:n.state,key:n.key,idx:q},x(n)],u.replaceState(n[0],"",n[1]),A(g));}function p(a){u.go(a);}void 0===b&&(b={});b=b.window;var m=void 0===b?document.defaultView:b,u=m.history,t=null;m.addEventListener("popstate",k);m.addEventListener("hashchange",function(){var a=h()[1];I(a)!==I(d)&&k();});var v=r.Pop;b=h();var q=b[0],d=b[1],f=F(),c=F();null==q&&(q=0,u.replaceState(_extends({},u.state,{idx:q}),""));return {get action(){return v},get location(){return d},
37
+ createHref:x,push:y,replace:w,go:p,back:function(){p(-1);},forward:function(){p(1);},listen:function(a){return f.push(a)},block:function(a){var e=c.push(a);1===c.length&&m.addEventListener("beforeunload",E);return function(){e();c.length||m.removeEventListener("beforeunload",E);}}}}
38
38
 
39
39
  var basename = '/';
40
40
  function setHistoryMode(mode, base) {
@@ -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,58 +87,70 @@ 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
- function navigate(option, method) {
107
- var success = option.success;
108
- var complete = option.complete;
109
- var fail = option.fail;
110
- var failReason;
111
- if (option.url) {
112
- processNavigateUrl(option);
113
- }
114
- 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() });
120
- }
121
- else if (method === 'navigateBack') {
122
- setHistoryBackDelta(option.delta);
123
- exports.history.go(-option.delta);
124
- }
125
- }
126
- catch (error) {
127
- failReason = error;
128
- }
122
+ async function navigate(option, method) {
129
123
  return new Promise(function (resolve, reject) {
130
- if (failReason) {
131
- fail && fail(failReason);
132
- complete && complete();
133
- reject(failReason);
134
- return;
135
- }
136
- var unlisten = exports.history.listen(function () {
137
- success && success();
138
- complete && complete();
124
+ var success = option.success;
125
+ var complete = option.complete;
126
+ var fail = option.fail;
127
+ var unListen = exports.history.listen(function () {
128
+ success === null || success === void 0 ? void 0 : success();
129
+ complete === null || complete === void 0 ? void 0 : complete();
139
130
  resolve();
140
- unlisten();
131
+ unListen();
141
132
  });
133
+ try {
134
+ if ('url' in option) {
135
+ var pathPieces = processNavigateUrl(option);
136
+ var state = { timestamp: Date.now() };
137
+ if (method === 'navigateTo') {
138
+ exports.history.push(pathPieces, state);
139
+ }
140
+ else if (method === 'redirectTo') {
141
+ exports.history.replace(pathPieces, state);
142
+ }
143
+ }
144
+ else if (method === 'navigateBack') {
145
+ setHistoryBackDelta(option.delta);
146
+ exports.history.go(-option.delta);
147
+ }
148
+ }
149
+ catch (error) {
150
+ fail === null || fail === void 0 ? void 0 : fail(error);
151
+ complete === null || complete === void 0 ? void 0 : complete();
152
+ reject(error);
153
+ }
142
154
  });
143
155
  }
144
156
  function navigateTo(option) {
@@ -156,9 +168,11 @@ function navigateBack(options) {
156
168
  return navigate(options, 'navigateBack');
157
169
  }
158
170
  function switchTab(option) {
159
- return navigateTo(option);
171
+ // TODO: 清除掉所有的栈去目标页面
172
+ return redirectTo(option);
160
173
  }
161
174
  function reLaunch(option) {
175
+ // TODO: 清除掉所有的栈去目标页面
162
176
  return redirectTo(option);
163
177
  }
164
178
  function getCurrentPages() {
@@ -828,6 +842,9 @@ function initTabbar(config) {
828
842
  tabbar.conf.custom = false;
829
843
  tabbar.conf.customRoutes = {};
830
844
  }
845
+ if (typeof routerConfig.basename !== 'undefined') {
846
+ tabbar.conf.basename = routerConfig.basename;
847
+ }
831
848
  var container = document.getElementById('container');
832
849
  // eslint-disable-next-line no-unused-expressions
833
850
  container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
@@ -1629,21 +1646,37 @@ function createRouter(app, config, framework) {
1629
1646
  shouldLoad = true;
1630
1647
  }
1631
1648
  else if (action === 'REPLACE') {
1632
- unloadPage(runtime.Current.page);
1649
+ if (isTabBar(config)) {
1650
+ hidePage(runtime.Current.page);
1651
+ var pathname = stripBasename(config.router.pathname, basename);
1652
+ var prevIndex$1 = stacks.findIndex(function (r) {
1653
+ var _a;
1654
+ return ((_a = r.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname;
1655
+ });
1656
+ if (prevIndex$1 > -1) {
1657
+ // tabbar 页且之前出现过,直接复用
1658
+ var prev$1 = stacks[prevIndex$1];
1659
+ return showPage(prev$1, pageConfig, prevIndex$1);
1660
+ }
1661
+ }
1662
+ else {
1663
+ unloadPage(runtime.Current.page);
1664
+ }
1633
1665
  shouldLoad = true;
1634
1666
  }
1635
1667
  if (shouldLoad) {
1636
1668
  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);
1669
+ var loadConfig = Object.assign({}, pageConfig);
1670
+ delete loadConfig['path'];
1671
+ delete loadConfig['load'];
1672
+ var pathname$1 = stripBasename(config.router.pathname, basename);
1673
+ var routerIndex = stacks.length;
1674
+ 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);
1675
+ loadPage(page, pageConfig, routerIndex);
1643
1676
  }
1644
1677
  }, 500);
1645
1678
  if (exports.history.location.pathname === '/') {
1646
- exports.history.replace(parsePath(routes[0].path + exports.history.location.search));
1679
+ exports.history.replace(prependBasename(routes[0].path + exports.history.location.search));
1647
1680
  }
1648
1681
  render({ location: exports.history.location, action: r.Push });
1649
1682
  app.onShow(qs(stacks.length));