@i18n-micro/route-strategy 1.1.18 → 1.1.23
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 +552 -850
- package/dist/utils/common.d.ts +1 -1
- package/package.json +9 -8
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("node:path"),k=require("node:fs"),_=require("@i18n-micro/core"),C=l=>l.map(e=>({...e})),M=l=>!!(l.redirect&&!l.file),j=(l,e,t,a="localized-")=>t?`${a}${l}-${e}`:`${a}${l}`,b=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,""),T=(l,e,t)=>t&&l!==e.code,V=(l,e)=>(typeof l=="string"?l:l.code)===e.code,J=[/^\/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 K(l,e){if(/(?:^|\/)__[^/]+/.test(l))return!0;for(const t of J)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 Q(l,e){const t=l.reduce((o,s)=>{const r=o.find(i=>i.code===s.code);return r?Object.assign(r,s):o.push(s),o},[]).filter(o=>!o.disabled),a=t.find(o=>o.code===e)??{code:e};return{locales:t,defaultLocale:a}}function A(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.replace(/:([^()/]+)\(\)/g,":$1")).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 X(l){if(!(typeof l>"u"))return l.startsWith("/")&&l.endsWith("/")?l?.slice(1,-1):l}function Y(l){return!l||!/[\u0080-\uFFFF]/.test(l)?l:l.split("/").map(e=>!e||e.startsWith(":")||!/[\u0080-\uFFFF]/.test(e)?e:encodeURI(e)).join("/")}function q(l){const e=Y(l);return e!==l?[e]:[]}function N(l,e,t){const a=X(t?.toString()),o=a||(Array.isArray(l)?l.join("|"):l),s=e;return R(w.posix.join("/",`:locale(${o})`,s))}function B(l){const e=l;return R(e)}const Z=(l,e)=>l??(e??"").replace(/[^a-z0-9]/gi,"-").replace(/^-+|-+$/g,"");function F(l){return z(l)||"/"}function W(l,e,t,a=""){const o={};return l.forEach(s=>{const r=Z(s.name,s.path),i=R(G(a,s.path??"")),n=F(i),f=A(i),u=z(f)||"/",h=e[n]||e[f]||e[u]||e[r];if(h)typeof h=="object"&&(o[n]=h);else{const c=t[r];c&&typeof c=="object"&&!Array.isArray(c)&&(o[n]=c)}if(s.children?.length){const c=R(G(a,s.path??""));Object.assign(o,W(s.children,e,t,c))}}),o}function v(l,e,t){const a=A(e),o=F(e);return l[t]===!1||l[a]===!1||l[o]===!1||l[z(a)]===!1}function ee(l,e,t,a){if(!l||Object.keys(l).length===0)return!0;const o=A(t),s=z(o)||"/",r=s==="/"?"/":s,i=l[o]??l[t]??l[s]??l[r]??(s!=="/"?l[`/${s}`]:void 0);if(!Array.isArray(i)||i.length===0)return!0;const n=e.map(u=>u.code),f=i.filter(u=>n.includes(u));return f.length===0?!0:f.includes(a)}function te(l){const e={};if(!l)return e;for(const t in l){const a=A(t),o=l[t];if(typeof o=="object"){const s={};for(const r in o){const i=o[r];i&&(s[r]=A(i))}e[a]=s}else e[a]=o}return e}class ae{constructor(e){this.locales=e.locales,this.defaultLocale=this.findLocaleByCode(e.locales,e.defaultLocaleCode)??{code:e.defaultLocaleCode},this.strategy=e.strategy,this.globalLocaleRoutes=te(e.globalLocaleRoutes??{}),this.filesLocaleRoutes=e.filesLocaleRoutes??{},this.routeLocales=e.routeLocales??{},this.localizedPaths=W(e.pages,this.globalLocaleRoutes,this.filesLocaleRoutes),this.activeLocaleCodes=this.locales.filter(t=>t.code!==this.defaultLocale.code||_.isPrefixAndDefaultStrategy(this.strategy)||_.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=F(e);return this.localizedPaths[a]?.[t]}getCustomPathsForPage(e,t){const a=F(e);return this.localizedPaths[a]??this.localizedPaths[t]}hasLocaleRestrictions(e,t){return!!(this.routeLocales[e]??this.routeLocales[t])}filterLocaleCodesWithoutCustomPaths(e){const t=F(e);return this.activeLocaleCodes.filter(a=>!this.localizedPaths[t]?.[a])}}function y(l,e){const t=(e??"").trim();return t.startsWith("/")?R(t):R(w.posix.join(l||"",t))}function g(l,e){const{path:t,name:a,children:o,meta:s,alias:r}=e,i={...l,path:t};return a!==void 0&&(i.name=a),o!==void 0&&(i.children=o),s!==void 0&&(i.meta={...l.meta,...s}),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 se(l){const e=l.alias,t=l.meta?.alias,a=e??t;return Array.isArray(a)?a:[]}function O(l,e,t,a="localized-"){const o=se(l);if(!o.length)return[];const s=[];for(const r of o){const i=N(e,r,t);s.push(g(l,{path:i,name:`${a}${l.name??""}`,alias:void 0,meta:{alias:void 0},children:l.children}))}return s}class E{processPage(e,t){if(e.path===void 0)throw new Error("page.path is required");if(M(e))return[e];if(e.path&&K(e.path,t.excludePatterns))return[e];const a=e.path??"",o=b(e.name,e.path);return v(t.globalLocaleRoutes,a,o)?[e]:this.generateVariants(e,t)}postProcess(e,t){return e}localizeChildren(e,t,a,o,s,r){return e.flatMap(i=>this.localizeChild(i,t,a,o,s,r))}localizeChildrenAllLocales(e,t,a,o,s){return e.flatMap(r=>this.localizeChildAllLocales(r,t,a,o,s))}localizeChildAllLocales(e,t,a,o,s){const r=y(a,e.path??""),i=b(e.name,e.path);if(v(s.globalLocaleRoutes,r,i))return[e];const n=y(t,e.path??""),f=F(n),u=s.localizedPaths[f];if(u)return o.flatMap(P=>{const L=u[P]??y(t,e.path??"");return this.localizeChild(e,L,a,P,s,!0)});const h=R(e.path??""),c=z(h),m=this.localizeChildrenAllLocales(C(e.children??[]),n,r,o,s),d=`${s.localizedRouteNamePrefix}${e.name??""}`;return[g(e,{path:c,name:d,children:m})]}localizeChild(e,t,a,o,s,r){const i=y(a,e.path??""),n=b(e.name,e.path);if(v(s.globalLocaleRoutes,i,n))return[e];const f=s.getCustomPath(i,o),u=z(R(f||(e.path??""))),h=f??y(t,e.path??""),c=this.localizeChildren(C(e.children??[]),h,i,o,s,r),m=b(e.name,e.path),d=`${s.localizedRouteNamePrefix}${m}-${o}`;return[g(e,{path:u,name:d,children:c})]}buildRoutePathForLocales(e,t,a,o,s,r,i){if(o){const n=e.includes(i);return r||!n?N(e,a,s):R(a)}return N(e,t,s)}}class oe extends E{generateVariants(e,t){const a=e.path??"",o=b(e.name,e.path),s=t.getAllowedLocales(a,o),r=t.getCustomPathsForPage(a,o),i=C(e.children??[]),n=[];if(n.push(e),r)for(const f of s){const u=r[f];if(!u)continue;const h=R(u),c=R(a);if(h===c)continue;const m=B(u),d=j(o,f,!0),P=this.localizeChildrenForNoPrefix(i,h,a,a,f,t,1);n.push(g(e,{path:m,name:d,children:P,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&f===t.defaultLocale.code&&h!==c&&(e.redirect=h)}return n.push(...O(e,s,t.customRegex,t.localizedRouteNamePrefix)),n}localizeChildrenForNoPrefix(e,t,a,o,s,r,i){return e.map(n=>{const f=y(a,n.path??"");let u,h;if(i===1){const P=r.getCustomPath(f,s);if(P){const L=R(P);u=z(L),h=L}else{const L=R(n.path??"");u=z(L),h=y(t,n.path??"")}}else{const P=R(n.path??"");u=z(P),h=y(t,n.path??"")}const c=this.localizeChildrenForNoPrefix(C(n.children??[]),h,f,o,s,r,i+1),m=b(n.name,n.path),d=j(m,s,!0);return g(n,{path:u,name:d,children:c})})}}function le(l){const e=l.alias??l.meta?.alias;return Array.isArray(e)?e:[]}class ie extends E{generateVariants(e,t){const a=e.path??"",o=b(e.name,e.path),s=t.getAllowedLocales(a,o),r=t.getCustomPathsForPage(a,o),i=C(e.children??[]),n=[];if(r)for(const u of s){const h=r[u],c=h?R(h):a,m=N(u,c,t.customRegex),d=j(o,u,!!h,t.localizedRouteNamePrefix),P=h??a,L=this.localizeChildren(i,P,a,u,t,!0),p=q(m);n.push(g(e,{path:m,name:d,children:L,alias:p.length?p:void 0,meta:{alias:p.length?p:[]}}))}else{const u=N(s,a,t.customRegex),h=j(o,s[0],!1,t.localizedRouteNamePrefix),c=this.localizeChildrenAllLocales(i,a,a,s,t);n.push(g(e,{path:u,name:h,children:c,alias:[],meta:{alias:[]}}))}const f=le(e);if(f.length)for(const u of f){const h=N(s,u,t.customRegex);n.push(g(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 r=s.path??"";return!!(r&&K(r,t.excludePatterns)||v(t.globalLocaleRoutes,r,s.name??"")||/^\/:locale/.test(r)||r==="/")}),o=t.defaultLocale.code;for(const[s,r]of Object.entries(t.globalLocaleRoutes)){if(r===!1||typeof r!="object")continue;const i=r[o];if(!i)continue;const n=s.startsWith("/")?s:`/${s}`,f=`/${o}${i.startsWith("/")?i:`/${i}`}`;n!==f&&a.push({path:n,name:`redirect-${s.replace(/\//g,"-")}`,redirect:f})}return t.fallbackRedirectComponentPath&&a.push({path:"/:pathMatch(.*)*",name:"custom-fallback-route",file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),a}}class re extends E{generateVariants(e,t){const a=e.path??"",o=b(e.name,e.path),s=t.getAllowedLocales(a,o),r=t.getCustomPathsForPage(a,o),i=C(e.children??[]),n=[],f=t.defaultLocale.code;n.push(e);const u=t.locales.filter(c=>s.includes(c.code));if(u.length>0)if(r)for(const c of u){const m=r[c.code];if(m)if(c.code===f){const d=this.createLocalizedRoute(e,[c.code],i,!0,m,t.customRegex,!1,c.code,a,t);d&&n.push(d);const P=this.createLocalizedRoute(e,[c.code],i,!0,m,t.customRegex,!0,c.code,a,t);P&&n.push(P)}else{const d=this.createLocalizedRoute(e,[c.code],i,!0,m,t.customRegex,!1,c.code,a,t);d&&n.push(d)}else{const d=this.createLocalizedRoute(e,[c.code],i,!1,"",t.customRegex,!1,c.code,a,t);d&&n.push(d)}}else{const c=u.map(d=>d.code),m=this.createLocalizedRoute(e,c,i,!1,"",t.customRegex,!1,!0,a,t);m&&n.push(m)}const h=O(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 r=s.name??"";typeof r=="string"&&r.startsWith(t.localizedRouteNamePrefix)?o.push(s):a.push(s)}return[...a,...o]}createLocalizedRoute(e,t,a,o,s="",r,i=!1,n=!1,f,u){const h=this.buildRoutePathForLocales(t,e.path??"",s,o,r,i,u.defaultLocale.code);if(!h||!(o&&s)&&h===e.path||t.length===0)return null;const m=t[0];if(!m)return null;const d=f??e.path??"",P=j(b(e.name??"",d),m,o,u.localizedRouteNamePrefix),L=i||m!==u.defaultLocale.code,p=t.length===1?this.localizeChildren(a,h,d,m,u,L):this.localizeChildrenAllLocales(a,h,d,t,u);return g(e,{path:h,name:P,children:p,alias:[],meta:{alias:[]}})}}class ne extends E{generateVariants(e,t){const a=e.path??"",o=b(e.name,e.path),s=t.getAllowedLocales(a,o),r=t.getCustomPathsForPage(a,o),i=C(e.children??[]),n=[],f=t.defaultLocale.code;if(s.includes(f)){const c=r?.[f],m=c?R(c):a,d=c?{[f]:c}:{},P=this.createLocalizedChildren(i,a,[f],!1,!1,!1,d,t);n.push(g(e,{path:m,name:e.name,children:P}))}const u=t.locales.filter(c=>s.includes(c.code)&&c.code!==f);if(u.length)if(r)for(const c of u){const m=r[c.code];if(m){const d=this.createLocalizedRoute(e,[c.code],i,!0,m,t.customRegex,!1,c.code,a,t);d&&n.push(d)}else{const d=this.createLocalizedRoute(e,[c.code],i,!1,"",t.customRegex,!1,c.code,a,t);d&&n.push(d)}}else{const c=u.map(d=>d.code),m=this.createLocalizedRoute(e,c,i,!1,"",t.customRegex,!1,!0,a,t);m&&n.push(m)}const h=O(e,s,t.customRegex,t.localizedRouteNamePrefix);return h.length&&n.push(...h),n}postProcess(e,t){const a=[],o=[];for(const r of e){const i=r.name??"";typeof i=="string"&&i.startsWith(t.localizedRouteNamePrefix)?o.push(r):a.push(r)}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,r=!1,i={},n){return e.flatMap(f=>this.createLocalizedVariants(f,t,a,o,s,r,i,n))}createLocalizedVariants(e,t,a,o,s,r=!1,i,n){const f=R(e.path??""),u=y(t,e.path??""),h=F(u),c=n.localizedPaths[h],m=!!c,d=[];if(!m){const P=z(f),L=this.createLocalizedChildren(C(e.children??[]),y(t,e.path??""),a,o,s,r,i,n),p=this.buildChildRouteName(e.name,r,n);return d.push(g(e,{path:P,name:p,children:L})),d}for(const P of a){const L=i?.[P],p=!!L,$=c?.[P];let x=R($||(e.path??""));p&&L&&($?x=R($):x=y(L,e.path??""));const S=T(P,n.defaultLocale,s)?N(P,x,n.customRegex):x,I=z(S),D=$?R($):p?L:y(t,e.path??""),U=this.createLocalizedChildren(C(e.children??[]),D,[P],o,s,P,{...i,[P]:D},n),H=this.buildLocalizedRouteName(b(e.name,e.path),P,o,!!c,n);d.push(g(e,{path:I,name:H,children:U}))}return d}buildChildRouteName(e,t,a){return t===!0?`${a.localizedRouteNamePrefix}${e}`:typeof t=="string"?`${a.localizedRouteNamePrefix}${e}-${t}`:e}createLocalizedRoute(e,t,a,o,s="",r,i=!1,n=!1,f,u){const h=this.buildRoutePathForLocales(t,e.path??"",s,o,r,i,u.defaultLocale.code);if(!h||h===e.path||t.length===0)return null;const c=t[0];if(!c)return null;const m=f??e.path??"",d=j(b(e.name??"",m),c,o,u.localizedRouteNamePrefix);return g(e,{path:h,name:d,children:this.createLocalizedChildren(a,m,t,!0,!1,n,{[c]:s},u),alias:[],meta:{alias:[]}})}buildLocalizedRouteName(e,t,a,o=!1,s){return a?o?`${s.localizedRouteNamePrefix}${e}-${t}`:t&&!V(t,s.defaultLocale)?`${s.localizedRouteNamePrefix}${e}-${t}`:`${s.localizedRouteNamePrefix}${e}`:e}}function ce(l){switch(l){case"no_prefix":return new oe;case"prefix":return new ie;case"prefix_and_default":return new re;case"prefix_except_default":return new ne;default:throw new Error(`Unknown route strategy: ${l}`)}}class ue{constructor(e){this.localizedPaths={};const{locales:t,defaultLocaleCode:a,strategy:o,globalLocaleRoutes:s,filesLocaleRoutes:r={},routeLocales:i={},noPrefixRedirect:n,excludePatterns:f,localizedRouteNamePrefix:u="localized-",customRegexMatcher:h,fallbackRedirectComponentPath:c}=e,m=Q(t,a);this.locales=m.locales,this.defaultLocale=m.defaultLocale,this.strategy=o,this.noPrefixRedirect=n,this.excludePatterns=f,this.localizedRouteNamePrefix=u,this.customRegex=h,this.fallbackRedirectComponentPath=c,this.activeLocaleCodes=this.computeActiveLocaleCodes();const d={};for(const P in s){const L=A(P),p=s[P];if(typeof p=="object"){const $={};for(const x in p){const S=p[x];S&&($[x]=A(S))}d[L]=$}else d[L]=p}this.globalLocaleRoutes=d,this.rawGlobalLocaleRoutes=s??{},this.filesLocaleRoutes=r??{},this.routeLocales=i??{}}computeActiveLocaleCodes(){return this.locales.filter(e=>e.code!==this.defaultLocale.code||_.isPrefixAndDefaultStrategy(this.strategy)||_.isPrefixStrategy(this.strategy)).map(e=>e.code)}extendPages(e){const t=new ae({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=ce(this.strategy),o=[...e],s=[];for(const i of o)s.push(...a.processPage(i,t));const r=a.postProcess(s,t);e.length=0,e.push(...r)}extractLocalizedPaths(e,t=""){return W(e,this.globalLocaleRoutes,this.filesLocaleRoutes,t)}ensureTranslationFilesExist(e,t,a,o){this.locales.forEach(s=>{const r=w.join(a,t,`${s.code}.json`);this.ensureFileExists(r),o||e.forEach(i=>{const n=w.join(a,t,"pages",`${i}/${s.code}.json`);this.ensureFileExists(n)})})}ensureFileExists(e){try{const t=w.dirname(e);k.existsSync(t)||k.mkdirSync(t,{recursive:!0}),k.existsSync(e)||k.writeFileSync(e,JSON.stringify({}),"utf-8")}catch{}}resolveLocalizedPath(e,t){const a=A(e),o=z(a)||"/";let s;const r=this.globalLocaleRoutes[o],i=this.globalLocaleRoutes[a];r&&typeof r=="object"&&!Array.isArray(r)?s=r[t]:i&&typeof i=="object"&&!Array.isArray(i)&&(s=i[t]);const n=!!s,f=s?R(s):R(a)||"";let u=!1;if(this.strategy==="no_prefix"?u=!1:this.strategy==="prefix"||this.strategy==="prefix_and_default"?u=!0:this.strategy==="prefix_except_default"&&(u=t!==this.defaultLocale.code),t===this.defaultLocale.code&&!n&&(this.strategy==="prefix_except_default"||this.strategy==="no_prefix")&&(u=!1),!u)return R(f)||"/";const h=z(f);return h?R(`/${t}/${h}`):`/${t}`}generateDataRoutes(e,t,a){const o=[],s=this.collectPageNames(e);for(const r of this.locales)if(o.push(`/${t}/index/${r.code}/data.json`),!a)for(const i of s)o.push(`/${t}/${i}/${r.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=ue;exports.buildEncodedPathAliases=q;exports.buildFullPath=N;exports.buildFullPathNoPrefix=B;exports.buildRouteName=j;exports.cloneArray=C;exports.extractLocalizedPaths=W;exports.isInternalPath=K;exports.isLocaleAllowedForUnlocalizedRoute=ee;exports.isLocaleDefault=V;exports.isLocalizationDisabledForPage=v;exports.isPageRedirectOnly=M;exports.normalizePath=R;exports.normalizeRouteKey=A;exports.removeLeadingSlash=z;exports.shouldAddLocalePrefix=T;
|
|
1
|
+
Object.defineProperty(exports,Symbol.toStringTag,{value:`Module`});var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,s)=>(s=n==null?{}:e(i(n)),o(r||!n||!n.__esModule||!a.call(n,`default`)?t(s,`default`,{value:n,enumerable:!0}):s,n));let c=require("@i18n-micro/utils/app-path"),l=require("node:path");l=s(l,1);let u=require("node:fs"),d=require("@i18n-micro/core");var f=e=>e.map(e=>({...e})),p=e=>!(!e.redirect||e.file),m=(e,t,n,r=`localized-`)=>n?`${r}${e}-${t}`:`${r}${e}`,h=(e,t)=>e??(t??``).replace(/[^a-z0-9]/gi,`-`).replace(/^-+|-+$/g,``),g=(e,t,n)=>n&&e!==t.code,_=(e,t)=>(typeof e==`string`?e:e.code)===t.code;function v(e,t){let n=e.reduce((e,t)=>{let n=e.find(e=>e.code===t.code);return n?Object.assign(n,t):e.push(t),e},[]).filter(e=>!e.disabled);return{locales:n,defaultLocale:n.find(e=>e.code===t)??{code:t}}}function y(e){return e.split(`/`).map(e=>e.startsWith(`[...`)&&e.endsWith(`]`)?`:${e.substring(4,e.length-1)}(.*)*`:e.startsWith(`[`)&&e.endsWith(`]`)?`:${e.substring(1,e.length-1)}`:e.replace(/:([^()/]+)\(\)/g,`:$1`)).join(`/`)}var b=e=>{if(!e)return``;let t=l.default.posix.normalize(e).replace(/\/+$/,``);return t===`.`?``:t},x=e=>e.startsWith(`/`)?e.slice(1):e;function S(...e){return l.default.posix.join(...e)}function C(e){if(e!==void 0)return e.startsWith(`/`)&&e.endsWith(`/`)?e?.slice(1,-1):e}function w(e){return e}function T(e){return!e||!/[\u0080-\uFFFF]/.test(e)?e:e.split(`/`).map(e=>!e||e.startsWith(`:`)||!/[\u0080-\uFFFF]/.test(e)?e:encodeURI(e)).join(`/`)}function E(e){let t=T(e);return t===e?[]:[t]}function D(e,t,n){let r=C(n?.toString())||(Array.isArray(e)?e.join(`|`):e),i=w(t);return b(l.default.posix.join(`/`,`:locale(${r})`,i))}function O(e){return b(w(e))}var k=(e,t)=>e??(t??``).replace(/[^a-z0-9]/gi,`-`).replace(/^-+|-+$/g,``);function A(e){return x(e)||`/`}function j(e,t,n,r=``){let i={};return e.forEach(e=>{let a=k(e.name,e.path),o=b(S(r,e.path??``)),s=A(o),c=y(o),l=x(c)||`/`,u=t[s]||t[c]||t[l]||t[a];if(u)typeof u==`object`&&(i[s]=u);else{let e=n[a];e&&typeof e==`object`&&!Array.isArray(e)&&(i[s]=e)}if(e.children?.length){let a=b(S(r,e.path??``));Object.assign(i,j(e.children,t,n,a))}}),i}function M(e,t,n){let r=y(t),i=A(t);return e[n]===!1||e[r]===!1||e[i]===!1||e[x(r)]===!1}function N(e,t,n,r){if(!e||Object.keys(e).length===0)return!0;let i=y(n),a=x(i)||`/`,o=a===`/`?`/`:a,s=e[i]??e[n]??e[a]??e[o]??(a===`/`?void 0:e[`/${a}`]);if(!Array.isArray(s)||s.length===0)return!0;let c=t.map(e=>e.code),l=s.filter(e=>c.includes(e));return l.length===0||l.includes(r)}function P(e){let t={};if(!e)return t;for(let n in e){let r=y(n),i=e[n];if(typeof i==`object`){let e={};for(let t in i){let n=i[t];n&&(e[t]=y(n))}t[r]=e}else t[r]=i}return t}var F=class{constructor(e){this.locales=e.locales,this.defaultLocale=this.findLocaleByCode(e.locales,e.defaultLocaleCode)??{code:e.defaultLocaleCode},this.strategy=e.strategy,this.globalLocaleRoutes=P(e.globalLocaleRoutes??{}),this.filesLocaleRoutes=e.filesLocaleRoutes??{},this.routeLocales=e.routeLocales??{},this.localizedPaths=j(e.pages,this.globalLocaleRoutes,this.filesLocaleRoutes),this.activeLocaleCodes=this.locales.filter(e=>e.code!==this.defaultLocale.code||(0,d.isPrefixAndDefaultStrategy)(this.strategy)||(0,d.isPrefixStrategy)(this.strategy)).map(e=>e.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(e=>e.code===t)}getAllowedLocales(e,t){let n=this.routeLocales[e]??this.routeLocales[t];return n?.length?n.filter(e=>this.locales.some(t=>t.code===e)):this.locales.map(e=>e.code)}getCustomPath(e,t){let n=A(e);return this.localizedPaths[n]?.[t]}getCustomPathsForPage(e,t){let n=A(e);return this.localizedPaths[n]??this.localizedPaths[t]}hasLocaleRestrictions(e,t){return!!(this.routeLocales[e]??this.routeLocales[t])}filterLocaleCodesWithoutCustomPaths(e){let t=A(e);return this.activeLocaleCodes.filter(e=>!this.localizedPaths[t]?.[e])}};function I(e,t){let n=(t??``).trim();return n.startsWith(`/`)?b(n):b(l.default.posix.join(e||``,n))}function L(e,t){let{path:n,name:r,children:i,meta:a,alias:o}=t,s={...e,path:n};return r!==void 0&&(s.name=r),i!==void 0&&(s.children=i),a!==void 0&&(s.meta={...e.meta,...a}),o===void 0?`alias`in t&&(s.alias=void 0,s.meta={...s.meta,alias:void 0}):(s.alias=o,s.meta={...s.meta,alias:o}),s}function R(e){let t=e.alias,n=e.meta?.alias,r=t??n;return Array.isArray(r)?r:[]}function z(e,t,n,r=`localized-`){let i=R(e);if(!i.length)return[];let a=[];for(let o of i){let i=D(t,o,n);a.push(L(e,{path:i,name:`${r}${e.name??``}`,alias:void 0,meta:{alias:void 0},children:e.children}))}return a}var B=class{processPage(e,t){if(e.path===void 0)throw Error(`page.path is required`);if(p(e)||e.path&&(0,c.isInternalPath)(e.path,t.excludePatterns))return[e];let n=e.path??``,r=h(e.name,e.path);return M(t.globalLocaleRoutes,n,r)?[e]:this.generateVariants(e,t)}postProcess(e,t){return e}localizeChildren(e,t,n,r,i,a){return e.flatMap(e=>this.localizeChild(e,t,n,r,i,a))}localizeChildrenAllLocales(e,t,n,r,i){return e.flatMap(e=>this.localizeChildAllLocales(e,t,n,r,i))}localizeChildAllLocales(e,t,n,r,i){let a=I(n,e.path??``),o=h(e.name,e.path);if(M(i.globalLocaleRoutes,a,o))return[e];let s=I(t,e.path??``),c=A(s),l=i.localizedPaths[c];if(l)return r.flatMap(r=>{let a=l[r]??I(t,e.path??``);return this.localizeChild(e,a,n,r,i,!0)});let u=x(b(e.path??``)),d=this.localizeChildrenAllLocales(f(e.children??[]),s,a,r,i);return[L(e,{path:u,name:`${i.localizedRouteNamePrefix}${e.name??``}`,children:d})]}localizeChild(e,t,n,r,i,a){let o=I(n,e.path??``),s=h(e.name,e.path);if(M(i.globalLocaleRoutes,o,s))return[e];let c=i.getCustomPath(o,r),l=x(b(c||(e.path??``))),u=c??I(t,e.path??``),d=this.localizeChildren(f(e.children??[]),u,o,r,i,a),p=h(e.name,e.path);return[L(e,{path:l,name:`${i.localizedRouteNamePrefix}${p}-${r}`,children:d})]}buildRoutePathForLocales(e,t,n,r,i,a,o){if(r){let t=e.includes(o);return a||!t?D(e,n,i):b(n)}return D(e,t,i)}},V=class extends B{generateVariants(e,t){let n=e.path??``,r=h(e.name,e.path),i=t.getAllowedLocales(n,r),a=t.getCustomPathsForPage(n,r),o=f(e.children??[]),s=[];if(s.push(e),a)for(let c of i){let i=a[c];if(!i)continue;let l=b(i),u=b(n);if(l===u)continue;let d=O(i),f=m(r,c,!0),p=this.localizeChildrenForNoPrefix(o,l,n,n,c,t,1);s.push(L(e,{path:d,name:f,children:p,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&c===t.defaultLocale.code&&l!==u&&(e.redirect=l)}return s.push(...z(e,i,t.customRegex,t.localizedRouteNamePrefix)),s}localizeChildrenForNoPrefix(e,t,n,r,i,a,o){return e.map(e=>{let s=I(n,e.path??``),c,l;if(o===1){let n=a.getCustomPath(s,i);if(n){let e=b(n);c=x(e),l=e}else c=x(b(e.path??``)),l=I(t,e.path??``)}else c=x(b(e.path??``)),l=I(t,e.path??``);let u=this.localizeChildrenForNoPrefix(f(e.children??[]),l,s,r,i,a,o+1),d=m(h(e.name,e.path),i,!0);return L(e,{path:c,name:d,children:u})})}};function H(e){let t=e.alias??e.meta?.alias;return Array.isArray(t)?t:[]}var U=class extends B{generateVariants(e,t){let n=e.path??``,r=h(e.name,e.path),i=t.getAllowedLocales(n,r),a=t.getCustomPathsForPage(n,r),o=f(e.children??[]),s=[];if(a)for(let c of i){let i=a[c],l=D(c,i?b(i):n,t.customRegex),u=m(r,c,!!i,t.localizedRouteNamePrefix),d=i??n,f=this.localizeChildren(o,d,n,c,t,!0),p=E(l);s.push(L(e,{path:l,name:u,children:f,alias:p.length?p:void 0,meta:{alias:p.length?p:[]}}))}else{let a=D(i,n,t.customRegex),c=m(r,i[0],!1,t.localizedRouteNamePrefix),l=this.localizeChildrenAllLocales(o,n,n,i,t);s.push(L(e,{path:a,name:c,children:l,alias:[],meta:{alias:[]}}))}let c=H(e);if(c.length)for(let n of c){let r=D(i,n,t.customRegex);s.push(L(e,{path:r,name:`${t.localizedRouteNamePrefix}${e.name??``}`,alias:void 0,meta:{alias:void 0}}))}return s}postProcess(e,t){let n=e.filter(e=>{if(e.name===`index`&&e.path===`/`)return!1;let n=e.path??``;return!!(n&&(0,c.isInternalPath)(n,t.excludePatterns)||M(t.globalLocaleRoutes,n,e.name??``)||/^\/:locale/.test(n)||n===`/`)}),r=t.defaultLocale.code;for(let[e,i]of Object.entries(t.globalLocaleRoutes)){if(i===!1||typeof i!=`object`)continue;let t=i[r];if(!t)continue;let a=e.startsWith(`/`)?e:`/${e}`,o=`/${r}${t.startsWith(`/`)?t:`/${t}`}`;a!==o&&n.push({path:a,name:`redirect-${e.replace(/\//g,`-`)}`,redirect:o})}return t.fallbackRedirectComponentPath&&n.push({path:`/:pathMatch(.*)*`,name:`custom-fallback-route`,file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),n}},W=class extends B{generateVariants(e,t){let n=e.path??``,r=h(e.name,e.path),i=t.getAllowedLocales(n,r),a=t.getCustomPathsForPage(n,r),o=f(e.children??[]),s=[],c=t.defaultLocale.code;s.push(e);let l=t.locales.filter(e=>i.includes(e.code));if(l.length>0){if(a)for(let r of l){let i=a[r.code];if(i){if(r.code===c){let a=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!1,r.code,n,t);a&&s.push(a);let c=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!0,r.code,n,t);c&&s.push(c)}else{let a=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!1,r.code,n,t);a&&s.push(a)}}else{let i=this.createLocalizedRoute(e,[r.code],o,!1,``,t.customRegex,!1,r.code,n,t);i&&s.push(i)}}else{let r=l.map(e=>e.code),i=this.createLocalizedRoute(e,r,o,!1,``,t.customRegex,!1,!0,n,t);i&&s.push(i)}}let u=z(e,i,t.customRegex,t.localizedRouteNamePrefix);return u.length&&s.push(...u),s}postProcess(e,t){let n=[],r=[];for(let i of e){let e=i.name??``;typeof e==`string`&&e.startsWith(t.localizedRouteNamePrefix)?r.push(i):n.push(i)}return[...n,...r]}createLocalizedRoute(e,t,n,r,i=``,a,o=!1,s=!1,c,l){let u=this.buildRoutePathForLocales(t,e.path??``,i,r,a,o,l.defaultLocale.code);if(!u||!(r&&i)&&u===e.path||t.length===0)return null;let d=t[0];if(!d)return null;let f=c??e.path??``,p=m(h(e.name??``,f),d,r,l.localizedRouteNamePrefix),g=o||d!==l.defaultLocale.code;return L(e,{path:u,name:p,children:t.length===1?this.localizeChildren(n,u,f,d,l,g):this.localizeChildrenAllLocales(n,u,f,t,l),alias:[],meta:{alias:[]}})}},G=class extends B{generateVariants(e,t){let n=e.path??``,r=h(e.name,e.path),i=t.getAllowedLocales(n,r),a=t.getCustomPathsForPage(n,r),o=f(e.children??[]),s=[],c=t.defaultLocale.code;if(i.includes(c)){let r=a?.[c],i=r?b(r):n,l=r?{[c]:r}:{},u=this.createLocalizedChildren(o,n,[c],!1,!1,!1,l,t);s.push(L(e,{path:i,name:e.name,children:u}))}let l=t.locales.filter(e=>i.includes(e.code)&&e.code!==c);if(l.length){if(a)for(let r of l){let i=a[r.code];if(i){let a=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!1,r.code,n,t);a&&s.push(a)}else{let i=this.createLocalizedRoute(e,[r.code],o,!1,``,t.customRegex,!1,r.code,n,t);i&&s.push(i)}}else{let r=l.map(e=>e.code),i=this.createLocalizedRoute(e,r,o,!1,``,t.customRegex,!1,!0,n,t);i&&s.push(i)}}let u=z(e,i,t.customRegex,t.localizedRouteNamePrefix);return u.length&&s.push(...u),s}postProcess(e,t){let n=[],r=[];for(let i of e){let e=i.name??``;typeof e==`string`&&e.startsWith(t.localizedRouteNamePrefix)?r.push(i):n.push(i)}let i=[...n,...r];return t.fallbackRedirectComponentPath&&i.push({path:`/:pathMatch(.*)*`,name:`custom-fallback-route`,file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),i}createLocalizedChildren(e,t,n,r=!0,i=!1,a=!1,o={},s){return e.flatMap(e=>this.createLocalizedVariants(e,t,n,r,i,a,o,s))}createLocalizedVariants(e,t,n,r,i,a=!1,o,s){let c=b(e.path??``),l=A(I(t,e.path??``)),u=s.localizedPaths[l],d=!!u,p=[];if(!d){let l=x(c),u=this.createLocalizedChildren(f(e.children??[]),I(t,e.path??``),n,r,i,a,o,s),d=this.buildChildRouteName(e.name,a,s);return p.push(L(e,{path:l,name:d,children:u})),p}for(let a of n){let n=o?.[a],c=!!n,l=u?.[a],d=b(l||(e.path??``));c&&n&&(d=l?b(l):I(n,e.path??``));let m=x(g(a,s.defaultLocale,i)?D(a,d,s.customRegex):d),_=l?b(l):c?n:I(t,e.path??``),v=this.createLocalizedChildren(f(e.children??[]),_,[a],r,i,a,{...o,[a]:_},s),y=this.buildLocalizedRouteName(h(e.name,e.path),a,r,!!u,s);p.push(L(e,{path:m,name:y,children:v}))}return p}buildChildRouteName(e,t,n){return t===!0?`${n.localizedRouteNamePrefix}${e}`:typeof t==`string`?`${n.localizedRouteNamePrefix}${e}-${t}`:e}createLocalizedRoute(e,t,n,r,i=``,a,o=!1,s=!1,c,l){let u=this.buildRoutePathForLocales(t,e.path??``,i,r,a,o,l.defaultLocale.code);if(!u||u===e.path||t.length===0)return null;let d=t[0];if(!d)return null;let f=c??e.path??``;return L(e,{path:u,name:m(h(e.name??``,f),d,r,l.localizedRouteNamePrefix),children:this.createLocalizedChildren(n,f,t,!0,!1,s,{[d]:i},l),alias:[],meta:{alias:[]}})}buildLocalizedRouteName(e,t,n,r=!1,i){return n?r||t&&!_(t,i.defaultLocale)?`${i.localizedRouteNamePrefix}${e}-${t}`:`${i.localizedRouteNamePrefix}${e}`:e}};function K(e){switch(e){case`no_prefix`:return new V;case`prefix`:return new U;case`prefix_and_default`:return new W;case`prefix_except_default`:return new G;default:throw Error(`Unknown route strategy: ${e}`)}}var q=class{constructor(e){this.localizedPaths={};let{locales:t,defaultLocaleCode:n,strategy:r,globalLocaleRoutes:i,filesLocaleRoutes:a={},routeLocales:o={},noPrefixRedirect:s,excludePatterns:c,localizedRouteNamePrefix:l=`localized-`,customRegexMatcher:u,fallbackRedirectComponentPath:d}=e,f=v(t,n);this.locales=f.locales,this.defaultLocale=f.defaultLocale,this.strategy=r,this.noPrefixRedirect=s,this.excludePatterns=c,this.localizedRouteNamePrefix=l,this.customRegex=u,this.fallbackRedirectComponentPath=d,this.activeLocaleCodes=this.computeActiveLocaleCodes();let p={};for(let e in i){let t=y(e),n=i[e];if(typeof n==`object`){let e={};for(let t in n){let r=n[t];r&&(e[t]=y(r))}p[t]=e}else p[t]=n}this.globalLocaleRoutes=p,this.rawGlobalLocaleRoutes=i??{},this.filesLocaleRoutes=a??{},this.routeLocales=o??{}}computeActiveLocaleCodes(){return this.locales.filter(e=>e.code!==this.defaultLocale.code||(0,d.isPrefixAndDefaultStrategy)(this.strategy)||(0,d.isPrefixStrategy)(this.strategy)).map(e=>e.code)}extendPages(e){let t=new F({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;let n=K(this.strategy),r=[...e],i=[];for(let e of r)i.push(...n.processPage(e,t));let a=n.postProcess(i,t);e.length=0,e.push(...a)}extractLocalizedPaths(e,t=``){return j(e,this.globalLocaleRoutes,this.filesLocaleRoutes,t)}ensureTranslationFilesExist(e,t,n,r){this.locales.forEach(i=>{let a=l.default.join(n,t,`${i.code}.json`);this.ensureFileExists(a),r||e.forEach(e=>{let r=l.default.join(n,t,`pages`,`${e}/${i.code}.json`);this.ensureFileExists(r)})})}ensureFileExists(e){try{let t=l.default.dirname(e);(0,u.existsSync)(t)||(0,u.mkdirSync)(t,{recursive:!0}),(0,u.existsSync)(e)||(0,u.writeFileSync)(e,JSON.stringify({}),`utf-8`)}catch{}}resolveLocalizedPath(e,t){let n=y(e),r=x(n)||`/`,i,a=this.globalLocaleRoutes[r],o=this.globalLocaleRoutes[n];a&&typeof a==`object`&&!Array.isArray(a)?i=a[t]:o&&typeof o==`object`&&!Array.isArray(o)&&(i=o[t]);let s=!!i,c=i?b(i):b(n)||``,l=!1;if(this.strategy===`no_prefix`?l=!1:this.strategy===`prefix`||this.strategy===`prefix_and_default`?l=!0:this.strategy===`prefix_except_default`&&(l=t!==this.defaultLocale.code),t===this.defaultLocale.code&&!s&&(this.strategy===`prefix_except_default`||this.strategy===`no_prefix`)&&(l=!1),!l)return b(c)||`/`;let u=x(c);return u?b(`/${t}/${u}`):`/${t}`}generateDataRoutes(e,t,n){let r=[],i=this.collectPageNames(e);for(let e of this.locales)if(r.push(`/${t}/index/${e.code}/data.json`),!n)for(let n of i)r.push(`/${t}/${n}/${e.code}/data.json`);return r}collectPageNames(e){let t=new Set,n=e=>{for(let r of e)typeof r.name==`string`&&r.name.length>0&&t.add(r.name),r.children&&r.children.length>0&&n(r.children)};return n(e),[...t]}};exports.RouteGenerator=q,exports.buildEncodedPathAliases=E,exports.buildFullPath=D,exports.buildFullPathNoPrefix=O,exports.buildRouteName=m,exports.cloneArray=f,exports.extractLocalizedPaths=j,Object.defineProperty(exports,"isInternalPath",{enumerable:!0,get:function(){return c.isInternalPath}}),exports.isLocaleAllowedForUnlocalizedRoute=N,exports.isLocaleDefault=_,exports.isLocalizationDisabledForPage=M,exports.isPageRedirectOnly=p,exports.normalizePath=b,exports.normalizeRouteKey=y,exports.removeLeadingSlash=x,exports.shouldAddLocalePrefix=g;
|