@i18n-micro/route-strategy 1.1.5 → 1.1.7
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.d.cts +5 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.mjs +332 -322
- package/dist/locale-route-access.d.ts +6 -0
- package/dist/utils/index.d.cts +3 -0
- package/package.json +20 -12
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("node:path"),S=require("node:fs"),k=require("@i18n-micro/core"),b=l=>l.map(e=>({...e})),K=l=>!!(l.redirect&&!l.file),F=(l,e,t,a="localized-")=>t?`${a}${l}-${e}`:`${a}${l}`,C=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,""),M=(l,e,t)=>t&&l!==e.code,T=(l,e)=>(typeof l=="string"?l:l.code)===e.code,H=[/^\/sitemap.*\.xml$/,/^\/sitemap\.xml$/,/^\/robots\.txt$/,/^\/favicon\.ico$/,/^\/apple-touch-icon.*\.png$/,/^\/manifest\.json$/,/^\/sw\.js$/,/^\/workbox-.*\.js$/,/\.(xml|txt|ico|json|js|css|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/];function E(l,e){if(/(?:^|\/)__[^/]+/.test(l))return!0;for(const t of H)if(t.test(l))return!0;if(e){for(const t of e)if(typeof t=="string"){if(t.includes("*")||t.includes("?")){if(new RegExp(t.replace(/\*/g,".*").replace(/\?/g,".")).test(l))return!0}else if(l===t||l.startsWith(t))return!0}else if(t instanceof RegExp&&t.test(l))return!0}return!1}function J(l,e){const t=l.reduce((o,s)=>{const i=o.find(r=>r.code===s.code);return i?Object.assign(i,s):o.push(s),o},[]).filter(o=>!o.disabled),a=t.find(o=>o.code===e)??{code:e};return{locales:t,defaultLocale:a}}function x(l){return l.split("/").map(e=>e.startsWith("[...")&&e.endsWith("]")?`:${e.substring(4,e.length-1)}(.*)*`:e.startsWith("[")&&e.endsWith("]")?`:${e.substring(1,e.length-1)}`:e).join("/")}const R=l=>{if(!l)return"";const e=w.posix.normalize(l).replace(/\/+$/,"");return e==="."?"":e},g=l=>l.startsWith("/")?l.slice(1):l;function O(...l){return w.posix.join(...l)}function Q(l){if(!(typeof l>"u"))return l.startsWith("/")&&l.endsWith("/")?l?.slice(1,-1):l}function V(l){return!l||!/[\u0080-\uFFFF]/.test(l)?l:l.split("/").map(e=>!e||e.startsWith(":")||!/[\u0080-\uFFFF]/.test(e)?e:encodeURI(e)).join("/")}function N(l,e,t){const a=Q(t?.toString()),o=a||(Array.isArray(l)?l.join("|"):l),s=V(e);return R(w.posix.join("/",`:locale(${o})`,s))}function I(l){const e=V(l);return R(e)}const X=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,"");function j(l){return g(l)||"/"}function _(l,e,t,a=""){const o={};return l.forEach(s=>{const i=X(s.name,s.path),r=R(O(a,s.path??"")),n=j(r),d=x(r),c=e[n]||e[d]||e[i];if(c)typeof c=="object"&&(o[n]=c);else{const h=t[i];h&&typeof h=="object"&&!Array.isArray(h)&&(o[n]=h)}if(s.children?.length){const h=R(O(a,s.path??""));Object.assign(o,_(s.children,e,t,h))}}),o}function Y(l){const e={};if(!l)return e;for(const t in l){const a=x(t),o=l[t];if(typeof o=="object"){const s={};for(const i in o){const r=o[i];r&&(s[i]=x(r))}e[a]=s}else e[a]=o}return e}class Z{constructor(e){this.locales=e.locales,this.defaultLocale=this.findLocaleByCode(e.locales,e.defaultLocaleCode)??{code:e.defaultLocaleCode},this.strategy=e.strategy,this.globalLocaleRoutes=Y(e.globalLocaleRoutes??{}),this.filesLocaleRoutes=e.filesLocaleRoutes??{},this.routeLocales=e.routeLocales??{},this.localizedPaths=_(e.pages,this.globalLocaleRoutes,this.filesLocaleRoutes),this.activeLocaleCodes=this.locales.filter(t=>t.code!==this.defaultLocale.code||k.isPrefixAndDefaultStrategy(this.strategy)||k.isPrefixStrategy(this.strategy)).map(t=>t.code),this.excludePatterns=e.excludePatterns,this.customRegex=e.customRegex,this.noPrefixRedirect=e.noPrefixRedirect??!1,this.localizedRouteNamePrefix=e.localizedRouteNamePrefix??"localized-",this.fallbackRedirectComponentPath=e.fallbackRedirectComponentPath,this.rawGlobalLocaleRoutes=e.rawGlobalLocaleRoutes}findLocaleByCode(e,t){return e.find(a=>a.code===t)}getAllowedLocales(e,t){const a=this.routeLocales[e]??this.routeLocales[t];return a?.length?a.filter(o=>this.locales.some(s=>s.code===o)):this.locales.map(o=>o.code)}getCustomPath(e,t){const a=j(e);return this.localizedPaths[a]?.[t]}getCustomPathsForPage(e,t){const a=j(e);return this.localizedPaths[a]??this.localizedPaths[t]}hasLocaleRestrictions(e,t){return!!(this.routeLocales[e]??this.routeLocales[t])}filterLocaleCodesWithoutCustomPaths(e){const t=j(e);return this.activeLocaleCodes.filter(a=>!this.localizedPaths[t]?.[a])}}function z(l,e){const t=(e??"").trim();return t.startsWith("/")?R(t):R(w.posix.join(l||"",t))}function p(l,e){const{path:t,name:a,children:o,meta:s,alias:i}=e,r={...l,path:t};return a!==void 0&&(r.name=a),o!==void 0&&(r.children=o),s!==void 0&&(r.meta={...l.meta,...s}),i!==void 0?(r.alias=i,r.meta={...r.meta,alias:i}):"alias"in e&&(r.alias=void 0,r.meta={...r.meta,alias:void 0}),r}function ee(l){const e=l.alias,t=l.meta?.alias,a=e??t;return Array.isArray(a)?a:[]}function D(l,e,t,a="localized-"){const o=ee(l);if(!o.length)return[];const s=[];for(const i of o){const r=N(e,i,t);s.push(p(l,{path:r,name:`${a}${l.name??""}`,alias:void 0,meta:{alias:void 0},children:l.children}))}return s}class W{processPage(e,t){if(e.path===void 0)throw new Error("page.path is required");if(K(e))return[e];if(e.path&&E(e.path,t.excludePatterns))return[e];const a=e.path??"",o=C(e.name,e.path),s=x(a);return t.globalLocaleRoutes[o]===!1||t.globalLocaleRoutes[s]===!1?[e]:this.generateVariants(e,t)}postProcess(e,t){return e}localizeChildren(e,t,a,o,s,i){return e.flatMap(r=>this.localizeChild(r,t,a,o,s,i))}localizeChildrenAllLocales(e,t,a,o,s){return e.flatMap(i=>this.localizeChildAllLocales(i,t,a,o,s))}localizeChildAllLocales(e,t,a,o,s){const i=z(a,e.path??""),r=z(t,e.path??""),n=j(r),d=s.localizedPaths[n];if(d)return o.flatMap(f=>{const P=d[f]??z(t,e.path??"");return this.localizeChild(e,P,a,f,s,!0)});const c=R(e.path??""),h=g(c),u=this.localizeChildrenAllLocales(b(e.children??[]),r,i,o,s),m=`${s.localizedRouteNamePrefix}${e.name??""}`;return[p(e,{path:h,name:m,children:u})]}localizeChild(e,t,a,o,s,i){const r=z(a,e.path??""),n=s.getCustomPath(r,o),d=g(R(n||(e.path??""))),c=n??z(t,e.path??""),h=this.localizeChildren(b(e.children??[]),c,r,o,s,i),u=C(e.name,e.path),m=`${s.localizedRouteNamePrefix}${u}-${o}`;return[p(e,{path:d,name:m,children:h})]}buildRoutePathForLocales(e,t,a,o,s,i,r){if(o){const n=e.includes(r);return i||!n?N(e,a,s):R(a)}return N(e,t,s)}}class te extends W{generateVariants(e,t){const a=e.path??"",o=C(e.name,e.path),s=t.getAllowedLocales(a,o),i=t.getCustomPathsForPage(a,o),r=b(e.children??[]),n=[];if(n.push(e),i)for(const d of s){const c=i[d];if(!c)continue;const h=R(c),u=R(a);if(h===u)continue;const m=I(c),f=F(o,d,!0),P=this.localizeChildrenForNoPrefix(r,h,a,a,d,t,1);n.push(p(e,{path:m,name:f,children:P,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&d===t.defaultLocale.code&&h!==u&&(e.redirect=h)}return n.push(...D(e,s,t.customRegex,t.localizedRouteNamePrefix)),n}localizeChildrenForNoPrefix(e,t,a,o,s,i,r){return e.map(n=>{const d=z(a,n.path??"");let c,h;if(r===1){const P=i.getCustomPath(d,s);if(P){const L=R(P);c=g(L),h=L}else{const L=R(n.path??"");c=g(L),h=z(t,n.path??"")}}else{const P=R(n.path??"");c=g(P),h=z(t,n.path??"")}const u=this.localizeChildrenForNoPrefix(b(n.children??[]),h,d,o,s,i,r+1),m=C(n.name,n.path),f=F(m,s,!0);return p(n,{path:c,name:f,children:u})})}}function ae(l){const e=l.alias??l.meta?.alias;return Array.isArray(e)?e:[]}class se extends W{generateVariants(e,t){const a=e.path??"",o=C(e.name,e.path),s=t.getAllowedLocales(a,o),i=t.getCustomPathsForPage(a,o),r=b(e.children??[]),n=[];if(i)for(const c of s){const h=i[c],u=h?R(h):a,m=N(c,u,t.customRegex),f=F(o,c,!!h,t.localizedRouteNamePrefix),P=h??a,L=this.localizeChildren(r,P,a,c,t,!0);n.push(p(e,{path:m,name:f,children:L,alias:[],meta:{alias:[]}}))}else{const c=N(s,a,t.customRegex),h=F(o,s[0],!1,t.localizedRouteNamePrefix),u=this.localizeChildrenAllLocales(r,a,a,s,t);n.push(p(e,{path:c,name:h,children:u,alias:[],meta:{alias:[]}}))}const d=ae(e);if(d.length)for(const c of d){const h=N(s,c,t.customRegex);n.push(p(e,{path:h,name:`${t.localizedRouteNamePrefix}${e.name??""}`,alias:void 0,meta:{alias:void 0}}))}return n}postProcess(e,t){const a=e.filter(s=>{if(s.name==="index"&&s.path==="/")return!1;const i=s.path??"";return!!(i&&E(i,t.excludePatterns)||t.globalLocaleRoutes[s.name??""]===!1||/^\/:locale/.test(i)||i==="/")}),o=t.defaultLocale.code;for(const[s,i]of Object.entries(t.globalLocaleRoutes)){if(i===!1||typeof i!="object")continue;const r=i[o];if(!r)continue;const n=s.startsWith("/")?s:`/${s}`,d=`/${o}${r.startsWith("/")?r:`/${r}`}`;n!==d&&a.push({path:n,name:`redirect-${s.replace(/\//g,"-")}`,redirect:d})}return t.fallbackRedirectComponentPath&&a.push({path:"/:pathMatch(.*)*",name:"custom-fallback-route",file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),a}}class oe extends W{generateVariants(e,t){const a=e.path??"",o=C(e.name,e.path),s=t.getAllowedLocales(a,o),i=t.getCustomPathsForPage(a,o),r=b(e.children??[]),n=[],d=t.defaultLocale.code;n.push(e);const c=t.locales.filter(u=>s.includes(u.code));if(c.length>0)if(i)for(const u of c){const m=i[u.code];if(m)if(u.code===d){const f=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!1,u.code,a,t);f&&n.push(f);const P=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!0,u.code,a,t);P&&n.push(P)}else{const f=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!1,u.code,a,t);f&&n.push(f)}else{const f=this.createLocalizedRoute(e,[u.code],r,!1,"",t.customRegex,!1,u.code,a,t);f&&n.push(f)}}else{const u=c.map(f=>f.code),m=this.createLocalizedRoute(e,u,r,!1,"",t.customRegex,!1,!0,a,t);m&&n.push(m)}const h=D(e,s,t.customRegex,t.localizedRouteNamePrefix);return h.length&&n.push(...h),n}postProcess(e,t){const a=[],o=[];for(const s of e){const i=s.name??"";typeof i=="string"&&i.startsWith(t.localizedRouteNamePrefix)?o.push(s):a.push(s)}return[...a,...o]}createLocalizedRoute(e,t,a,o,s="",i,r=!1,n=!1,d,c){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(s),o,i,r,c.defaultLocale.code);if(!h||!(o&&s)&&h===e.path||t.length===0)return null;const m=t[0];if(!m)return null;const f=d??e.path??"",P=F(C(e.name??"",f),m,o,c.localizedRouteNamePrefix),L=r||m!==c.defaultLocale.code,y=t.length===1?this.localizeChildren(a,h,f,m,c,L):this.localizeChildrenAllLocales(a,h,f,t,c);return p(e,{path:h,name:P,children:y,alias:[],meta:{alias:[]}})}}class le extends W{generateVariants(e,t){const a=e.path??"",o=C(e.name,e.path),s=t.getAllowedLocales(a,o),i=t.getCustomPathsForPage(a,o),r=b(e.children??[]),n=[],d=t.defaultLocale.code;if(s.includes(d)){const u=i?.[d],m=u?R(u):a,f=u?{[d]:u}:{},P=this.createLocalizedChildren(r,a,[d],!1,!1,!1,f,t);n.push(p(e,{path:m,name:e.name,children:P}))}const c=t.locales.filter(u=>s.includes(u.code)&&u.code!==d);if(c.length)if(i)for(const u of c){const m=i[u.code];if(m){const f=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!1,u.code,a,t);f&&n.push(f)}else{const f=this.createLocalizedRoute(e,[u.code],r,!1,"",t.customRegex,!1,u.code,a,t);f&&n.push(f)}}else{const u=c.map(f=>f.code),m=this.createLocalizedRoute(e,u,r,!1,"",t.customRegex,!1,!0,a,t);m&&n.push(m)}const h=D(e,s,t.customRegex,t.localizedRouteNamePrefix);return h.length&&n.push(...h),n}postProcess(e,t){const a=[],o=[];for(const i of e){const r=i.name??"";typeof r=="string"&&r.startsWith(t.localizedRouteNamePrefix)?o.push(i):a.push(i)}const s=[...a,...o];return t.fallbackRedirectComponentPath&&s.push({path:"/:pathMatch(.*)*",name:"custom-fallback-route",file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),s}createLocalizedChildren(e,t,a,o=!0,s=!1,i=!1,r={},n){return e.flatMap(d=>this.createLocalizedVariants(d,t,a,o,s,i,r,n))}createLocalizedVariants(e,t,a,o,s,i=!1,r,n){const d=R(e.path??""),c=z(t,e.path??""),h=j(c),u=n.localizedPaths[h],m=!!u,f=[];if(!m){const P=g(d),L=this.createLocalizedChildren(b(e.children??[]),z(t,e.path??""),a,o,s,i,r,n),y=this.buildChildRouteName(e.name,i,n);return f.push(p(e,{path:P,name:y,children:L})),f}for(const P of a){const L=r?.[P],y=!!L,$=u?.[P];let A=R($||(e.path??""));y&&L&&($?A=R($):A=z(L,e.path??""));const v=M(P,n.defaultLocale,s)?N(P,A,n.customRegex):A,q=g(v),G=$?R($):y?L:z(t,e.path??""),B=this.createLocalizedChildren(b(e.children??[]),G,[P],o,s,P,{...r,[P]:G},n),U=this.buildLocalizedRouteName(C(e.name,e.path),P,o,!!u,n);f.push(p(e,{path:q,name:U,children:B}))}return f}buildChildRouteName(e,t,a){return t===!0?`${a.localizedRouteNamePrefix}${e}`:typeof t=="string"?`${a.localizedRouteNamePrefix}${e}-${t}`:e}createLocalizedRoute(e,t,a,o,s="",i,r=!1,n=!1,d,c){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(s),o,i,r,c.defaultLocale.code);if(!h||h===e.path||t.length===0)return null;const u=t[0];if(!u)return null;const m=d??e.path??"",f=F(C(e.name??"",m),u,o,c.localizedRouteNamePrefix);return p(e,{path:h,name:f,children:this.createLocalizedChildren(a,m,t,!0,!1,n,{[u]:s},c),alias:[],meta:{alias:[]}})}buildLocalizedRouteName(e,t,a,o=!1,s){return a?o?`${s.localizedRouteNamePrefix}${e}-${t}`:t&&!T(t,s.defaultLocale)?`${s.localizedRouteNamePrefix}${e}-${t}`:`${s.localizedRouteNamePrefix}${e}`:e}}function ie(l){switch(l){case"no_prefix":return new te;case"prefix":return new se;case"prefix_and_default":return new oe;case"prefix_except_default":return new le;default:throw new Error(`Unknown route strategy: ${l}`)}}class re{constructor(e){this.localizedPaths={};const{locales:t,defaultLocaleCode:a,strategy:o,globalLocaleRoutes:s,filesLocaleRoutes:i={},routeLocales:r={},noPrefixRedirect:n,excludePatterns:d,localizedRouteNamePrefix:c="localized-",customRegexMatcher:h,fallbackRedirectComponentPath:u}=e,m=J(t,a);this.locales=m.locales,this.defaultLocale=m.defaultLocale,this.strategy=o,this.noPrefixRedirect=n,this.excludePatterns=d,this.localizedRouteNamePrefix=c,this.customRegex=h,this.fallbackRedirectComponentPath=u,this.activeLocaleCodes=this.computeActiveLocaleCodes();const f={};for(const P in s){const L=x(P),y=s[P];if(typeof y=="object"){const $={};for(const A in y){const v=y[A];v&&($[A]=x(v))}f[L]=$}else f[L]=y}this.globalLocaleRoutes=f,this.rawGlobalLocaleRoutes=s??{},this.filesLocaleRoutes=i??{},this.routeLocales=r??{}}computeActiveLocaleCodes(){return this.locales.filter(e=>e.code!==this.defaultLocale.code||k.isPrefixAndDefaultStrategy(this.strategy)||k.isPrefixStrategy(this.strategy)).map(e=>e.code)}extendPages(e){const t=new Z({locales:this.locales,defaultLocaleCode:this.defaultLocale.code,strategy:this.strategy,globalLocaleRoutes:this.globalLocaleRoutes,filesLocaleRoutes:this.filesLocaleRoutes,routeLocales:this.routeLocales,pages:e,excludePatterns:this.excludePatterns,customRegex:this.customRegex,noPrefixRedirect:this.noPrefixRedirect,localizedRouteNamePrefix:this.localizedRouteNamePrefix,fallbackRedirectComponentPath:this.fallbackRedirectComponentPath,rawGlobalLocaleRoutes:this.rawGlobalLocaleRoutes});this.localizedPaths=t.localizedPaths;const a=ie(this.strategy),o=[...e],s=[];for(const r of o)s.push(...a.processPage(r,t));const i=a.postProcess(s,t);e.length=0,e.push(...i)}extractLocalizedPaths(e,t=""){return _(e,this.globalLocaleRoutes,this.filesLocaleRoutes,t)}ensureTranslationFilesExist(e,t,a,o){this.locales.forEach(s=>{const i=w.join(a,t,`${s.code}.json`);this.ensureFileExists(i),o||e.forEach(r=>{const n=w.join(a,t,"pages",`${r}/${s.code}.json`);this.ensureFileExists(n)})})}ensureFileExists(e){try{const t=w.dirname(e);S.existsSync(t)||S.mkdirSync(t,{recursive:!0}),S.existsSync(e)||S.writeFileSync(e,JSON.stringify({}),"utf-8")}catch{}}resolveLocalizedPath(e,t){const a=x(e),o=g(a)||"/";let s;const i=this.globalLocaleRoutes[o],r=this.globalLocaleRoutes[a];i&&typeof i=="object"&&!Array.isArray(i)?s=i[t]:r&&typeof r=="object"&&!Array.isArray(r)&&(s=r[t]);const n=!!s,d=s?R(s):R(a)||"";let c=!1;if(this.strategy==="no_prefix"?c=!1:this.strategy==="prefix"||this.strategy==="prefix_and_default"?c=!0:this.strategy==="prefix_except_default"&&(c=t!==this.defaultLocale.code),t===this.defaultLocale.code&&!n&&(this.strategy==="prefix_except_default"||this.strategy==="no_prefix")&&(c=!1),!c)return R(d)||"/";const h=g(d);return h?R(`/${t}/${h}`):`/${t}`}generateDataRoutes(e,t,a){const o=[],s=this.collectPageNames(e);for(const i of this.locales)if(o.push(`/${t}/index/${i.code}/data.json`),!a)for(const r of s)o.push(`/${t}/${r}/${i.code}/data.json`);return o}collectPageNames(e){const t=new Set,a=o=>{for(const s of o)typeof s.name=="string"&&s.name.length>0&&t.add(s.name),s.children&&s.children.length>0&&a(s.children)};return a(e),[...t]}}exports.RouteGenerator=re;exports.buildFullPath=N;exports.buildFullPathNoPrefix=I;exports.buildRouteName=F;exports.cloneArray=b;exports.extractLocalizedPaths=_;exports.isInternalPath=E;exports.isLocaleDefault=T;exports.isPageRedirectOnly=K;exports.normalizePath=R;exports.normalizeRouteKey=x;exports.removeLeadingSlash=g;exports.shouldAddLocalePrefix=M;
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("node:path"),S=require("node:fs"),k=require("@i18n-micro/core"),b=l=>l.map(e=>({...e})),K=l=>!!(l.redirect&&!l.file),F=(l,e,t,s="localized-")=>t?`${s}${l}-${e}`:`${s}${l}`,C=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,""),M=(l,e,t)=>t&&l!==e.code,T=(l,e)=>(typeof l=="string"?l:l.code)===e.code,H=[/^\/sitemap.*\.xml$/,/^\/sitemap\.xml$/,/^\/robots\.txt$/,/^\/favicon\.ico$/,/^\/apple-touch-icon.*\.png$/,/^\/manifest\.json$/,/^\/sw\.js$/,/^\/workbox-.*\.js$/,/\.(xml|txt|ico|json|js|css|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/];function E(l,e){if(/(?:^|\/)__[^/]+/.test(l))return!0;for(const t of H)if(t.test(l))return!0;if(e){for(const t of e)if(typeof t=="string"){if(t.includes("*")||t.includes("?")){if(new RegExp(t.replace(/\*/g,".*").replace(/\?/g,".")).test(l))return!0}else if(l===t||l.startsWith(t))return!0}else if(t instanceof RegExp&&t.test(l))return!0}return!1}function J(l,e){const t=l.reduce((o,a)=>{const r=o.find(i=>i.code===a.code);return r?Object.assign(r,a):o.push(a),o},[]).filter(o=>!o.disabled),s=t.find(o=>o.code===e)??{code:e};return{locales:t,defaultLocale:s}}function x(l){return l.split("/").map(e=>e.startsWith("[...")&&e.endsWith("]")?`:${e.substring(4,e.length-1)}(.*)*`:e.startsWith("[")&&e.endsWith("]")?`:${e.substring(1,e.length-1)}`:e).join("/")}const R=l=>{if(!l)return"";const e=w.posix.normalize(l).replace(/\/+$/,"");return e==="."?"":e},z=l=>l.startsWith("/")?l.slice(1):l;function G(...l){return w.posix.join(...l)}function Q(l){if(!(typeof l>"u"))return l.startsWith("/")&&l.endsWith("/")?l?.slice(1,-1):l}function V(l){return!l||!/[\u0080-\uFFFF]/.test(l)?l:l.split("/").map(e=>!e||e.startsWith(":")||!/[\u0080-\uFFFF]/.test(e)?e:encodeURI(e)).join("/")}function A(l,e,t){const s=Q(t?.toString()),o=s||(Array.isArray(l)?l.join("|"):l),a=V(e);return R(w.posix.join("/",`:locale(${o})`,a))}function I(l){const e=V(l);return R(e)}const X=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,"");function j(l){return z(l)||"/"}function _(l,e,t,s=""){const o={};return l.forEach(a=>{const r=X(a.name,a.path),i=R(G(s,a.path??"")),n=j(i),f=x(i),c=e[n]||e[f]||e[r];if(c)typeof c=="object"&&(o[n]=c);else{const h=t[r];h&&typeof h=="object"&&!Array.isArray(h)&&(o[n]=h)}if(a.children?.length){const h=R(G(s,a.path??""));Object.assign(o,_(a.children,e,t,h))}}),o}function Y(l,e,t,s){if(!l||Object.keys(l).length===0)return!0;const o=x(t),a=z(o)||"/",r=a==="/"?"/":a,i=l[o]??l[t]??l[a]??l[r]??(a!=="/"?l[`/${a}`]:void 0);if(!Array.isArray(i)||i.length===0)return!0;const n=e.map(c=>c.code),f=i.filter(c=>n.includes(c));return f.length===0?!0:f.includes(s)}function Z(l){const e={};if(!l)return e;for(const t in l){const s=x(t),o=l[t];if(typeof o=="object"){const a={};for(const r in o){const i=o[r];i&&(a[r]=x(i))}e[s]=a}else e[s]=o}return e}class ee{constructor(e){this.locales=e.locales,this.defaultLocale=this.findLocaleByCode(e.locales,e.defaultLocaleCode)??{code:e.defaultLocaleCode},this.strategy=e.strategy,this.globalLocaleRoutes=Z(e.globalLocaleRoutes??{}),this.filesLocaleRoutes=e.filesLocaleRoutes??{},this.routeLocales=e.routeLocales??{},this.localizedPaths=_(e.pages,this.globalLocaleRoutes,this.filesLocaleRoutes),this.activeLocaleCodes=this.locales.filter(t=>t.code!==this.defaultLocale.code||k.isPrefixAndDefaultStrategy(this.strategy)||k.isPrefixStrategy(this.strategy)).map(t=>t.code),this.excludePatterns=e.excludePatterns,this.customRegex=e.customRegex,this.noPrefixRedirect=e.noPrefixRedirect??!1,this.localizedRouteNamePrefix=e.localizedRouteNamePrefix??"localized-",this.fallbackRedirectComponentPath=e.fallbackRedirectComponentPath,this.rawGlobalLocaleRoutes=e.rawGlobalLocaleRoutes}findLocaleByCode(e,t){return e.find(s=>s.code===t)}getAllowedLocales(e,t){const s=this.routeLocales[e]??this.routeLocales[t];return s?.length?s.filter(o=>this.locales.some(a=>a.code===o)):this.locales.map(o=>o.code)}getCustomPath(e,t){const s=j(e);return this.localizedPaths[s]?.[t]}getCustomPathsForPage(e,t){const s=j(e);return this.localizedPaths[s]??this.localizedPaths[t]}hasLocaleRestrictions(e,t){return!!(this.routeLocales[e]??this.routeLocales[t])}filterLocaleCodesWithoutCustomPaths(e){const t=j(e);return this.activeLocaleCodes.filter(s=>!this.localizedPaths[t]?.[s])}}function g(l,e){const t=(e??"").trim();return t.startsWith("/")?R(t):R(w.posix.join(l||"",t))}function p(l,e){const{path:t,name:s,children:o,meta:a,alias:r}=e,i={...l,path:t};return s!==void 0&&(i.name=s),o!==void 0&&(i.children=o),a!==void 0&&(i.meta={...l.meta,...a}),r!==void 0?(i.alias=r,i.meta={...i.meta,alias:r}):"alias"in e&&(i.alias=void 0,i.meta={...i.meta,alias:void 0}),i}function te(l){const e=l.alias,t=l.meta?.alias,s=e??t;return Array.isArray(s)?s:[]}function O(l,e,t,s="localized-"){const o=te(l);if(!o.length)return[];const a=[];for(const r of o){const i=A(e,r,t);a.push(p(l,{path:i,name:`${s}${l.name??""}`,alias:void 0,meta:{alias:void 0},children:l.children}))}return a}class W{processPage(e,t){if(e.path===void 0)throw new Error("page.path is required");if(K(e))return[e];if(e.path&&E(e.path,t.excludePatterns))return[e];const s=e.path??"",o=C(e.name,e.path),a=x(s);return t.globalLocaleRoutes[o]===!1||t.globalLocaleRoutes[a]===!1?[e]:this.generateVariants(e,t)}postProcess(e,t){return e}localizeChildren(e,t,s,o,a,r){return e.flatMap(i=>this.localizeChild(i,t,s,o,a,r))}localizeChildrenAllLocales(e,t,s,o,a){return e.flatMap(r=>this.localizeChildAllLocales(r,t,s,o,a))}localizeChildAllLocales(e,t,s,o,a){const r=g(s,e.path??""),i=g(t,e.path??""),n=j(i),f=a.localizedPaths[n];if(f)return o.flatMap(d=>{const P=f[d]??g(t,e.path??"");return this.localizeChild(e,P,s,d,a,!0)});const c=R(e.path??""),h=z(c),u=this.localizeChildrenAllLocales(b(e.children??[]),i,r,o,a),m=`${a.localizedRouteNamePrefix}${e.name??""}`;return[p(e,{path:h,name:m,children:u})]}localizeChild(e,t,s,o,a,r){const i=g(s,e.path??""),n=a.getCustomPath(i,o),f=z(R(n||(e.path??""))),c=n??g(t,e.path??""),h=this.localizeChildren(b(e.children??[]),c,i,o,a,r),u=C(e.name,e.path),m=`${a.localizedRouteNamePrefix}${u}-${o}`;return[p(e,{path:f,name:m,children:h})]}buildRoutePathForLocales(e,t,s,o,a,r,i){if(o){const n=e.includes(i);return r||!n?A(e,s,a):R(s)}return A(e,t,a)}}class ae extends W{generateVariants(e,t){const s=e.path??"",o=C(e.name,e.path),a=t.getAllowedLocales(s,o),r=t.getCustomPathsForPage(s,o),i=b(e.children??[]),n=[];if(n.push(e),r)for(const f of a){const c=r[f];if(!c)continue;const h=R(c),u=R(s);if(h===u)continue;const m=I(c),d=F(o,f,!0),P=this.localizeChildrenForNoPrefix(i,h,s,s,f,t,1);n.push(p(e,{path:m,name:d,children:P,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&f===t.defaultLocale.code&&h!==u&&(e.redirect=h)}return n.push(...O(e,a,t.customRegex,t.localizedRouteNamePrefix)),n}localizeChildrenForNoPrefix(e,t,s,o,a,r,i){return e.map(n=>{const f=g(s,n.path??"");let c,h;if(i===1){const P=r.getCustomPath(f,a);if(P){const L=R(P);c=z(L),h=L}else{const L=R(n.path??"");c=z(L),h=g(t,n.path??"")}}else{const P=R(n.path??"");c=z(P),h=g(t,n.path??"")}const u=this.localizeChildrenForNoPrefix(b(n.children??[]),h,f,o,a,r,i+1),m=C(n.name,n.path),d=F(m,a,!0);return p(n,{path:c,name:d,children:u})})}}function se(l){const e=l.alias??l.meta?.alias;return Array.isArray(e)?e:[]}class oe extends W{generateVariants(e,t){const s=e.path??"",o=C(e.name,e.path),a=t.getAllowedLocales(s,o),r=t.getCustomPathsForPage(s,o),i=b(e.children??[]),n=[];if(r)for(const c of a){const h=r[c],u=h?R(h):s,m=A(c,u,t.customRegex),d=F(o,c,!!h,t.localizedRouteNamePrefix),P=h??s,L=this.localizeChildren(i,P,s,c,t,!0);n.push(p(e,{path:m,name:d,children:L,alias:[],meta:{alias:[]}}))}else{const c=A(a,s,t.customRegex),h=F(o,a[0],!1,t.localizedRouteNamePrefix),u=this.localizeChildrenAllLocales(i,s,s,a,t);n.push(p(e,{path:c,name:h,children:u,alias:[],meta:{alias:[]}}))}const f=se(e);if(f.length)for(const c of f){const h=A(a,c,t.customRegex);n.push(p(e,{path:h,name:`${t.localizedRouteNamePrefix}${e.name??""}`,alias:void 0,meta:{alias:void 0}}))}return n}postProcess(e,t){const s=e.filter(a=>{if(a.name==="index"&&a.path==="/")return!1;const r=a.path??"";return!!(r&&E(r,t.excludePatterns)||t.globalLocaleRoutes[a.name??""]===!1||/^\/:locale/.test(r)||r==="/")}),o=t.defaultLocale.code;for(const[a,r]of Object.entries(t.globalLocaleRoutes)){if(r===!1||typeof r!="object")continue;const i=r[o];if(!i)continue;const n=a.startsWith("/")?a:`/${a}`,f=`/${o}${i.startsWith("/")?i:`/${i}`}`;n!==f&&s.push({path:n,name:`redirect-${a.replace(/\//g,"-")}`,redirect:f})}return t.fallbackRedirectComponentPath&&s.push({path:"/:pathMatch(.*)*",name:"custom-fallback-route",file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),s}}class le extends W{generateVariants(e,t){const s=e.path??"",o=C(e.name,e.path),a=t.getAllowedLocales(s,o),r=t.getCustomPathsForPage(s,o),i=b(e.children??[]),n=[],f=t.defaultLocale.code;n.push(e);const c=t.locales.filter(u=>a.includes(u.code));if(c.length>0)if(r)for(const u of c){const m=r[u.code];if(m)if(u.code===f){const d=this.createLocalizedRoute(e,[u.code],i,!0,m,t.customRegex,!1,u.code,s,t);d&&n.push(d);const P=this.createLocalizedRoute(e,[u.code],i,!0,m,t.customRegex,!0,u.code,s,t);P&&n.push(P)}else{const d=this.createLocalizedRoute(e,[u.code],i,!0,m,t.customRegex,!1,u.code,s,t);d&&n.push(d)}else{const d=this.createLocalizedRoute(e,[u.code],i,!1,"",t.customRegex,!1,u.code,s,t);d&&n.push(d)}}else{const u=c.map(d=>d.code),m=this.createLocalizedRoute(e,u,i,!1,"",t.customRegex,!1,!0,s,t);m&&n.push(m)}const h=O(e,a,t.customRegex,t.localizedRouteNamePrefix);return h.length&&n.push(...h),n}postProcess(e,t){const s=[],o=[];for(const a of e){const r=a.name??"";typeof r=="string"&&r.startsWith(t.localizedRouteNamePrefix)?o.push(a):s.push(a)}return[...s,...o]}createLocalizedRoute(e,t,s,o,a="",r,i=!1,n=!1,f,c){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(a),o,r,i,c.defaultLocale.code);if(!h||!(o&&a)&&h===e.path||t.length===0)return null;const m=t[0];if(!m)return null;const d=f??e.path??"",P=F(C(e.name??"",d),m,o,c.localizedRouteNamePrefix),L=i||m!==c.defaultLocale.code,y=t.length===1?this.localizeChildren(s,h,d,m,c,L):this.localizeChildrenAllLocales(s,h,d,t,c);return p(e,{path:h,name:P,children:y,alias:[],meta:{alias:[]}})}}class ie extends W{generateVariants(e,t){const s=e.path??"",o=C(e.name,e.path),a=t.getAllowedLocales(s,o),r=t.getCustomPathsForPage(s,o),i=b(e.children??[]),n=[],f=t.defaultLocale.code;if(a.includes(f)){const u=r?.[f],m=u?R(u):s,d=u?{[f]:u}:{},P=this.createLocalizedChildren(i,s,[f],!1,!1,!1,d,t);n.push(p(e,{path:m,name:e.name,children:P}))}const c=t.locales.filter(u=>a.includes(u.code)&&u.code!==f);if(c.length)if(r)for(const u of c){const m=r[u.code];if(m){const d=this.createLocalizedRoute(e,[u.code],i,!0,m,t.customRegex,!1,u.code,s,t);d&&n.push(d)}else{const d=this.createLocalizedRoute(e,[u.code],i,!1,"",t.customRegex,!1,u.code,s,t);d&&n.push(d)}}else{const u=c.map(d=>d.code),m=this.createLocalizedRoute(e,u,i,!1,"",t.customRegex,!1,!0,s,t);m&&n.push(m)}const h=O(e,a,t.customRegex,t.localizedRouteNamePrefix);return h.length&&n.push(...h),n}postProcess(e,t){const s=[],o=[];for(const r of e){const i=r.name??"";typeof i=="string"&&i.startsWith(t.localizedRouteNamePrefix)?o.push(r):s.push(r)}const a=[...s,...o];return t.fallbackRedirectComponentPath&&a.push({path:"/:pathMatch(.*)*",name:"custom-fallback-route",file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),a}createLocalizedChildren(e,t,s,o=!0,a=!1,r=!1,i={},n){return e.flatMap(f=>this.createLocalizedVariants(f,t,s,o,a,r,i,n))}createLocalizedVariants(e,t,s,o,a,r=!1,i,n){const f=R(e.path??""),c=g(t,e.path??""),h=j(c),u=n.localizedPaths[h],m=!!u,d=[];if(!m){const P=z(f),L=this.createLocalizedChildren(b(e.children??[]),g(t,e.path??""),s,o,a,r,i,n),y=this.buildChildRouteName(e.name,r,n);return d.push(p(e,{path:P,name:y,children:L})),d}for(const P of s){const L=i?.[P],y=!!L,$=u?.[P];let N=R($||(e.path??""));y&&L&&($?N=R($):N=g(L,e.path??""));const v=M(P,n.defaultLocale,a)?A(P,N,n.customRegex):N,U=z(v),D=$?R($):y?L:g(t,e.path??""),q=this.createLocalizedChildren(b(e.children??[]),D,[P],o,a,P,{...i,[P]:D},n),B=this.buildLocalizedRouteName(C(e.name,e.path),P,o,!!u,n);d.push(p(e,{path:U,name:B,children:q}))}return d}buildChildRouteName(e,t,s){return t===!0?`${s.localizedRouteNamePrefix}${e}`:typeof t=="string"?`${s.localizedRouteNamePrefix}${e}-${t}`:e}createLocalizedRoute(e,t,s,o,a="",r,i=!1,n=!1,f,c){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(a),o,r,i,c.defaultLocale.code);if(!h||h===e.path||t.length===0)return null;const u=t[0];if(!u)return null;const m=f??e.path??"",d=F(C(e.name??"",m),u,o,c.localizedRouteNamePrefix);return p(e,{path:h,name:d,children:this.createLocalizedChildren(s,m,t,!0,!1,n,{[u]:a},c),alias:[],meta:{alias:[]}})}buildLocalizedRouteName(e,t,s,o=!1,a){return s?o?`${a.localizedRouteNamePrefix}${e}-${t}`:t&&!T(t,a.defaultLocale)?`${a.localizedRouteNamePrefix}${e}-${t}`:`${a.localizedRouteNamePrefix}${e}`:e}}function re(l){switch(l){case"no_prefix":return new ae;case"prefix":return new oe;case"prefix_and_default":return new le;case"prefix_except_default":return new ie;default:throw new Error(`Unknown route strategy: ${l}`)}}class ne{constructor(e){this.localizedPaths={};const{locales:t,defaultLocaleCode:s,strategy:o,globalLocaleRoutes:a,filesLocaleRoutes:r={},routeLocales:i={},noPrefixRedirect:n,excludePatterns:f,localizedRouteNamePrefix:c="localized-",customRegexMatcher:h,fallbackRedirectComponentPath:u}=e,m=J(t,s);this.locales=m.locales,this.defaultLocale=m.defaultLocale,this.strategy=o,this.noPrefixRedirect=n,this.excludePatterns=f,this.localizedRouteNamePrefix=c,this.customRegex=h,this.fallbackRedirectComponentPath=u,this.activeLocaleCodes=this.computeActiveLocaleCodes();const d={};for(const P in a){const L=x(P),y=a[P];if(typeof y=="object"){const $={};for(const N in y){const v=y[N];v&&($[N]=x(v))}d[L]=$}else d[L]=y}this.globalLocaleRoutes=d,this.rawGlobalLocaleRoutes=a??{},this.filesLocaleRoutes=r??{},this.routeLocales=i??{}}computeActiveLocaleCodes(){return this.locales.filter(e=>e.code!==this.defaultLocale.code||k.isPrefixAndDefaultStrategy(this.strategy)||k.isPrefixStrategy(this.strategy)).map(e=>e.code)}extendPages(e){const t=new ee({locales:this.locales,defaultLocaleCode:this.defaultLocale.code,strategy:this.strategy,globalLocaleRoutes:this.globalLocaleRoutes,filesLocaleRoutes:this.filesLocaleRoutes,routeLocales:this.routeLocales,pages:e,excludePatterns:this.excludePatterns,customRegex:this.customRegex,noPrefixRedirect:this.noPrefixRedirect,localizedRouteNamePrefix:this.localizedRouteNamePrefix,fallbackRedirectComponentPath:this.fallbackRedirectComponentPath,rawGlobalLocaleRoutes:this.rawGlobalLocaleRoutes});this.localizedPaths=t.localizedPaths;const s=re(this.strategy),o=[...e],a=[];for(const i of o)a.push(...s.processPage(i,t));const r=s.postProcess(a,t);e.length=0,e.push(...r)}extractLocalizedPaths(e,t=""){return _(e,this.globalLocaleRoutes,this.filesLocaleRoutes,t)}ensureTranslationFilesExist(e,t,s,o){this.locales.forEach(a=>{const r=w.join(s,t,`${a.code}.json`);this.ensureFileExists(r),o||e.forEach(i=>{const n=w.join(s,t,"pages",`${i}/${a.code}.json`);this.ensureFileExists(n)})})}ensureFileExists(e){try{const t=w.dirname(e);S.existsSync(t)||S.mkdirSync(t,{recursive:!0}),S.existsSync(e)||S.writeFileSync(e,JSON.stringify({}),"utf-8")}catch{}}resolveLocalizedPath(e,t){const s=x(e),o=z(s)||"/";let a;const r=this.globalLocaleRoutes[o],i=this.globalLocaleRoutes[s];r&&typeof r=="object"&&!Array.isArray(r)?a=r[t]:i&&typeof i=="object"&&!Array.isArray(i)&&(a=i[t]);const n=!!a,f=a?R(a):R(s)||"";let c=!1;if(this.strategy==="no_prefix"?c=!1:this.strategy==="prefix"||this.strategy==="prefix_and_default"?c=!0:this.strategy==="prefix_except_default"&&(c=t!==this.defaultLocale.code),t===this.defaultLocale.code&&!n&&(this.strategy==="prefix_except_default"||this.strategy==="no_prefix")&&(c=!1),!c)return R(f)||"/";const h=z(f);return h?R(`/${t}/${h}`):`/${t}`}generateDataRoutes(e,t,s){const o=[],a=this.collectPageNames(e);for(const r of this.locales)if(o.push(`/${t}/index/${r.code}/data.json`),!s)for(const i of a)o.push(`/${t}/${i}/${r.code}/data.json`);return o}collectPageNames(e){const t=new Set,s=o=>{for(const a of o)typeof a.name=="string"&&a.name.length>0&&t.add(a.name),a.children&&a.children.length>0&&s(a.children)};return s(e),[...t]}}exports.RouteGenerator=ne;exports.buildFullPath=A;exports.buildFullPathNoPrefix=I;exports.buildRouteName=F;exports.cloneArray=b;exports.extractLocalizedPaths=_;exports.isInternalPath=E;exports.isLocaleAllowedForUnlocalizedRoute=Y;exports.isLocaleDefault=T;exports.isPageRedirectOnly=K;exports.normalizePath=R;exports.normalizeRouteKey=x;exports.removeLeadingSlash=z;exports.shouldAddLocalePrefix=M;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { extractLocalizedPaths, LocalizedPathsMap } from './core/localized-paths';
|
|
2
|
+
import { isLocaleAllowedForUnlocalizedRoute } from './locale-route-access';
|
|
3
|
+
import { RouteGenerator, RouteGeneratorOptions } from './route-generator';
|
|
4
|
+
import { buildFullPath, buildFullPathNoPrefix, buildRouteName, cloneArray, isInternalPath, isLocaleDefault, isPageRedirectOnly, normalizePath, normalizeRouteKey, removeLeadingSlash, shouldAddLocalePrefix } from './utils';
|
|
5
|
+
export { RouteGenerator, type RouteGeneratorOptions, isLocaleAllowedForUnlocalizedRoute, extractLocalizedPaths, type LocalizedPathsMap, isInternalPath, normalizeRouteKey, normalizePath, cloneArray, isPageRedirectOnly, removeLeadingSlash, buildRouteName, shouldAddLocalePrefix, isLocaleDefault, buildFullPath, buildFullPathNoPrefix, };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { extractLocalizedPaths, LocalizedPathsMap } from './core/localized-paths';
|
|
2
|
+
import { isLocaleAllowedForUnlocalizedRoute } from './locale-route-access';
|
|
2
3
|
import { RouteGenerator, RouteGeneratorOptions } from './route-generator';
|
|
3
4
|
import { buildFullPath, buildFullPathNoPrefix, buildRouteName, cloneArray, isInternalPath, isLocaleDefault, isPageRedirectOnly, normalizePath, normalizeRouteKey, removeLeadingSlash, shouldAddLocalePrefix } from './utils';
|
|
4
|
-
export { RouteGenerator, type RouteGeneratorOptions, extractLocalizedPaths, type LocalizedPathsMap, isInternalPath, normalizeRouteKey, normalizePath, cloneArray, isPageRedirectOnly, removeLeadingSlash, buildRouteName, shouldAddLocalePrefix, isLocaleDefault, buildFullPath, buildFullPathNoPrefix, };
|
|
5
|
+
export { RouteGenerator, type RouteGeneratorOptions, isLocaleAllowedForUnlocalizedRoute, extractLocalizedPaths, type LocalizedPathsMap, isInternalPath, normalizeRouteKey, normalizePath, cloneArray, isPageRedirectOnly, removeLeadingSlash, buildRouteName, shouldAddLocalePrefix, isLocaleDefault, buildFullPath, buildFullPathNoPrefix, };
|