@lytjs/router 4.0.1 → 4.0.5

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.cjs CHANGED
@@ -1 +1 @@
1
- var E=Object.defineProperty;var k=Object.getOwnPropertyDescriptor;var S=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var z=(e,t)=>{for(var r in t)E(e,r,{get:t[r],enumerable:!0})},K=(e,t,r,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of S(t))!U.call(e,u)&&u!==r&&E(e,u,{get:()=>t[u],enumerable:!(a=k(t,u))||a.enumerable});return e};var $=e=>K(E({},"__esModule",{value:!0}),e);var B={};z(B,{createHashHistory:()=>w,createNavigationGuards:()=>x,createRouteMatcher:()=>y,createRouter:()=>O,createWebHistory:()=>m,runAfterGuards:()=>N,runGuards:()=>v});module.exports=$(B);function A(e){let t=[],r=!1,a=e.split("/"),u=[];for(let c=0;c<a.length;c++){let n=a[c];if(n!=="")if(n==="*")u.push("(.+)"),t.push("*"),r=!0;else if(n.startsWith(":")){let i=n.slice(1);u.push("([^/]+)"),t.push(i)}else u.push(D(n))}let s="^/"+u.join("/")+"$";return{regex:new RegExp(s),paramKeys:t,isWildcard:r}}function D(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function T(e,t=""){let r=H(t,e.path),{regex:a,paramKeys:u,isWildcard:s}=A(r);if(e._regex=a,e._paramKeys=u,e._isWildcard=s,e.children)for(let c of e.children)T(c,r)}function H(e,t){let r=e.replace(/\/+$/,""),a=t.replace(/^\/+/,"");return a?r+"/"+a:r||"/"}function y(e){let t=[];function r(n,i=""){for(let o of n)if(T(o,i),t.push(o),o.children){let d=H(i,o.path);r(o.children,d)}}r(e);function a(n){let i=n.replace(/\/+$/,"")||"/";for(let o of t){if(!o._regex)continue;let d=i.match(o._regex);if(d){let g={};if(o._paramKeys)for(let f=0;f<o._paramKeys.length;f++){let p=o._paramKeys[f];g[p]=d[f+1]||""}return{record:o,params:g,matchedPath:i}}}return null}function u(n){if(T(n),t.push(n),n.children){let i=H("",n.path);r(n.children,i)}}function s(n){for(let i=t.length-1;i>=0;i--)t[i].name===n&&t.splice(i,1)}function c(){return[...t]}return{matchRoute:a,addRoute:u,removeRoute:s,getRoutes:c}}function j(e){let t={};if(!e||e==="?")return t;let a=(e.startsWith("?")?e.slice(1):e).split("&");for(let u of a){let s=u.indexOf("=");if(s===-1)t[decodeURIComponent(u)]="";else{let c=decodeURIComponent(u.slice(0,s)),n=decodeURIComponent(u.slice(s+1));t[c]=n}}return t}function q(e){let t=[];for(let r of Object.keys(e)){let a=e[r];a!=null&&t.push(encodeURIComponent(r)+"="+encodeURIComponent(a))}return t.join("&")}function I(e){let t="",r=e,a=e.indexOf("#");a!==-1&&(t=e.slice(a+1),r=e.slice(0,a));let u=r,s={},c=r.indexOf("?");return c!==-1&&(u=r.slice(0,c),s=j(r.slice(c))),{path:u,query:s,hash:t}}function h(e){return e.startsWith("/")||(e="/"+e),e.replace(/\/+/g,"/")}function m(e="/"){let t=h(e),r=[],a=s(window.location.href);function u(n){let i=s(window.location.href);i.fromPopState=!0,i.state=n.state;let o=a;a=i,c(i,o)}function s(n){let i;try{i=new URL(n)}catch(p){i=new URL(n,window.location.origin)}let o=i.pathname;t!=="/"&&o.startsWith(t)&&(o=o.slice(t.length)||"/");let{path:d,query:g,hash:f}=I(o+i.hash);return{path:h(d),fullPath:h(d)+(Object.keys(g).length?"?"+q(g):"")+(f?"#"+f:""),query:g,hash:f,state:i.state||null,fromPopState:!1}}function c(n,i){for(let o of r)o(n,i)}return window.addEventListener("popstate",u),{base:t,get location(){return a},push(n,i){let o=h(n),d=a,g=(t+o).replace(/\/+/g,"/");window.history.pushState(i||null,"",g),a=s(window.location.href),a.state=i||null,c(a,d)},replace(n,i){let o=h(n),d=a,g=(t+o).replace(/\/+/g,"/");window.history.replaceState(i||null,"",g),a=s(window.location.href),a.state=i||null,c(a,d)},go(n){window.history.go(n)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return s(window.location.href)},listen(n){return r.push(n),()=>{let i=r.indexOf(n);i!==-1&&r.splice(i,1)}},destroy(){window.removeEventListener("popstate",u),r.length=0}}}function w(){let e=[],t=a();function r(){let s=a();s.fromPopState=!0;let c=t;t=s,u(s,c)}function a(){let c=window.location.hash.slice(1)||"/",{path:n,query:i,hash:o}=I(c);return{path:h(n),fullPath:h(n)+(Object.keys(i).length?"?"+q(i):"")+(o?"#"+o:""),query:i,hash:o,state:null,fromPopState:!1}}function u(s,c){for(let n of e)n(s,c)}return window.addEventListener("hashchange",r),{base:"",get location(){return t},push(s,c){let n=h(s),i=t;window.location.hash=n,t=a(),u(t,i)},replace(s,c){let n=h(s),i=t;window.location.replace(window.location.pathname+window.location.search+"#"+n),t=a(),u(t,i)},go(s){window.history.go(s)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return a()},listen(s){return e.push(s),()=>{let c=e.indexOf(s);c!==-1&&e.splice(c,1)}},destroy(){window.removeEventListener("hashchange",r),e.length=0}}}function x(){let e=[],t=[],r=[];function a(c){return e.push(c),()=>{let n=e.indexOf(c);n!==-1&&e.splice(n,1)}}function u(c){return t.push(c),()=>{let n=t.indexOf(c);n!==-1&&t.splice(n,1)}}function s(c){return r.push(c),()=>{let n=r.indexOf(c);n!==-1&&r.splice(n,1)}}return{_beforeEachGuards:e,_beforeResolveGuards:t,_afterEachGuards:r,beforeEach:a,beforeResolve:u,afterEach:s}}function v(e,t,r){return new Promise((a,u)=>{let s=0;function c(){let i=!1;return o=>{i||(i=!0,o===!1?u(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):typeof o=="string"?u(new Error("REDIRECT:"+o)):(s++,n()))}}function n(){if(s>=e.length){a();return}let i=e[s],o=c();try{let d=i(t,r,o);d!=null&&typeof d=="object"&&typeof d.then=="function"&&d.then(()=>{}).catch(g=>{!g||typeof g.message!="string"||!g.message.startsWith("REDIRECT:")?u(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):u(g)})}catch(d){u(new Error("\u5BFC\u822A\u5B88\u536B\u6267\u884C\u51FA\u9519"))}}n()})}function N(e,t,r){for(let a of e)try{a(t,r)}catch(u){}}var b=require("@lytjs/reactivity");function Q(e,t){return{path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash}}function C(e,t){return(0,b.reactive)({path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash,matched:t?[t.record]:[]})}function O(e){let t=y(e.routes),r=e.mode==="hash"?w():m(e.base||"/"),a=x(),u=(0,b.ref)(C(r.location,t.matchRoute(r.location.path))),s=!1,c=r.listen(async(o,d)=>{await n(o.path,d,!0)});async function n(o,d,g=!1){var f,p;if(!s){s=!0;try{let _=t.matchRoute(o),M={path:o,fullPath:o,query:{},hash:"",state:null,fromPopState:g},L=Q(M,_),R=u.value,P={path:R.path,fullPath:R.fullPath,params:R.params,name:R.name,meta:R.meta,query:R.query,hash:R.hash};try{await v(a._beforeEachGuards,L,P)}catch(l){if((f=l==null?void 0:l.message)!=null&&f.startsWith("REDIRECT:")){let G=l.message.replace("REDIRECT:","");s=!1,g||r.replace(G);return}return}try{await v(a._beforeResolveGuards,L,P)}catch(l){if((p=l==null?void 0:l.message)!=null&&p.startsWith("REDIRECT:")){let G=l.message.replace("REDIRECT:","");s=!1,g||r.replace(G);return}return}let W=C(M,_);u.value=W,N(a._afterEachGuards,L,P)}finally{s=!1}}}let i={currentRoute:u,async push(o){r.push(o)},async replace(o){r.replace(o)},go(o){r.go(o)},back(){r.back()},forward(){r.forward()},beforeEach(o){return a.beforeEach(o)},afterEach(o){return a.afterEach(o)},beforeResolve(o){return a.beforeResolve(o)},addRoute(o){t.addRoute(o)},removeRoute(o){t.removeRoute(o)},getRoutes(){return t.getRoutes()},install(o){o.config&&o.config.globalProperties&&(o.config.globalProperties.$router=i,Object.defineProperty(o.config.globalProperties,"$route",{get(){return u.value}})),o.provide&&(o.provide("router",i),o.provide("route",u));let d=t.matchRoute(r.location.path);u.value=C(r.location,d)},destroy(){c(),r.destroy()}};return i}
1
+ var T=Object.defineProperty;var S=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var U=Object.prototype.hasOwnProperty;var z=(e,t)=>{for(var r in t)T(e,r,{get:t[r],enumerable:!0})},A=(e,t,r,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let u of W(t))!U.call(e,u)&&u!==r&&T(e,u,{get:()=>t[u],enumerable:!(i=S(t,u))||i.enumerable});return e};var D=e=>A(T({},"__esModule",{value:!0}),e);var j={};z(j,{createHashHistory:()=>x,createNavigationGuards:()=>N,createRouteMatcher:()=>m,createRouter:()=>O,createWebHistory:()=>w,runAfterGuards:()=>b,runGuards:()=>y});module.exports=D(j);var p=require("@lytjs/common");function H(e,t=""){let r=(0,p.normalizePath)(t,e.path),{regex:i,paramKeys:u,isWildcard:s}=(0,p.pathToRegex)(r);if(e._regex=i,e._paramKeys=u,e._isWildcard=s,e.children)for(let c of e.children)H(c,r)}function m(e){let t=[];function r(n,a=""){for(let o of n)if(H(o,a),t.push(o),o.children){let d=(0,p.normalizePath)(a,o.path);r(o.children,d)}}r(e);function i(n){let a=n.replace(/\/+$/,"")||"/";for(let o of t){if(!o._regex)continue;let d=a.match(o._regex);if(d){let f={};if(o._paramKeys)for(let g=0;g<o._paramKeys.length;g++){let v=o._paramKeys[g];f[v]=d[g+1]||""}return{record:o,params:f,matchedPath:a}}}return null}function u(n){if(H(n),t.push(n),n.children){let a=(0,p.normalizePath)("",n.path);r(n.children,a)}}function s(n){for(let a=t.length-1;a>=0;a--)t[a].name===n&&t.splice(a,1)}function c(){return[...t]}return{matchRoute:i,addRoute:u,removeRoute:s,getRoutes:c}}function K(e){let t={};if(!e||e==="?")return t;let i=(e.startsWith("?")?e.slice(1):e).split("&");for(let u of i){let s=u.indexOf("=");if(s===-1)t[decodeURIComponent(u)]="";else{let c=decodeURIComponent(u.slice(0,s)),n=decodeURIComponent(u.slice(s+1));t[c]=n}}return t}function q(e){let t=[];for(let r of Object.keys(e)){let i=e[r];i!=null&&t.push(encodeURIComponent(r)+"="+encodeURIComponent(i))}return t.join("&")}function I(e){let t="",r=e,i=e.indexOf("#");i!==-1&&(t=e.slice(i+1),r=e.slice(0,i));let u=r,s={},c=r.indexOf("?");return c!==-1&&(u=r.slice(0,c),s=K(r.slice(c))),{path:u,query:s,hash:t}}function h(e){return e.startsWith("/")||(e="/"+e),e.replace(/\/+/g,"/")}function w(e="/"){let t=h(e),r=[],i=s(window.location.href);function u(n){let a=s(window.location.href);a.fromPopState=!0,a.state=n.state;let o=i;i=a,c(a,o)}function s(n){let a;try{a=new URL(n)}catch(v){a=new URL(n,window.location.origin)}let o=a.pathname;t!=="/"&&o.startsWith(t)&&(o=o.slice(t.length)||"/");let{path:d,query:f,hash:g}=I(o+a.hash);return{path:h(d),fullPath:h(d)+(Object.keys(f).length?"?"+q(f):"")+(g?"#"+g:""),query:f,hash:g,state:a.state||null,fromPopState:!1}}function c(n,a){for(let o of r)o(n,a)}return window.addEventListener("popstate",u),{base:t,get location(){return i},push(n,a){let o=h(n),d=i,f=(t+o).replace(/\/+/g,"/");window.history.pushState(a||null,"",f),i=s(window.location.href),i.state=a||null,c(i,d)},replace(n,a){let o=h(n),d=i,f=(t+o).replace(/\/+/g,"/");window.history.replaceState(a||null,"",f),i=s(window.location.href),i.state=a||null,c(i,d)},go(n){window.history.go(n)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return s(window.location.href)},listen(n){return r.push(n),()=>{let a=r.indexOf(n);a!==-1&&r.splice(a,1)}},destroy(){window.removeEventListener("popstate",u),r.length=0}}}function x(){let e=[],t=i();function r(){let s=i();s.fromPopState=!0;let c=t;t=s,u(s,c)}function i(){let c=window.location.hash.slice(1)||"/",{path:n,query:a,hash:o}=I(c);return{path:h(n),fullPath:h(n)+(Object.keys(a).length?"?"+q(a):"")+(o?"#"+o:""),query:a,hash:o,state:null,fromPopState:!1}}function u(s,c){for(let n of e)n(s,c)}return window.addEventListener("hashchange",r),{base:"",get location(){return t},push(s,c){let n=h(s),a=t;window.location.hash=n,t=i(),u(t,a)},replace(s,c){let n=h(s),a=t;window.location.replace(window.location.pathname+window.location.search+"#"+n),t=i(),u(t,a)},go(s){window.history.go(s)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return i()},listen(s){return e.push(s),()=>{let c=e.indexOf(s);c!==-1&&e.splice(c,1)}},destroy(){window.removeEventListener("hashchange",r),e.length=0}}}function N(){let e=[],t=[],r=[];function i(c){return e.push(c),()=>{let n=e.indexOf(c);n!==-1&&e.splice(n,1)}}function u(c){return t.push(c),()=>{let n=t.indexOf(c);n!==-1&&t.splice(n,1)}}function s(c){return r.push(c),()=>{let n=r.indexOf(c);n!==-1&&r.splice(n,1)}}return{_beforeEachGuards:e,_beforeResolveGuards:t,_afterEachGuards:r,beforeEach:i,beforeResolve:u,afterEach:s}}function y(e,t,r){return new Promise((i,u)=>{let s=0;function c(){let a=!1;return o=>{a||(a=!0,o===!1?u(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):typeof o=="string"?u(new Error("REDIRECT:"+o)):(s++,n()))}}function n(){if(s>=e.length){i();return}let a=e[s],o=c();try{let d=a(t,r,o);d!=null&&typeof d=="object"&&typeof d.then=="function"&&d.then(()=>{}).catch(f=>{!f||typeof f.message!="string"||!f.message.startsWith("REDIRECT:")?u(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):u(f)})}catch(d){u(new Error("\u5BFC\u822A\u5B88\u536B\u6267\u884C\u51FA\u9519"))}}n()})}function b(e,t,r){for(let i of e)try{i(t,r)}catch(u){}}var L=require("@lytjs/reactivity");function $(e,t){return{path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash}}function C(e,t){return(0,L.reactive)({path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash,matched:t?[t.record]:[]})}function O(e){let t=m(e.routes),r=e.mode==="hash"?x():w(e.base||"/"),i=N(),u=(0,L.ref)(C(r.location,t.matchRoute(r.location.path))),s=!1,c=r.listen(async(o,d)=>{await n(o.path,d,!0)});async function n(o,d,f=!1){var g,v;if(!s){s=!0;try{let _=t.matchRoute(o),M={path:o,fullPath:o,query:{},hash:"",state:null,fromPopState:f},G=$(M,_),R=u.value,P={path:R.path,fullPath:R.fullPath,params:R.params,name:R.name,meta:R.meta,query:R.query,hash:R.hash};try{await y(i._beforeEachGuards,G,P)}catch(l){if((g=l==null?void 0:l.message)!=null&&g.startsWith("REDIRECT:")){let E=l.message.replace("REDIRECT:","");s=!1,f||r.replace(E);return}return}try{await y(i._beforeResolveGuards,G,P)}catch(l){if((v=l==null?void 0:l.message)!=null&&v.startsWith("REDIRECT:")){let E=l.message.replace("REDIRECT:","");s=!1,f||r.replace(E);return}return}let k=C(M,_);u.value=k,b(i._afterEachGuards,G,P)}finally{s=!1}}}let a={currentRoute:u,async push(o){r.push(o)},async replace(o){r.replace(o)},go(o){r.go(o)},back(){r.back()},forward(){r.forward()},beforeEach(o){return i.beforeEach(o)},afterEach(o){return i.afterEach(o)},beforeResolve(o){return i.beforeResolve(o)},addRoute(o){t.addRoute(o)},removeRoute(o){t.removeRoute(o)},getRoutes(){return t.getRoutes()},install(o){o.config&&o.config.globalProperties&&(o.config.globalProperties.$router=a,Object.defineProperty(o.config.globalProperties,"$route",{get(){return u.value}})),o.provide&&(o.provide("router",a),o.provide("route",u));let d=t.matchRoute(r.location.path);u.value=C(r.location,d)},destroy(){c(),r.destroy()}};return a}
package/dist/index.mjs CHANGED
@@ -1 +1 @@
1
- function I(e){let t=[],n=!1,i=e.split("/"),c=[];for(let u=0;u<i.length;u++){let r=i[u];if(r!=="")if(r==="*")c.push("(.+)"),t.push("*"),n=!0;else if(r.startsWith(":")){let a=r.slice(1);c.push("([^/]+)"),t.push(a)}else c.push(O(r))}let s="^/"+c.join("/")+"$";return{regex:new RegExp(s),paramKeys:t,isWildcard:n}}function O(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function x(e,t=""){let n=N(t,e.path),{regex:i,paramKeys:c,isWildcard:s}=I(n);if(e._regex=i,e._paramKeys=c,e._isWildcard=s,e.children)for(let u of e.children)x(u,n)}function N(e,t){let n=e.replace(/\/+$/,""),i=t.replace(/^\/+/,"");return i?n+"/"+i:n||"/"}function b(e){let t=[];function n(r,a=""){for(let o of r)if(x(o,a),t.push(o),o.children){let d=N(a,o.path);n(o.children,d)}}n(e);function i(r){let a=r.replace(/\/+$/,"")||"/";for(let o of t){if(!o._regex)continue;let d=a.match(o._regex);if(d){let g={};if(o._paramKeys)for(let f=0;f<o._paramKeys.length;f++){let p=o._paramKeys[f];g[p]=d[f+1]||""}return{record:o,params:g,matchedPath:a}}}return null}function c(r){if(x(r),t.push(r),r.children){let a=N("",r.path);n(r.children,a)}}function s(r){for(let a=t.length-1;a>=0;a--)t[a].name===r&&t.splice(a,1)}function u(){return[...t]}return{matchRoute:i,addRoute:c,removeRoute:s,getRoutes:u}}function W(e){let t={};if(!e||e==="?")return t;let i=(e.startsWith("?")?e.slice(1):e).split("&");for(let c of i){let s=c.indexOf("=");if(s===-1)t[decodeURIComponent(c)]="";else{let u=decodeURIComponent(c.slice(0,s)),r=decodeURIComponent(c.slice(s+1));t[u]=r}}return t}function _(e){let t=[];for(let n of Object.keys(e)){let i=e[n];i!=null&&t.push(encodeURIComponent(n)+"="+encodeURIComponent(i))}return t.join("&")}function M(e){let t="",n=e,i=e.indexOf("#");i!==-1&&(t=e.slice(i+1),n=e.slice(0,i));let c=n,s={},u=n.indexOf("?");return u!==-1&&(c=n.slice(0,u),s=W(n.slice(u))),{path:c,query:s,hash:t}}function h(e){return e.startsWith("/")||(e="/"+e),e.replace(/\/+/g,"/")}function L(e="/"){let t=h(e),n=[],i=s(window.location.href);function c(r){let a=s(window.location.href);a.fromPopState=!0,a.state=r.state;let o=i;i=a,u(a,o)}function s(r){let a;try{a=new URL(r)}catch(p){a=new URL(r,window.location.origin)}let o=a.pathname;t!=="/"&&o.startsWith(t)&&(o=o.slice(t.length)||"/");let{path:d,query:g,hash:f}=M(o+a.hash);return{path:h(d),fullPath:h(d)+(Object.keys(g).length?"?"+_(g):"")+(f?"#"+f:""),query:g,hash:f,state:a.state||null,fromPopState:!1}}function u(r,a){for(let o of n)o(r,a)}return window.addEventListener("popstate",c),{base:t,get location(){return i},push(r,a){let o=h(r),d=i,g=(t+o).replace(/\/+/g,"/");window.history.pushState(a||null,"",g),i=s(window.location.href),i.state=a||null,u(i,d)},replace(r,a){let o=h(r),d=i,g=(t+o).replace(/\/+/g,"/");window.history.replaceState(a||null,"",g),i=s(window.location.href),i.state=a||null,u(i,d)},go(r){window.history.go(r)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return s(window.location.href)},listen(r){return n.push(r),()=>{let a=n.indexOf(r);a!==-1&&n.splice(a,1)}},destroy(){window.removeEventListener("popstate",c),n.length=0}}}function P(){let e=[],t=i();function n(){let s=i();s.fromPopState=!0;let u=t;t=s,c(s,u)}function i(){let u=window.location.hash.slice(1)||"/",{path:r,query:a,hash:o}=M(u);return{path:h(r),fullPath:h(r)+(Object.keys(a).length?"?"+_(a):"")+(o?"#"+o:""),query:a,hash:o,state:null,fromPopState:!1}}function c(s,u){for(let r of e)r(s,u)}return window.addEventListener("hashchange",n),{base:"",get location(){return t},push(s,u){let r=h(s),a=t;window.location.hash=r,t=i(),c(t,a)},replace(s,u){let r=h(s),a=t;window.location.replace(window.location.pathname+window.location.search+"#"+r),t=i(),c(t,a)},go(s){window.history.go(s)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return i()},listen(s){return e.push(s),()=>{let u=e.indexOf(s);u!==-1&&e.splice(u,1)}},destroy(){window.removeEventListener("hashchange",n),e.length=0}}}function G(){let e=[],t=[],n=[];function i(u){return e.push(u),()=>{let r=e.indexOf(u);r!==-1&&e.splice(r,1)}}function c(u){return t.push(u),()=>{let r=t.indexOf(u);r!==-1&&t.splice(r,1)}}function s(u){return n.push(u),()=>{let r=n.indexOf(u);r!==-1&&n.splice(r,1)}}return{_beforeEachGuards:e,_beforeResolveGuards:t,_afterEachGuards:n,beforeEach:i,beforeResolve:c,afterEach:s}}function v(e,t,n){return new Promise((i,c)=>{let s=0;function u(){let a=!1;return o=>{a||(a=!0,o===!1?c(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):typeof o=="string"?c(new Error("REDIRECT:"+o)):(s++,r()))}}function r(){if(s>=e.length){i();return}let a=e[s],o=u();try{let d=a(t,n,o);d!=null&&typeof d=="object"&&typeof d.then=="function"&&d.then(()=>{}).catch(g=>{!g||typeof g.message!="string"||!g.message.startsWith("REDIRECT:")?c(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):c(g)})}catch(d){c(new Error("\u5BFC\u822A\u5B88\u536B\u6267\u884C\u51FA\u9519"))}}r()})}function E(e,t,n){for(let i of e)try{i(t,n)}catch(c){}}import{ref as k,reactive as S}from"@lytjs/reactivity";function U(e,t){return{path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash}}function T(e,t){return S({path:e.path,fullPath:e.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:e.query,hash:e.hash,matched:t?[t.record]:[]})}function z(e){let t=b(e.routes),n=e.mode==="hash"?P():L(e.base||"/"),i=G(),c=k(T(n.location,t.matchRoute(n.location.path))),s=!1,u=n.listen(async(o,d)=>{await r(o.path,d,!0)});async function r(o,d,g=!1){var f,p;if(!s){s=!0;try{let H=t.matchRoute(o),C={path:o,fullPath:o,query:{},hash:"",state:null,fromPopState:g},y=U(C,H),R=c.value,m={path:R.path,fullPath:R.fullPath,params:R.params,name:R.name,meta:R.meta,query:R.query,hash:R.hash};try{await v(i._beforeEachGuards,y,m)}catch(l){if((f=l==null?void 0:l.message)!=null&&f.startsWith("REDIRECT:")){let w=l.message.replace("REDIRECT:","");s=!1,g||n.replace(w);return}return}try{await v(i._beforeResolveGuards,y,m)}catch(l){if((p=l==null?void 0:l.message)!=null&&p.startsWith("REDIRECT:")){let w=l.message.replace("REDIRECT:","");s=!1,g||n.replace(w);return}return}let q=T(C,H);c.value=q,E(i._afterEachGuards,y,m)}finally{s=!1}}}let a={currentRoute:c,async push(o){n.push(o)},async replace(o){n.replace(o)},go(o){n.go(o)},back(){n.back()},forward(){n.forward()},beforeEach(o){return i.beforeEach(o)},afterEach(o){return i.afterEach(o)},beforeResolve(o){return i.beforeResolve(o)},addRoute(o){t.addRoute(o)},removeRoute(o){t.removeRoute(o)},getRoutes(){return t.getRoutes()},install(o){o.config&&o.config.globalProperties&&(o.config.globalProperties.$router=a,Object.defineProperty(o.config.globalProperties,"$route",{get(){return c.value}})),o.provide&&(o.provide("router",a),o.provide("route",c));let d=t.matchRoute(n.location.path);c.value=T(n.location,d)},destroy(){u(),n.destroy()}};return a}export{P as createHashHistory,G as createNavigationGuards,b as createRouteMatcher,z as createRouter,L as createWebHistory,E as runAfterGuards,v as runGuards};
1
+ import{normalizePath as x,pathToRegex as I}from"@lytjs/common";function N(o,t=""){let a=x(t,o.path),{regex:i,paramKeys:c,isWildcard:s}=I(a);if(o._regex=i,o._paramKeys=c,o._isWildcard=s,o.children)for(let u of o.children)N(u,a)}function b(o){let t=[];function a(r,n=""){for(let e of r)if(N(e,n),t.push(e),e.children){let d=x(n,e.path);a(e.children,d)}}a(o);function i(r){let n=r.replace(/\/+$/,"")||"/";for(let e of t){if(!e._regex)continue;let d=n.match(e._regex);if(d){let f={};if(e._paramKeys)for(let g=0;g<e._paramKeys.length;g++){let p=e._paramKeys[g];f[p]=d[g+1]||""}return{record:e,params:f,matchedPath:n}}}return null}function c(r){if(N(r),t.push(r),r.children){let n=x("",r.path);a(r.children,n)}}function s(r){for(let n=t.length-1;n>=0;n--)t[n].name===r&&t.splice(n,1)}function u(){return[...t]}return{matchRoute:i,addRoute:c,removeRoute:s,getRoutes:u}}function O(o){let t={};if(!o||o==="?")return t;let i=(o.startsWith("?")?o.slice(1):o).split("&");for(let c of i){let s=c.indexOf("=");if(s===-1)t[decodeURIComponent(c)]="";else{let u=decodeURIComponent(c.slice(0,s)),r=decodeURIComponent(c.slice(s+1));t[u]=r}}return t}function _(o){let t=[];for(let a of Object.keys(o)){let i=o[a];i!=null&&t.push(encodeURIComponent(a)+"="+encodeURIComponent(i))}return t.join("&")}function M(o){let t="",a=o,i=o.indexOf("#");i!==-1&&(t=o.slice(i+1),a=o.slice(0,i));let c=a,s={},u=a.indexOf("?");return u!==-1&&(c=a.slice(0,u),s=O(a.slice(u))),{path:c,query:s,hash:t}}function h(o){return o.startsWith("/")||(o="/"+o),o.replace(/\/+/g,"/")}function L(o="/"){let t=h(o),a=[],i=s(window.location.href);function c(r){let n=s(window.location.href);n.fromPopState=!0,n.state=r.state;let e=i;i=n,u(n,e)}function s(r){let n;try{n=new URL(r)}catch(p){n=new URL(r,window.location.origin)}let e=n.pathname;t!=="/"&&e.startsWith(t)&&(e=e.slice(t.length)||"/");let{path:d,query:f,hash:g}=M(e+n.hash);return{path:h(d),fullPath:h(d)+(Object.keys(f).length?"?"+_(f):"")+(g?"#"+g:""),query:f,hash:g,state:n.state||null,fromPopState:!1}}function u(r,n){for(let e of a)e(r,n)}return window.addEventListener("popstate",c),{base:t,get location(){return i},push(r,n){let e=h(r),d=i,f=(t+e).replace(/\/+/g,"/");window.history.pushState(n||null,"",f),i=s(window.location.href),i.state=n||null,u(i,d)},replace(r,n){let e=h(r),d=i,f=(t+e).replace(/\/+/g,"/");window.history.replaceState(n||null,"",f),i=s(window.location.href),i.state=n||null,u(i,d)},go(r){window.history.go(r)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return s(window.location.href)},listen(r){return a.push(r),()=>{let n=a.indexOf(r);n!==-1&&a.splice(n,1)}},destroy(){window.removeEventListener("popstate",c),a.length=0}}}function G(){let o=[],t=i();function a(){let s=i();s.fromPopState=!0;let u=t;t=s,c(s,u)}function i(){let u=window.location.hash.slice(1)||"/",{path:r,query:n,hash:e}=M(u);return{path:h(r),fullPath:h(r)+(Object.keys(n).length?"?"+_(n):"")+(e?"#"+e:""),query:n,hash:e,state:null,fromPopState:!1}}function c(s,u){for(let r of o)r(s,u)}return window.addEventListener("hashchange",a),{base:"",get location(){return t},push(s,u){let r=h(s),n=t;window.location.hash=r,t=i(),c(t,n)},replace(s,u){let r=h(s),n=t;window.location.replace(window.location.pathname+window.location.search+"#"+r),t=i(),c(t,n)},go(s){window.history.go(s)},back(){window.history.back()},forward(){window.history.forward()},getCurrentRoute(){return i()},listen(s){return o.push(s),()=>{let u=o.indexOf(s);u!==-1&&o.splice(u,1)}},destroy(){window.removeEventListener("hashchange",a),o.length=0}}}function P(){let o=[],t=[],a=[];function i(u){return o.push(u),()=>{let r=o.indexOf(u);r!==-1&&o.splice(r,1)}}function c(u){return t.push(u),()=>{let r=t.indexOf(u);r!==-1&&t.splice(r,1)}}function s(u){return a.push(u),()=>{let r=a.indexOf(u);r!==-1&&a.splice(r,1)}}return{_beforeEachGuards:o,_beforeResolveGuards:t,_afterEachGuards:a,beforeEach:i,beforeResolve:c,afterEach:s}}function v(o,t,a){return new Promise((i,c)=>{let s=0;function u(){let n=!1;return e=>{n||(n=!0,e===!1?c(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):typeof e=="string"?c(new Error("REDIRECT:"+e)):(s++,r()))}}function r(){if(s>=o.length){i();return}let n=o[s],e=u();try{let d=n(t,a,e);d!=null&&typeof d=="object"&&typeof d.then=="function"&&d.then(()=>{}).catch(f=>{!f||typeof f.message!="string"||!f.message.startsWith("REDIRECT:")?c(new Error("\u5BFC\u822A\u88AB\u5B88\u536B\u4E2D\u6B62")):c(f)})}catch(d){c(new Error("\u5BFC\u822A\u5B88\u536B\u6267\u884C\u51FA\u9519"))}}r()})}function E(o,t,a){for(let i of o)try{i(t,a)}catch(c){}}import{ref as k,reactive as S}from"@lytjs/reactivity";function W(o,t){return{path:o.path,fullPath:o.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:o.query,hash:o.hash}}function T(o,t){return S({path:o.path,fullPath:o.fullPath,params:(t==null?void 0:t.params)||{},name:t==null?void 0:t.record.name,meta:(t==null?void 0:t.record.meta)||{},query:o.query,hash:o.hash,matched:t?[t.record]:[]})}function U(o){let t=b(o.routes),a=o.mode==="hash"?G():L(o.base||"/"),i=P(),c=k(T(a.location,t.matchRoute(a.location.path))),s=!1,u=a.listen(async(e,d)=>{await r(e.path,d,!0)});async function r(e,d,f=!1){var g,p;if(!s){s=!0;try{let H=t.matchRoute(e),C={path:e,fullPath:e,query:{},hash:"",state:null,fromPopState:f},y=W(C,H),R=c.value,m={path:R.path,fullPath:R.fullPath,params:R.params,name:R.name,meta:R.meta,query:R.query,hash:R.hash};try{await v(i._beforeEachGuards,y,m)}catch(l){if((g=l==null?void 0:l.message)!=null&&g.startsWith("REDIRECT:")){let w=l.message.replace("REDIRECT:","");s=!1,f||a.replace(w);return}return}try{await v(i._beforeResolveGuards,y,m)}catch(l){if((p=l==null?void 0:l.message)!=null&&p.startsWith("REDIRECT:")){let w=l.message.replace("REDIRECT:","");s=!1,f||a.replace(w);return}return}let q=T(C,H);c.value=q,E(i._afterEachGuards,y,m)}finally{s=!1}}}let n={currentRoute:c,async push(e){a.push(e)},async replace(e){a.replace(e)},go(e){a.go(e)},back(){a.back()},forward(){a.forward()},beforeEach(e){return i.beforeEach(e)},afterEach(e){return i.afterEach(e)},beforeResolve(e){return i.beforeResolve(e)},addRoute(e){t.addRoute(e)},removeRoute(e){t.removeRoute(e)},getRoutes(){return t.getRoutes()},install(e){e.config&&e.config.globalProperties&&(e.config.globalProperties.$router=n,Object.defineProperty(e.config.globalProperties,"$route",{get(){return c.value}})),e.provide&&(e.provide("router",n),e.provide("route",c));let d=t.matchRoute(a.location.path);c.value=T(a.location,d)},destroy(){u(),a.destroy()}};return n}export{G as createHashHistory,P as createNavigationGuards,b as createRouteMatcher,U as createRouter,L as createWebHistory,E as runAfterGuards,v as runGuards};
@@ -1 +1 @@
1
- {"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../../src/matcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAMH,WAAW;AACX,MAAM,WAAW,WAAW;IAC1B,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,mBAAmB;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,WAAW;IACX,SAAS,CAAC,EAAE,GAAG,CAAC;IAChB,YAAY;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU;IACV,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB,mBAAmB;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,eAAe;IACf,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,aAAa;AACb,MAAM,WAAW,gBAAgB;IAC/B,eAAe;IACf,MAAM,EAAE,WAAW,CAAC;IACpB,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,eAAe;IACf,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,cAAc;AACd,MAAM,WAAW,YAAY;IAC3B,wBAAwB;IACxB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAClD,aAAa;IACb,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IACnC,cAAc;IACd,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,eAAe;IACf,SAAS,IAAI,WAAW,EAAE,CAAC;CAC5B;AAoID;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,YAAY,CAwHtE"}
1
+ {"version":3,"file":"matcher.d.ts","sourceRoot":"","sources":["../../src/matcher.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAaH,WAAW;AACX,MAAM,WAAW,WAAW;IAC1B,kCAAkC;IAClC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,2BAA2B;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,mBAAmB;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAC3B,WAAW;IACX,SAAS,CAAC,EAAE,GAAG,CAAC;IAChB,YAAY;IACZ,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU;IACV,QAAQ,CAAC,EAAE,WAAW,EAAE,CAAC;IACzB,mBAAmB;IACnB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,kBAAkB;IAClB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,eAAe;IACf,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB;AAED,aAAa;AACb,MAAM,WAAW,gBAAgB;IAC/B,eAAe;IACf,MAAM,EAAE,WAAW,CAAC;IACpB,8BAA8B;IAC9B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC/B,eAAe;IACf,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,cAAc;AACd,MAAM,WAAW,YAAY;IAC3B,wBAAwB;IACxB,UAAU,CAAC,IAAI,EAAE,MAAM,GAAG,gBAAgB,GAAG,IAAI,CAAC;IAClD,aAAa;IACb,QAAQ,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAC;IACnC,cAAc;IACd,WAAW,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,CAAC;IAChC,eAAe;IACf,SAAS,IAAI,WAAW,EAAE,CAAC;CAC5B;AAsCD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,YAAY,CAwHtE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lytjs/router",
3
- "version": "4.0.1",
3
+ "version": "4.0.5",
4
4
  "description": "Lyt.js 内置路由 - History/Hash 模式、导航守卫、动态路由",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
@@ -14,7 +14,9 @@
14
14
  }
15
15
  },
16
16
  "sideEffects": false,
17
- "files": ["dist"],
17
+ "files": [
18
+ "dist"
19
+ ],
18
20
  "license": "MIT",
19
21
  "author": "lytjs",
20
22
  "repository": {
@@ -23,11 +25,21 @@
23
25
  },
24
26
  "homepage": "https://gitee.com/lytjs/lytjs",
25
27
  "keywords": [
26
- "lyt", "lytjs", "javascript", "framework", "frontend", "vue-like",
27
- "lightweight", "zero-dependency", "router", "history", "hash"
28
+ "lyt",
29
+ "lytjs",
30
+ "javascript",
31
+ "framework",
32
+ "frontend",
33
+ "vue-like",
34
+ "lightweight",
35
+ "zero-dependency",
36
+ "router",
37
+ "history",
38
+ "hash"
28
39
  ],
29
40
  "dependencies": {
30
- "@lytjs/reactivity": "^4.0.0"
41
+ "@lytjs/common": "^4.0.5",
42
+ "@lytjs/reactivity": "^4.0.5"
31
43
  },
32
44
  "engines": {
33
45
  "node": ">=18.0.0"