@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 +1 -1
- package/dist/index.mjs +1 -1
- package/dist/types/matcher.d.ts.map +1 -1
- package/package.json +17 -5
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
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
|
-
|
|
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;
|
|
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.
|
|
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": [
|
|
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",
|
|
27
|
-
"
|
|
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/
|
|
41
|
+
"@lytjs/common": "^4.0.5",
|
|
42
|
+
"@lytjs/reactivity": "^4.0.5"
|
|
31
43
|
},
|
|
32
44
|
"engines": {
|
|
33
45
|
"node": ">=18.0.0"
|