@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 +93 -60
- package/dist/index.js.map +1 -1
- package/dist/router.esm.js +84 -58
- package/dist/router.esm.js.map +1 -1
- package/package.json +4 -4
package/dist/router.esm.js
CHANGED
|
@@ -21,14 +21,14 @@ function _extends() {
|
|
|
21
21
|
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="";}
|
|
22
22
|
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)}
|
|
23
23
|
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}
|
|
24
|
-
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({
|
|
25
|
-
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",
|
|
26
|
-
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."):
|
|
27
|
-
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."):
|
|
28
|
-
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({
|
|
29
|
-
"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)+
|
|
30
|
-
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},
|
|
31
|
-
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);}}}}
|
|
24
|
+
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,
|
|
25
|
+
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",
|
|
26
|
+
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."):
|
|
27
|
+
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."):
|
|
28
|
+
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?
|
|
29
|
+
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)+
|
|
30
|
+
")"):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},
|
|
31
|
+
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);}}}}
|
|
32
32
|
|
|
33
33
|
let history;
|
|
34
34
|
let basename = '/';
|
|
@@ -45,7 +45,7 @@ function setHistoryMode(mode, base = '/') {
|
|
|
45
45
|
history = createHashHistory(options);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
|
-
function
|
|
48
|
+
function prependBasename(url = '') {
|
|
49
49
|
return basename.replace(/\/$/, '') + '/' + url.replace(/^\//, '');
|
|
50
50
|
}
|
|
51
51
|
|
|
@@ -78,56 +78,61 @@ 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
|
-
|
|
85
|
-
|
|
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(
|
|
93
|
-
|
|
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
|
-
function navigate(option, method) {
|
|
98
|
-
const { success, complete, fail } = option;
|
|
99
|
-
let failReason;
|
|
100
|
-
if (option.url) {
|
|
101
|
-
processNavigateUrl(option);
|
|
102
|
-
}
|
|
103
|
-
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() });
|
|
109
|
-
}
|
|
110
|
-
else if (method === 'navigateBack') {
|
|
111
|
-
setHistoryBackDelta(option.delta);
|
|
112
|
-
history.go(-option.delta);
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
catch (error) {
|
|
116
|
-
failReason = error;
|
|
117
|
-
}
|
|
106
|
+
async function navigate(option, method) {
|
|
118
107
|
return new Promise((resolve, reject) => {
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
return;
|
|
124
|
-
}
|
|
125
|
-
const unlisten = history.listen(() => {
|
|
126
|
-
success && success();
|
|
127
|
-
complete && complete();
|
|
108
|
+
const { success, complete, fail } = option;
|
|
109
|
+
const unListen = history.listen(() => {
|
|
110
|
+
success === null || success === void 0 ? void 0 : success();
|
|
111
|
+
complete === null || complete === void 0 ? void 0 : complete();
|
|
128
112
|
resolve();
|
|
129
|
-
|
|
113
|
+
unListen();
|
|
130
114
|
});
|
|
115
|
+
try {
|
|
116
|
+
if ('url' in option) {
|
|
117
|
+
const pathPieces = processNavigateUrl(option);
|
|
118
|
+
const state = { timestamp: Date.now() };
|
|
119
|
+
if (method === 'navigateTo') {
|
|
120
|
+
history.push(pathPieces, state);
|
|
121
|
+
}
|
|
122
|
+
else if (method === 'redirectTo') {
|
|
123
|
+
history.replace(pathPieces, state);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
else if (method === 'navigateBack') {
|
|
127
|
+
setHistoryBackDelta(option.delta);
|
|
128
|
+
history.go(-option.delta);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
fail === null || fail === void 0 ? void 0 : fail(error);
|
|
133
|
+
complete === null || complete === void 0 ? void 0 : complete();
|
|
134
|
+
reject(error);
|
|
135
|
+
}
|
|
131
136
|
});
|
|
132
137
|
}
|
|
133
138
|
function navigateTo(option) {
|
|
@@ -143,9 +148,11 @@ function navigateBack(options = { delta: 1 }) {
|
|
|
143
148
|
return navigate(options, 'navigateBack');
|
|
144
149
|
}
|
|
145
150
|
function switchTab(option) {
|
|
146
|
-
|
|
151
|
+
// TODO: 清除掉所有的栈去目标页面
|
|
152
|
+
return redirectTo(option);
|
|
147
153
|
}
|
|
148
154
|
function reLaunch(option) {
|
|
155
|
+
// TODO: 清除掉所有的栈去目标页面
|
|
149
156
|
return redirectTo(option);
|
|
150
157
|
}
|
|
151
158
|
function getCurrentPages() {
|
|
@@ -815,6 +822,9 @@ function initTabbar(config) {
|
|
|
815
822
|
tabbar.conf.custom = false;
|
|
816
823
|
tabbar.conf.customRoutes = {};
|
|
817
824
|
}
|
|
825
|
+
if (typeof routerConfig.basename !== 'undefined') {
|
|
826
|
+
tabbar.conf.basename = routerConfig.basename;
|
|
827
|
+
}
|
|
818
828
|
const container = document.getElementById('container');
|
|
819
829
|
// eslint-disable-next-line no-unused-expressions
|
|
820
830
|
container === null || container === void 0 ? void 0 : container.appendChild(tabbar);
|
|
@@ -1600,21 +1610,37 @@ function createRouter(app, config, framework) {
|
|
|
1600
1610
|
shouldLoad = true;
|
|
1601
1611
|
}
|
|
1602
1612
|
else if (action === 'REPLACE') {
|
|
1603
|
-
|
|
1613
|
+
if (isTabBar(config)) {
|
|
1614
|
+
hidePage(Current.page);
|
|
1615
|
+
const pathname = stripBasename(config.router.pathname, basename);
|
|
1616
|
+
const prevIndex = stacks.findIndex((r) => {
|
|
1617
|
+
var _a;
|
|
1618
|
+
return ((_a = r.path) === null || _a === void 0 ? void 0 : _a.replace(/\?.*/g, '')) === pathname;
|
|
1619
|
+
});
|
|
1620
|
+
if (prevIndex > -1) {
|
|
1621
|
+
// tabbar 页且之前出现过,直接复用
|
|
1622
|
+
const prev = stacks[prevIndex];
|
|
1623
|
+
return showPage(prev, pageConfig, prevIndex);
|
|
1624
|
+
}
|
|
1625
|
+
}
|
|
1626
|
+
else {
|
|
1627
|
+
unloadPage(Current.page);
|
|
1628
|
+
}
|
|
1604
1629
|
shouldLoad = true;
|
|
1605
1630
|
}
|
|
1606
1631
|
if (shouldLoad) {
|
|
1607
1632
|
const el = (_c = element.default) !== null && _c !== void 0 ? _c : element;
|
|
1608
|
-
const
|
|
1609
|
-
delete
|
|
1610
|
-
delete
|
|
1611
|
-
const pathname =
|
|
1612
|
-
const
|
|
1613
|
-
|
|
1633
|
+
const loadConfig = Object.assign({}, pageConfig);
|
|
1634
|
+
delete loadConfig['path'];
|
|
1635
|
+
delete loadConfig['load'];
|
|
1636
|
+
const pathname = stripBasename(config.router.pathname, basename);
|
|
1637
|
+
const routerIndex = stacks.length;
|
|
1638
|
+
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);
|
|
1639
|
+
loadPage(page, pageConfig, routerIndex);
|
|
1614
1640
|
}
|
|
1615
1641
|
}, 500);
|
|
1616
1642
|
if (history.location.pathname === '/') {
|
|
1617
|
-
history.replace(
|
|
1643
|
+
history.replace(prependBasename(routes[0].path + history.location.search));
|
|
1618
1644
|
}
|
|
1619
1645
|
render({ location: history.location, action: r.Push });
|
|
1620
1646
|
app.onShow(qs(stacks.length));
|