@i18n-micro/route-strategy 1.1.0 → 1.1.2
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/core/context.d.ts +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.mjs +160 -202
- package/dist/route-generator.d.ts +2 -2
- package/dist/utils/common.d.ts +1 -1
- package/dist/utils/index.d.ts +3 -3
- package/package.json +3 -3
package/dist/core/context.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NuxtPage } from '@nuxt/schema';
|
|
2
1
|
import { GlobalLocaleRoutes, Locale, Strategies } from '@i18n-micro/types';
|
|
2
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
3
3
|
import { LocaleRoutesConfig } from '../strategies/types';
|
|
4
4
|
import { LocalizedPathsMap } from './localized-paths';
|
|
5
5
|
export interface GeneratorContextOptions {
|
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");function $(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 H(l){if(!(typeof l>"u"))return l.startsWith("/")&&l.endsWith("/")?l?.slice(1,-1):l}function K(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=H(t?.toString()),o=a||(Array.isArray(l)?l.join("|"):l),s=K(e);return R(w.posix.join("/",`:locale(${o})`,s))}function M(l){const e=K(l);return R(e)}const b=l=>l.map(e=>({...e})),T=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,""),V=(l,e,t,a)=>t&&!(l===e.code&&!a),I=(l,e,t)=>(typeof l=="string"?l:l.code)===e.code&&!t,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 E(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 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}}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=$(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=$(t),o=l[t];if(typeof o=="object"){const s={};for(const i in o){const r=o[i];r&&(s[i]=$(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(T(e))return[e];if(e.path&&E(e.path,t.excludePatterns))return[e];const a=e.path??"",o=C(e.name,e.path),s=$(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=M(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(o=>{if(o.name==="index"&&o.path==="/")return!1;const s=o.path??"";return!!(s&&E(s,t.excludePatterns)||t.globalLocaleRoutes[o.name??""]===!1||/^\/:locale/.test(s)||s==="/")});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,x=u?.[P];let A=R(x||(e.path??""));y&&L&&(x?A=R(x):A=z(L,e.path??""));const v=V(P,n.defaultLocale,s,!1)?N(P,A,n.customRegex):A,q=g(v),G=x?R(x):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&&!I(t,s.defaultLocale,!1)?`${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=Q(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=$(P),y=s[P];if(typeof y=="object"){const x={};for(const A in y){const v=y[A];v&&(x[A]=$(v))}f[L]=x}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){const t=w.dirname(e);S.existsSync(t)||S.mkdirSync(t,{recursive:!0}),S.existsSync(e)||S.writeFileSync(e,JSON.stringify({}),"utf-8")}resolveLocalizedPath(e,t){const a=$(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=e.map(i=>i.name).filter(i=>typeof i=="string"&&i.length>0);for(const i of this.locales)if(o.push(`/${t}/general/${i.code}/data.json`),!a)for(const r of s)o.push(`/${t}/${r}/${i.code}/data.json`);return o}}exports.RouteGenerator=re;exports.buildFullPath=N;exports.buildFullPathNoPrefix=M;exports.buildRouteName=F;exports.cloneArray=b;exports.extractLocalizedPaths=_;exports.isInternalPath=E;exports.isLocaleDefault=I;exports.isPageRedirectOnly=T;exports.normalizePath=R;exports.normalizeRouteKey=$;exports.removeLeadingSlash=g;exports.shouldAddLocalePrefix=V;
|
|
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,a)=>t&&!(l===e.code&&!a),T=(l,e,t)=>(typeof l=="string"?l:l.code)===e.code&&!t,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??"")),c=j(r),d=x(r),n=e[c]||e[d]||e[i];if(n)typeof n=="object"&&(o[c]=n);else{const h=t[i];h&&typeof h=="object"&&!Array.isArray(h)&&(o[c]=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??""),c=j(r),d=s.localizedPaths[c];if(d)return o.flatMap(f=>{const P=d[f]??z(t,e.path??"");return this.localizeChild(e,P,a,f,s,!0)});const n=R(e.path??""),h=g(n),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??""),c=s.getCustomPath(r,o),d=g(R(c||(e.path??""))),n=c??z(t,e.path??""),h=this.localizeChildren(b(e.children??[]),n,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 c=e.includes(r);return i||!c?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??[]),c=[];if(c.push(e),i)for(const d of s){const n=i[d];if(!n)continue;const h=R(n),u=R(a);if(h===u)continue;const m=I(n),f=F(o,d,!0),P=this.localizeChildrenForNoPrefix(r,h,a,a,d,t,1);c.push(p(e,{path:m,name:f,children:P,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&d===t.defaultLocale.code&&h!==u&&(e.redirect=h)}return c.push(...D(e,s,t.customRegex,t.localizedRouteNamePrefix)),c}localizeChildrenForNoPrefix(e,t,a,o,s,i,r){return e.map(c=>{const d=z(a,c.path??"");let n,h;if(r===1){const P=i.getCustomPath(d,s);if(P){const L=R(P);n=g(L),h=L}else{const L=R(c.path??"");n=g(L),h=z(t,c.path??"")}}else{const P=R(c.path??"");n=g(P),h=z(t,c.path??"")}const u=this.localizeChildrenForNoPrefix(b(c.children??[]),h,d,o,s,i,r+1),m=C(c.name,c.path),f=F(m,s,!0);return p(c,{path:n,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??[]),c=[];if(i)for(const n of s){const h=i[n],u=h?R(h):a,m=N(n,u,t.customRegex),f=F(o,n,!!h,t.localizedRouteNamePrefix),P=h??a,L=this.localizeChildren(r,P,a,n,t,!0);c.push(p(e,{path:m,name:f,children:L,alias:[],meta:{alias:[]}}))}else{const n=N(s,a,t.customRegex),h=F(o,s[0],!1,t.localizedRouteNamePrefix),u=this.localizeChildrenAllLocales(r,a,a,s,t);c.push(p(e,{path:n,name:h,children:u,alias:[],meta:{alias:[]}}))}const d=ae(e);if(d.length)for(const n of d){const h=N(s,n,t.customRegex);c.push(p(e,{path:h,name:`${t.localizedRouteNamePrefix}${e.name??""}`,alias:void 0,meta:{alias:void 0}}))}return c}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 c=s.startsWith("/")?s:`/${s}`,d=`/${o}${r.startsWith("/")?r:`/${r}`}`;c!==d&&a.push({path:c,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??[]),c=[],d=t.defaultLocale.code;c.push(e);const n=t.locales.filter(u=>s.includes(u.code));if(n.length>0)if(i)for(const u of n){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&&c.push(f);const P=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!0,u.code,a,t);P&&c.push(P)}else{const f=this.createLocalizedRoute(e,[u.code],r,!0,m,t.customRegex,!1,u.code,a,t);f&&c.push(f)}else{const f=this.createLocalizedRoute(e,[u.code],r,!1,"",t.customRegex,!1,u.code,a,t);f&&c.push(f)}}else{const u=n.map(f=>f.code),m=this.createLocalizedRoute(e,u,r,!1,"",t.customRegex,!1,!0,a,t);m&&c.push(m)}const h=D(e,s,t.customRegex,t.localizedRouteNamePrefix);return h.length&&c.push(...h),c}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,c=!1,d,n){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(s),o,i,r,n.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,n.localizedRouteNamePrefix),L=r||m!==n.defaultLocale.code,y=t.length===1?this.localizeChildren(a,h,f,m,n,L):this.localizeChildrenAllLocales(a,h,f,t,n);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??[]),c=[],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);c.push(p(e,{path:m,name:e.name,children:P}))}const n=t.locales.filter(u=>s.includes(u.code)&&u.code!==d);if(n.length)if(i)for(const u of n){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&&c.push(f)}else{const f=this.createLocalizedRoute(e,[u.code],r,!1,"",t.customRegex,!1,u.code,a,t);f&&c.push(f)}}else{const u=n.map(f=>f.code),m=this.createLocalizedRoute(e,u,r,!1,"",t.customRegex,!1,!0,a,t);m&&c.push(m)}const h=D(e,s,t.customRegex,t.localizedRouteNamePrefix);return h.length&&c.push(...h),c}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={},c){return e.flatMap(d=>this.createLocalizedVariants(d,t,a,o,s,i,r,c))}createLocalizedVariants(e,t,a,o,s,i=!1,r,c){const d=R(e.path??""),n=z(t,e.path??""),h=j(n),u=c.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,c),y=this.buildChildRouteName(e.name,i,c);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,c.defaultLocale,s,!1)?N(P,A,c.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},c),U=this.buildLocalizedRouteName(C(e.name,e.path),P,o,!!u,c);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,c=!1,d,n){const h=this.buildRoutePathForLocales(t,e.path??"",encodeURI(s),o,i,r,n.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,n.localizedRouteNamePrefix);return p(e,{path:h,name:f,children:this.createLocalizedChildren(a,m,t,!0,!1,c,{[u]:s},n),alias:[],meta:{alias:[]}})}buildLocalizedRouteName(e,t,a,o=!1,s){return a?o?`${s.localizedRouteNamePrefix}${e}-${t}`:t&&!T(t,s.defaultLocale,!1)?`${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:c,excludePatterns:d,localizedRouteNamePrefix:n="localized-",customRegexMatcher:h,fallbackRedirectComponentPath:u}=e,m=J(t,a);this.locales=m.locales,this.defaultLocale=m.defaultLocale,this.strategy=o,this.noPrefixRedirect=c,this.excludePatterns=d,this.localizedRouteNamePrefix=n,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 c=w.join(a,t,"pages",`${r}/${s.code}.json`);this.ensureFileExists(c)})})}ensureFileExists(e){const t=w.dirname(e);S.existsSync(t)||S.mkdirSync(t,{recursive:!0}),S.existsSync(e)||S.writeFileSync(e,JSON.stringify({}),"utf-8")}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 c=!!s,d=s?R(s):R(a)||"";let n=!1;if(this.strategy==="no_prefix"?n=!1:this.strategy==="prefix"||this.strategy==="prefix_and_default"?n=!0:this.strategy==="prefix_except_default"&&(n=t!==this.defaultLocale.code),t===this.defaultLocale.code&&!c&&(this.strategy==="prefix_except_default"||this.strategy==="no_prefix")&&(n=!1),!n)return R(d)||"/";const h=g(d);return h?R(`/${t}/${h}`):`/${t}`}generateDataRoutes(e,t,a){const o=[],s=e.map(i=>i.name).filter(i=>typeof i=="string"&&i.length>0);for(const i of this.locales)if(o.push(`/${t}/general/${i.code}/data.json`),!a)for(const r of s)o.push(`/${t}/${r}/${i.code}/data.json`);return o}}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;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { RouteGenerator, RouteGeneratorOptions } from './route-generator';
|
|
2
1
|
import { extractLocalizedPaths, LocalizedPathsMap } from './core/localized-paths';
|
|
3
|
-
import {
|
|
2
|
+
import { RouteGenerator, RouteGeneratorOptions } from './route-generator';
|
|
3
|
+
import { buildFullPath, buildFullPathNoPrefix, buildRouteName, cloneArray, isInternalPath, isLocaleDefault, isPageRedirectOnly, normalizePath, normalizeRouteKey, removeLeadingSlash, shouldAddLocalePrefix } from './utils';
|
|
4
4
|
export { RouteGenerator, type RouteGeneratorOptions, extractLocalizedPaths, type LocalizedPathsMap, isInternalPath, normalizeRouteKey, normalizePath, cloneArray, isPageRedirectOnly, removeLeadingSlash, buildRouteName, shouldAddLocalePrefix, isLocaleDefault, buildFullPath, buildFullPathNoPrefix, };
|
package/dist/index.mjs
CHANGED
|
@@ -1,34 +1,7 @@
|
|
|
1
1
|
import N from "node:path";
|
|
2
2
|
import { existsSync as E, mkdirSync as B, writeFileSync as U } from "node:fs";
|
|
3
3
|
import { isPrefixAndDefaultStrategy as D, isPrefixStrategy as O } from "@i18n-micro/core";
|
|
4
|
-
|
|
5
|
-
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("/");
|
|
6
|
-
}
|
|
7
|
-
const R = (l) => {
|
|
8
|
-
if (!l)
|
|
9
|
-
return "";
|
|
10
|
-
const e = N.posix.normalize(l).replace(/\/+$/, "");
|
|
11
|
-
return e === "." ? "" : e;
|
|
12
|
-
}, y = (l) => l.startsWith("/") ? l.slice(1) : l;
|
|
13
|
-
function G(...l) {
|
|
14
|
-
return N.posix.join(...l);
|
|
15
|
-
}
|
|
16
|
-
function q(l) {
|
|
17
|
-
if (!(typeof l > "u"))
|
|
18
|
-
return l.startsWith("/") && l.endsWith("/") ? l?.slice(1, -1) : l;
|
|
19
|
-
}
|
|
20
|
-
function K(l) {
|
|
21
|
-
return !l || !/[\u0080-\uFFFF]/.test(l) ? l : l.split("/").map((e) => !e || e.startsWith(":") || !/[\u0080-\uFFFF]/.test(e) ? e : encodeURI(e)).join("/");
|
|
22
|
-
}
|
|
23
|
-
function A(l, e, t) {
|
|
24
|
-
const a = q(t?.toString()), o = a || (Array.isArray(l) ? l.join("|") : l), s = K(e);
|
|
25
|
-
return R(N.posix.join("/", `:locale(${o})`, s));
|
|
26
|
-
}
|
|
27
|
-
function H(l) {
|
|
28
|
-
const e = K(l);
|
|
29
|
-
return R(e);
|
|
30
|
-
}
|
|
31
|
-
const $ = (l) => l.map((e) => ({ ...e })), J = (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, ""), Q = (l, e, t, a) => t && !(l === e.code && !a), X = (l, e, t) => (typeof l == "string" ? l : l.code) === e.code && !t, Y = [
|
|
4
|
+
const $ = (l) => l.map((e) => ({ ...e })), q = (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, ""), H = (l, e, t, a) => t && !(l === e.code && !a), J = (l, e, t) => (typeof l == "string" ? l : l.code) === e.code && !t, Q = [
|
|
32
5
|
/^\/sitemap.*\.xml$/,
|
|
33
6
|
/^\/sitemap\.xml$/,
|
|
34
7
|
/^\/robots\.txt$/,
|
|
@@ -39,10 +12,10 @@ const $ = (l) => l.map((e) => ({ ...e })), J = (l) => !!(l.redirect && !l.file),
|
|
|
39
12
|
/^\/workbox-.*\.js$/,
|
|
40
13
|
/\.(xml|txt|ico|json|js|css|png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/
|
|
41
14
|
];
|
|
42
|
-
function
|
|
15
|
+
function K(l, e) {
|
|
43
16
|
if (/(?:^|\/)__[^/]+/.test(l))
|
|
44
17
|
return !0;
|
|
45
|
-
for (const t of
|
|
18
|
+
for (const t of Q)
|
|
46
19
|
if (t.test(l))
|
|
47
20
|
return !0;
|
|
48
21
|
if (e) {
|
|
@@ -57,13 +30,40 @@ function M(l, e) {
|
|
|
57
30
|
}
|
|
58
31
|
return !1;
|
|
59
32
|
}
|
|
60
|
-
function
|
|
33
|
+
function X(l, e) {
|
|
61
34
|
const t = l.reduce((o, s) => {
|
|
62
35
|
const i = o.find((r) => r.code === s.code);
|
|
63
36
|
return i ? Object.assign(i, s) : o.push(s), o;
|
|
64
37
|
}, []).filter((o) => !o.disabled), a = t.find((o) => o.code === e) ?? { code: e };
|
|
65
38
|
return { locales: t, defaultLocale: a };
|
|
66
39
|
}
|
|
40
|
+
function w(l) {
|
|
41
|
+
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("/");
|
|
42
|
+
}
|
|
43
|
+
const R = (l) => {
|
|
44
|
+
if (!l)
|
|
45
|
+
return "";
|
|
46
|
+
const e = N.posix.normalize(l).replace(/\/+$/, "");
|
|
47
|
+
return e === "." ? "" : e;
|
|
48
|
+
}, y = (l) => l.startsWith("/") ? l.slice(1) : l;
|
|
49
|
+
function G(...l) {
|
|
50
|
+
return N.posix.join(...l);
|
|
51
|
+
}
|
|
52
|
+
function Y(l) {
|
|
53
|
+
if (!(typeof l > "u"))
|
|
54
|
+
return l.startsWith("/") && l.endsWith("/") ? l?.slice(1, -1) : l;
|
|
55
|
+
}
|
|
56
|
+
function M(l) {
|
|
57
|
+
return !l || !/[\u0080-\uFFFF]/.test(l) ? l : l.split("/").map((e) => !e || e.startsWith(":") || !/[\u0080-\uFFFF]/.test(e) ? e : encodeURI(e)).join("/");
|
|
58
|
+
}
|
|
59
|
+
function A(l, e, t) {
|
|
60
|
+
const a = Y(t?.toString()), o = a || (Array.isArray(l) ? l.join("|") : l), s = M(e);
|
|
61
|
+
return R(N.posix.join("/", `:locale(${o})`, s));
|
|
62
|
+
}
|
|
63
|
+
function Z(l) {
|
|
64
|
+
const e = M(l);
|
|
65
|
+
return R(e);
|
|
66
|
+
}
|
|
67
67
|
const ee = (l, e) => l ?? (e ?? "").replace(/[^a-z0-9]/gi, "-").replace(/^-+|-+$/g, "");
|
|
68
68
|
function F(l) {
|
|
69
69
|
return y(l) || "/";
|
|
@@ -71,12 +71,12 @@ function F(l) {
|
|
|
71
71
|
function S(l, e, t, a = "") {
|
|
72
72
|
const o = {};
|
|
73
73
|
return l.forEach((s) => {
|
|
74
|
-
const i = ee(s.name, s.path), r = R(G(a, s.path ?? "")),
|
|
75
|
-
if (
|
|
76
|
-
typeof
|
|
74
|
+
const i = ee(s.name, s.path), r = R(G(a, s.path ?? "")), c = F(r), d = w(r), n = e[c] || e[d] || e[i];
|
|
75
|
+
if (n)
|
|
76
|
+
typeof n == "object" && (o[c] = n);
|
|
77
77
|
else {
|
|
78
78
|
const h = t[i];
|
|
79
|
-
h && typeof h == "object" && !Array.isArray(h) && (o[
|
|
79
|
+
h && typeof h == "object" && !Array.isArray(h) && (o[c] = h);
|
|
80
80
|
}
|
|
81
81
|
if (s.children?.length) {
|
|
82
82
|
const h = R(G(a, s.path ?? ""));
|
|
@@ -103,13 +103,7 @@ function te(l) {
|
|
|
103
103
|
}
|
|
104
104
|
class ae {
|
|
105
105
|
constructor(e) {
|
|
106
|
-
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 = S(
|
|
107
|
-
e.pages,
|
|
108
|
-
this.globalLocaleRoutes,
|
|
109
|
-
this.filesLocaleRoutes
|
|
110
|
-
), this.activeLocaleCodes = this.locales.filter(
|
|
111
|
-
(t) => t.code !== this.defaultLocale.code || D(this.strategy) || O(this.strategy)
|
|
112
|
-
).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;
|
|
106
|
+
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 = S(e.pages, this.globalLocaleRoutes, this.filesLocaleRoutes), this.activeLocaleCodes = this.locales.filter((t) => t.code !== this.defaultLocale.code || D(this.strategy) || O(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;
|
|
113
107
|
}
|
|
114
108
|
findLocaleByCode(e, t) {
|
|
115
109
|
return e.find((a) => a.code === t);
|
|
@@ -119,9 +113,7 @@ class ae {
|
|
|
119
113
|
*/
|
|
120
114
|
getAllowedLocales(e, t) {
|
|
121
115
|
const a = this.routeLocales[e] ?? this.routeLocales[t];
|
|
122
|
-
return a?.length ? a.filter(
|
|
123
|
-
(o) => this.locales.some((s) => s.code === o)
|
|
124
|
-
) : this.locales.map((o) => o.code);
|
|
116
|
+
return a?.length ? a.filter((o) => this.locales.some((s) => s.code === o)) : this.locales.map((o) => o.code);
|
|
125
117
|
}
|
|
126
118
|
/**
|
|
127
119
|
* Returns a custom path for a given locale by original page path, if any.
|
|
@@ -185,9 +177,9 @@ class k {
|
|
|
185
177
|
processPage(e, t) {
|
|
186
178
|
if (e.path === void 0)
|
|
187
179
|
throw new Error("page.path is required");
|
|
188
|
-
if (
|
|
180
|
+
if (q(e))
|
|
189
181
|
return [e];
|
|
190
|
-
if (e.path &&
|
|
182
|
+
if (e.path && K(e.path, t.excludePatterns))
|
|
191
183
|
return [e];
|
|
192
184
|
const a = e.path ?? "", o = b(e.name, e.path), s = w(a);
|
|
193
185
|
return t.globalLocaleRoutes[o] === !1 || t.globalLocaleRoutes[s] === !1 ? [e] : this.generateVariants(e, t);
|
|
@@ -212,19 +204,13 @@ class k {
|
|
|
212
204
|
return e.flatMap((i) => this.localizeChildAllLocales(i, t, a, o, s));
|
|
213
205
|
}
|
|
214
206
|
localizeChildAllLocales(e, t, a, o, s) {
|
|
215
|
-
const i = z(a, e.path ?? ""), r = z(t, e.path ?? ""),
|
|
207
|
+
const i = z(a, e.path ?? ""), r = z(t, e.path ?? ""), c = F(r), d = s.localizedPaths[c];
|
|
216
208
|
if (d)
|
|
217
209
|
return o.flatMap((f) => {
|
|
218
210
|
const P = d[f] ?? z(t, e.path ?? "");
|
|
219
211
|
return this.localizeChild(e, P, a, f, s, !0);
|
|
220
212
|
});
|
|
221
|
-
const
|
|
222
|
-
$(e.children ?? []),
|
|
223
|
-
r,
|
|
224
|
-
i,
|
|
225
|
-
o,
|
|
226
|
-
s
|
|
227
|
-
), m = `${s.localizedRouteNamePrefix}${e.name ?? ""}`;
|
|
213
|
+
const n = R(e.path ?? ""), h = y(n), u = this.localizeChildrenAllLocales($(e.children ?? []), r, i, o, s), m = `${s.localizedRouteNamePrefix}${e.name ?? ""}`;
|
|
228
214
|
return [
|
|
229
215
|
p(e, {
|
|
230
216
|
path: h,
|
|
@@ -234,14 +220,7 @@ class k {
|
|
|
234
220
|
];
|
|
235
221
|
}
|
|
236
222
|
localizeChild(e, t, a, o, s, i) {
|
|
237
|
-
const r = z(a, e.path ?? ""),
|
|
238
|
-
$(e.children ?? []),
|
|
239
|
-
c,
|
|
240
|
-
r,
|
|
241
|
-
o,
|
|
242
|
-
s,
|
|
243
|
-
i
|
|
244
|
-
), u = b(e.name, e.path), m = `${s.localizedRouteNamePrefix}${u}-${o}`;
|
|
223
|
+
const r = z(a, e.path ?? ""), c = s.getCustomPath(r, o), d = y(R(c || (e.path ?? ""))), n = c ?? z(t, e.path ?? ""), h = this.localizeChildren($(e.children ?? []), n, r, o, s, i), u = b(e.name, e.path), m = `${s.localizedRouteNamePrefix}${u}-${o}`;
|
|
245
224
|
return [
|
|
246
225
|
p(e, {
|
|
247
226
|
path: d,
|
|
@@ -259,32 +238,24 @@ class k {
|
|
|
259
238
|
*/
|
|
260
239
|
buildRoutePathForLocales(e, t, a, o, s, i, r) {
|
|
261
240
|
if (o) {
|
|
262
|
-
const
|
|
263
|
-
return i || !
|
|
241
|
+
const c = e.includes(r);
|
|
242
|
+
return i || !c ? A(e, a, s) : R(a);
|
|
264
243
|
}
|
|
265
244
|
return A(e, t, s);
|
|
266
245
|
}
|
|
267
246
|
}
|
|
268
247
|
class oe extends k {
|
|
269
248
|
generateVariants(e, t) {
|
|
270
|
-
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []),
|
|
271
|
-
if (
|
|
249
|
+
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []), c = [];
|
|
250
|
+
if (c.push(e), i)
|
|
272
251
|
for (const d of s) {
|
|
273
|
-
const
|
|
274
|
-
if (!
|
|
275
|
-
const h = R(
|
|
252
|
+
const n = i[d];
|
|
253
|
+
if (!n) continue;
|
|
254
|
+
const h = R(n), u = R(a);
|
|
276
255
|
if (h === u)
|
|
277
256
|
continue;
|
|
278
|
-
const m =
|
|
279
|
-
|
|
280
|
-
h,
|
|
281
|
-
a,
|
|
282
|
-
a,
|
|
283
|
-
d,
|
|
284
|
-
t,
|
|
285
|
-
1
|
|
286
|
-
);
|
|
287
|
-
n.push(
|
|
257
|
+
const m = Z(n), f = j(o, d, !0), P = this.localizeChildrenForNoPrefix(r, h, a, a, d, t, 1);
|
|
258
|
+
c.push(
|
|
288
259
|
p(e, {
|
|
289
260
|
path: m,
|
|
290
261
|
name: f,
|
|
@@ -294,7 +265,7 @@ class oe extends k {
|
|
|
294
265
|
})
|
|
295
266
|
), t.noPrefixRedirect && d === t.defaultLocale.code && h !== u && (e.redirect = h);
|
|
296
267
|
}
|
|
297
|
-
return
|
|
268
|
+
return c.push(..._(e, s, t.customRegex, t.localizedRouteNamePrefix)), c;
|
|
298
269
|
}
|
|
299
270
|
/**
|
|
300
271
|
* Localizes the children tree for the no_prefix strategy.
|
|
@@ -303,33 +274,33 @@ class oe extends k {
|
|
|
303
274
|
* - on deeper levels, path stays as a segment (item, detail, etc.).
|
|
304
275
|
*/
|
|
305
276
|
localizeChildrenForNoPrefix(e, t, a, o, s, i, r) {
|
|
306
|
-
return e.map((
|
|
307
|
-
const d = z(a,
|
|
308
|
-
let
|
|
277
|
+
return e.map((c) => {
|
|
278
|
+
const d = z(a, c.path ?? "");
|
|
279
|
+
let n, h;
|
|
309
280
|
if (r === 1) {
|
|
310
281
|
const P = i.getCustomPath(d, s);
|
|
311
282
|
if (P) {
|
|
312
283
|
const L = R(P);
|
|
313
|
-
|
|
284
|
+
n = y(L), h = L;
|
|
314
285
|
} else {
|
|
315
|
-
const L = R(
|
|
316
|
-
|
|
286
|
+
const L = R(c.path ?? "");
|
|
287
|
+
n = y(L), h = z(t, c.path ?? "");
|
|
317
288
|
}
|
|
318
289
|
} else {
|
|
319
|
-
const P = R(
|
|
320
|
-
|
|
290
|
+
const P = R(c.path ?? "");
|
|
291
|
+
n = y(P), h = z(t, c.path ?? "");
|
|
321
292
|
}
|
|
322
293
|
const u = this.localizeChildrenForNoPrefix(
|
|
323
|
-
$(
|
|
294
|
+
$(c.children ?? []),
|
|
324
295
|
h,
|
|
325
296
|
d,
|
|
326
297
|
o,
|
|
327
298
|
s,
|
|
328
299
|
i,
|
|
329
300
|
r + 1
|
|
330
|
-
), m = b(
|
|
331
|
-
return p(
|
|
332
|
-
path:
|
|
301
|
+
), m = b(c.name, c.path), f = j(m, s, !0);
|
|
302
|
+
return p(c, {
|
|
303
|
+
path: n,
|
|
333
304
|
name: f,
|
|
334
305
|
children: u
|
|
335
306
|
});
|
|
@@ -342,18 +313,11 @@ function le(l) {
|
|
|
342
313
|
}
|
|
343
314
|
class ie extends k {
|
|
344
315
|
generateVariants(e, t) {
|
|
345
|
-
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []),
|
|
316
|
+
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []), c = [];
|
|
346
317
|
if (i)
|
|
347
|
-
for (const
|
|
348
|
-
const h = i[
|
|
349
|
-
|
|
350
|
-
P,
|
|
351
|
-
a,
|
|
352
|
-
c,
|
|
353
|
-
t,
|
|
354
|
-
!0
|
|
355
|
-
);
|
|
356
|
-
n.push(
|
|
318
|
+
for (const n of s) {
|
|
319
|
+
const h = i[n], u = h ? R(h) : a, m = A(n, u, t.customRegex), f = j(o, n, !!h, t.localizedRouteNamePrefix), P = h ?? a, L = this.localizeChildren(r, P, a, n, t, !0);
|
|
320
|
+
c.push(
|
|
357
321
|
p(e, {
|
|
358
322
|
path: m,
|
|
359
323
|
name: f,
|
|
@@ -364,16 +328,10 @@ class ie extends k {
|
|
|
364
328
|
);
|
|
365
329
|
}
|
|
366
330
|
else {
|
|
367
|
-
const
|
|
368
|
-
|
|
369
|
-
a,
|
|
370
|
-
a,
|
|
371
|
-
s,
|
|
372
|
-
t
|
|
373
|
-
);
|
|
374
|
-
n.push(
|
|
331
|
+
const n = A(s, a, t.customRegex), h = j(o, s[0], !1, t.localizedRouteNamePrefix), u = this.localizeChildrenAllLocales(r, a, a, s, t);
|
|
332
|
+
c.push(
|
|
375
333
|
p(e, {
|
|
376
|
-
path:
|
|
334
|
+
path: n,
|
|
377
335
|
name: h,
|
|
378
336
|
children: u,
|
|
379
337
|
alias: [],
|
|
@@ -383,9 +341,9 @@ class ie extends k {
|
|
|
383
341
|
}
|
|
384
342
|
const d = le(e);
|
|
385
343
|
if (d.length)
|
|
386
|
-
for (const
|
|
387
|
-
const h = A(s,
|
|
388
|
-
|
|
344
|
+
for (const n of d) {
|
|
345
|
+
const h = A(s, n, t.customRegex);
|
|
346
|
+
c.push(
|
|
389
347
|
p(e, {
|
|
390
348
|
path: h,
|
|
391
349
|
name: `${t.localizedRouteNamePrefix}${e.name ?? ""}`,
|
|
@@ -394,14 +352,25 @@ class ie extends k {
|
|
|
394
352
|
})
|
|
395
353
|
);
|
|
396
354
|
}
|
|
397
|
-
return
|
|
355
|
+
return c;
|
|
398
356
|
}
|
|
399
357
|
postProcess(e, t) {
|
|
400
|
-
const a = e.filter((
|
|
401
|
-
if (
|
|
402
|
-
const
|
|
403
|
-
return !!(
|
|
404
|
-
});
|
|
358
|
+
const a = e.filter((s) => {
|
|
359
|
+
if (s.name === "index" && s.path === "/") return !1;
|
|
360
|
+
const i = s.path ?? "";
|
|
361
|
+
return !!(i && K(i, t.excludePatterns) || t.globalLocaleRoutes[s.name ?? ""] === !1 || /^\/:locale/.test(i) || i === "/");
|
|
362
|
+
}), o = t.defaultLocale.code;
|
|
363
|
+
for (const [s, i] of Object.entries(t.globalLocaleRoutes)) {
|
|
364
|
+
if (i === !1 || typeof i != "object") continue;
|
|
365
|
+
const r = i[o];
|
|
366
|
+
if (!r) continue;
|
|
367
|
+
const c = s.startsWith("/") ? s : `/${s}`, d = `/${o}${r.startsWith("/") ? r : `/${r}`}`;
|
|
368
|
+
c !== d && a.push({
|
|
369
|
+
path: c,
|
|
370
|
+
name: `redirect-${s.replace(/\//g, "-")}`,
|
|
371
|
+
redirect: d
|
|
372
|
+
});
|
|
373
|
+
}
|
|
405
374
|
return t.fallbackRedirectComponentPath && a.push({
|
|
406
375
|
path: "/:pathMatch(.*)*",
|
|
407
376
|
name: "custom-fallback-route",
|
|
@@ -414,12 +383,12 @@ class ie extends k {
|
|
|
414
383
|
}
|
|
415
384
|
class re extends k {
|
|
416
385
|
generateVariants(e, t) {
|
|
417
|
-
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []),
|
|
418
|
-
|
|
419
|
-
const
|
|
420
|
-
if (
|
|
386
|
+
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []), c = [], d = t.defaultLocale.code;
|
|
387
|
+
c.push(e);
|
|
388
|
+
const n = t.locales.filter((u) => s.includes(u.code));
|
|
389
|
+
if (n.length > 0)
|
|
421
390
|
if (i)
|
|
422
|
-
for (const u of
|
|
391
|
+
for (const u of n) {
|
|
423
392
|
const m = i[u.code];
|
|
424
393
|
if (m)
|
|
425
394
|
if (u.code === d) {
|
|
@@ -435,7 +404,7 @@ class re extends k {
|
|
|
435
404
|
a,
|
|
436
405
|
t
|
|
437
406
|
);
|
|
438
|
-
f &&
|
|
407
|
+
f && c.push(f);
|
|
439
408
|
const P = this.createLocalizedRoute(
|
|
440
409
|
e,
|
|
441
410
|
[u.code],
|
|
@@ -448,7 +417,7 @@ class re extends k {
|
|
|
448
417
|
a,
|
|
449
418
|
t
|
|
450
419
|
);
|
|
451
|
-
P &&
|
|
420
|
+
P && c.push(P);
|
|
452
421
|
} else {
|
|
453
422
|
const f = this.createLocalizedRoute(
|
|
454
423
|
e,
|
|
@@ -462,7 +431,7 @@ class re extends k {
|
|
|
462
431
|
a,
|
|
463
432
|
t
|
|
464
433
|
);
|
|
465
|
-
f &&
|
|
434
|
+
f && c.push(f);
|
|
466
435
|
}
|
|
467
436
|
else {
|
|
468
437
|
const f = this.createLocalizedRoute(
|
|
@@ -477,11 +446,11 @@ class re extends k {
|
|
|
477
446
|
a,
|
|
478
447
|
t
|
|
479
448
|
);
|
|
480
|
-
f &&
|
|
449
|
+
f && c.push(f);
|
|
481
450
|
}
|
|
482
451
|
}
|
|
483
452
|
else {
|
|
484
|
-
const u =
|
|
453
|
+
const u = n.map((f) => f.code), m = this.createLocalizedRoute(
|
|
485
454
|
e,
|
|
486
455
|
u,
|
|
487
456
|
r,
|
|
@@ -493,10 +462,10 @@ class re extends k {
|
|
|
493
462
|
a,
|
|
494
463
|
t
|
|
495
464
|
);
|
|
496
|
-
m &&
|
|
465
|
+
m && c.push(m);
|
|
497
466
|
}
|
|
498
467
|
const h = _(e, s, t.customRegex, t.localizedRouteNamePrefix);
|
|
499
|
-
return h.length &&
|
|
468
|
+
return h.length && c.push(...h), c;
|
|
500
469
|
}
|
|
501
470
|
postProcess(e, t) {
|
|
502
471
|
const a = [], o = [];
|
|
@@ -506,7 +475,7 @@ class re extends k {
|
|
|
506
475
|
}
|
|
507
476
|
return [...a, ...o];
|
|
508
477
|
}
|
|
509
|
-
createLocalizedRoute(e, t, a, o, s = "", i, r = !1,
|
|
478
|
+
createLocalizedRoute(e, t, a, o, s = "", i, r = !1, c = !1, d, n) {
|
|
510
479
|
const h = this.buildRoutePathForLocales(
|
|
511
480
|
t,
|
|
512
481
|
e.path ?? "",
|
|
@@ -514,12 +483,17 @@ class re extends k {
|
|
|
514
483
|
o,
|
|
515
484
|
i,
|
|
516
485
|
r,
|
|
517
|
-
|
|
486
|
+
n.defaultLocale.code
|
|
518
487
|
);
|
|
519
488
|
if (!h || !(o && s) && h === e.path || t.length === 0) return null;
|
|
520
489
|
const m = t[0];
|
|
521
490
|
if (!m) return null;
|
|
522
|
-
const f = d ?? e.path ?? "", P = j(
|
|
491
|
+
const f = d ?? e.path ?? "", P = j(
|
|
492
|
+
b(e.name ?? "", f),
|
|
493
|
+
m,
|
|
494
|
+
o,
|
|
495
|
+
n.localizedRouteNamePrefix
|
|
496
|
+
), L = r || m !== n.defaultLocale.code, g = t.length === 1 ? this.localizeChildren(a, h, f, m, n, L) : this.localizeChildrenAllLocales(a, h, f, t, n);
|
|
523
497
|
return p(e, {
|
|
524
498
|
path: h,
|
|
525
499
|
name: P,
|
|
@@ -529,9 +503,9 @@ class re extends k {
|
|
|
529
503
|
});
|
|
530
504
|
}
|
|
531
505
|
}
|
|
532
|
-
class
|
|
506
|
+
class ce extends k {
|
|
533
507
|
generateVariants(e, t) {
|
|
534
|
-
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []),
|
|
508
|
+
const a = e.path ?? "", o = b(e.name, e.path), s = t.getAllowedLocales(a, o), i = t.getCustomPathsForPage(a, o), r = $(e.children ?? []), c = [], d = t.defaultLocale.code;
|
|
535
509
|
if (s.includes(d)) {
|
|
536
510
|
const u = i?.[d], m = u ? R(u) : a, f = u ? { [d]: u } : {}, P = this.createLocalizedChildren(
|
|
537
511
|
r,
|
|
@@ -543,7 +517,7 @@ class ne extends k {
|
|
|
543
517
|
f,
|
|
544
518
|
t
|
|
545
519
|
);
|
|
546
|
-
|
|
520
|
+
c.push(
|
|
547
521
|
p(e, {
|
|
548
522
|
path: m,
|
|
549
523
|
name: e.name,
|
|
@@ -551,12 +525,10 @@ class ne extends k {
|
|
|
551
525
|
})
|
|
552
526
|
);
|
|
553
527
|
}
|
|
554
|
-
const
|
|
555
|
-
|
|
556
|
-
);
|
|
557
|
-
if (c.length)
|
|
528
|
+
const n = t.locales.filter((u) => s.includes(u.code) && u.code !== d);
|
|
529
|
+
if (n.length)
|
|
558
530
|
if (i)
|
|
559
|
-
for (const u of
|
|
531
|
+
for (const u of n) {
|
|
560
532
|
const m = i[u.code];
|
|
561
533
|
if (m) {
|
|
562
534
|
const f = this.createLocalizedRoute(
|
|
@@ -571,7 +543,7 @@ class ne extends k {
|
|
|
571
543
|
a,
|
|
572
544
|
t
|
|
573
545
|
);
|
|
574
|
-
f &&
|
|
546
|
+
f && c.push(f);
|
|
575
547
|
} else {
|
|
576
548
|
const f = this.createLocalizedRoute(
|
|
577
549
|
e,
|
|
@@ -585,11 +557,11 @@ class ne extends k {
|
|
|
585
557
|
a,
|
|
586
558
|
t
|
|
587
559
|
);
|
|
588
|
-
f &&
|
|
560
|
+
f && c.push(f);
|
|
589
561
|
}
|
|
590
562
|
}
|
|
591
563
|
else {
|
|
592
|
-
const u =
|
|
564
|
+
const u = n.map((f) => f.code), m = this.createLocalizedRoute(
|
|
593
565
|
e,
|
|
594
566
|
u,
|
|
595
567
|
r,
|
|
@@ -601,10 +573,10 @@ class ne extends k {
|
|
|
601
573
|
a,
|
|
602
574
|
t
|
|
603
575
|
);
|
|
604
|
-
m &&
|
|
576
|
+
m && c.push(m);
|
|
605
577
|
}
|
|
606
578
|
const h = _(e, s, t.customRegex, t.localizedRouteNamePrefix);
|
|
607
|
-
return h.length &&
|
|
579
|
+
return h.length && c.push(...h), c;
|
|
608
580
|
}
|
|
609
581
|
postProcess(e, t) {
|
|
610
582
|
const a = [], o = [];
|
|
@@ -622,22 +594,13 @@ class ne extends k {
|
|
|
622
594
|
}
|
|
623
595
|
}), s;
|
|
624
596
|
}
|
|
625
|
-
createLocalizedChildren(e, t, a, o = !0, s = !1, i = !1, r = {},
|
|
597
|
+
createLocalizedChildren(e, t, a, o = !0, s = !1, i = !1, r = {}, c) {
|
|
626
598
|
return e.flatMap(
|
|
627
|
-
(d) => this.createLocalizedVariants(
|
|
628
|
-
d,
|
|
629
|
-
t,
|
|
630
|
-
a,
|
|
631
|
-
o,
|
|
632
|
-
s,
|
|
633
|
-
i,
|
|
634
|
-
r,
|
|
635
|
-
n
|
|
636
|
-
)
|
|
599
|
+
(d) => this.createLocalizedVariants(d, t, a, o, s, i, r, c)
|
|
637
600
|
);
|
|
638
601
|
}
|
|
639
|
-
createLocalizedVariants(e, t, a, o, s, i = !1, r,
|
|
640
|
-
const d = R(e.path ?? ""),
|
|
602
|
+
createLocalizedVariants(e, t, a, o, s, i = !1, r, c) {
|
|
603
|
+
const d = R(e.path ?? ""), n = z(t, e.path ?? ""), h = F(n), u = c.localizedPaths[h], m = !!u, f = [];
|
|
641
604
|
if (!m) {
|
|
642
605
|
const P = y(d), L = this.createLocalizedChildren(
|
|
643
606
|
$(e.children ?? []),
|
|
@@ -647,8 +610,8 @@ class ne extends k {
|
|
|
647
610
|
s,
|
|
648
611
|
i,
|
|
649
612
|
r,
|
|
650
|
-
|
|
651
|
-
), g = this.buildChildRouteName(e.name, i,
|
|
613
|
+
c
|
|
614
|
+
), g = this.buildChildRouteName(e.name, i, c);
|
|
652
615
|
return f.push(
|
|
653
616
|
p(e, {
|
|
654
617
|
path: P,
|
|
@@ -661,12 +624,7 @@ class ne extends k {
|
|
|
661
624
|
const L = r?.[P], g = !!L, C = u?.[P];
|
|
662
625
|
let x = R(C || (e.path ?? ""));
|
|
663
626
|
g && L && (C ? x = R(C) : x = z(L, e.path ?? ""));
|
|
664
|
-
const v =
|
|
665
|
-
P,
|
|
666
|
-
n.defaultLocale,
|
|
667
|
-
s,
|
|
668
|
-
!1
|
|
669
|
-
) ? A(P, x, n.customRegex) : x, T = y(v), W = C ? R(C) : g ? L : z(t, e.path ?? ""), V = this.createLocalizedChildren(
|
|
627
|
+
const v = H(P, c.defaultLocale, s, !1) ? A(P, x, c.customRegex) : x, T = y(v), W = C ? R(C) : g ? L : z(t, e.path ?? ""), V = this.createLocalizedChildren(
|
|
670
628
|
$(e.children ?? []),
|
|
671
629
|
W,
|
|
672
630
|
[P],
|
|
@@ -677,13 +635,13 @@ class ne extends k {
|
|
|
677
635
|
...r,
|
|
678
636
|
[P]: W
|
|
679
637
|
},
|
|
680
|
-
|
|
638
|
+
c
|
|
681
639
|
), I = this.buildLocalizedRouteName(
|
|
682
640
|
b(e.name, e.path),
|
|
683
641
|
P,
|
|
684
642
|
o,
|
|
685
643
|
!!u,
|
|
686
|
-
|
|
644
|
+
c
|
|
687
645
|
);
|
|
688
646
|
f.push(
|
|
689
647
|
p(e, {
|
|
@@ -698,7 +656,7 @@ class ne extends k {
|
|
|
698
656
|
buildChildRouteName(e, t, a) {
|
|
699
657
|
return t === !0 ? `${a.localizedRouteNamePrefix}${e}` : typeof t == "string" ? `${a.localizedRouteNamePrefix}${e}-${t}` : e;
|
|
700
658
|
}
|
|
701
|
-
createLocalizedRoute(e, t, a, o, s = "", i, r = !1,
|
|
659
|
+
createLocalizedRoute(e, t, a, o, s = "", i, r = !1, c = !1, d, n) {
|
|
702
660
|
const h = this.buildRoutePathForLocales(
|
|
703
661
|
t,
|
|
704
662
|
e.path ?? "",
|
|
@@ -706,12 +664,17 @@ class ne extends k {
|
|
|
706
664
|
o,
|
|
707
665
|
i,
|
|
708
666
|
r,
|
|
709
|
-
|
|
667
|
+
n.defaultLocale.code
|
|
710
668
|
);
|
|
711
669
|
if (!h || h === e.path || t.length === 0) return null;
|
|
712
670
|
const u = t[0];
|
|
713
671
|
if (!u) return null;
|
|
714
|
-
const m = d ?? e.path ?? "", f = j(
|
|
672
|
+
const m = d ?? e.path ?? "", f = j(
|
|
673
|
+
b(e.name ?? "", m),
|
|
674
|
+
u,
|
|
675
|
+
o,
|
|
676
|
+
n.localizedRouteNamePrefix
|
|
677
|
+
);
|
|
715
678
|
return p(e, {
|
|
716
679
|
path: h,
|
|
717
680
|
name: f,
|
|
@@ -721,19 +684,19 @@ class ne extends k {
|
|
|
721
684
|
t,
|
|
722
685
|
!0,
|
|
723
686
|
!1,
|
|
724
|
-
|
|
687
|
+
c,
|
|
725
688
|
{ [u]: s },
|
|
726
|
-
|
|
689
|
+
n
|
|
727
690
|
),
|
|
728
691
|
alias: [],
|
|
729
692
|
meta: { alias: [] }
|
|
730
693
|
});
|
|
731
694
|
}
|
|
732
695
|
buildLocalizedRouteName(e, t, a, o = !1, s) {
|
|
733
|
-
return a ? o ? `${s.localizedRouteNamePrefix}${e}-${t}` : t && !
|
|
696
|
+
return a ? o ? `${s.localizedRouteNamePrefix}${e}-${t}` : t && !J(t, s.defaultLocale, !1) ? `${s.localizedRouteNamePrefix}${e}-${t}` : `${s.localizedRouteNamePrefix}${e}` : e;
|
|
734
697
|
}
|
|
735
698
|
}
|
|
736
|
-
function
|
|
699
|
+
function ne(l) {
|
|
737
700
|
switch (l) {
|
|
738
701
|
case "no_prefix":
|
|
739
702
|
return new oe();
|
|
@@ -742,7 +705,7 @@ function ce(l) {
|
|
|
742
705
|
case "prefix_and_default":
|
|
743
706
|
return new re();
|
|
744
707
|
case "prefix_except_default":
|
|
745
|
-
return new
|
|
708
|
+
return new ce();
|
|
746
709
|
default:
|
|
747
710
|
throw new Error(`Unknown route strategy: ${l}`);
|
|
748
711
|
}
|
|
@@ -757,13 +720,13 @@ class de {
|
|
|
757
720
|
globalLocaleRoutes: s,
|
|
758
721
|
filesLocaleRoutes: i = {},
|
|
759
722
|
routeLocales: r = {},
|
|
760
|
-
noPrefixRedirect:
|
|
723
|
+
noPrefixRedirect: c,
|
|
761
724
|
excludePatterns: d,
|
|
762
|
-
localizedRouteNamePrefix:
|
|
725
|
+
localizedRouteNamePrefix: n = "localized-",
|
|
763
726
|
customRegexMatcher: h,
|
|
764
727
|
fallbackRedirectComponentPath: u
|
|
765
|
-
} = e, m =
|
|
766
|
-
this.locales = m.locales, this.defaultLocale = m.defaultLocale, this.strategy = o, this.noPrefixRedirect =
|
|
728
|
+
} = e, m = X(t, a);
|
|
729
|
+
this.locales = m.locales, this.defaultLocale = m.defaultLocale, this.strategy = o, this.noPrefixRedirect = c, this.excludePatterns = d, this.localizedRouteNamePrefix = n, this.customRegex = h, this.fallbackRedirectComponentPath = u, this.activeLocaleCodes = this.computeActiveLocaleCodes();
|
|
767
730
|
const f = {};
|
|
768
731
|
for (const P in s) {
|
|
769
732
|
const L = w(P), g = s[P];
|
|
@@ -799,19 +762,14 @@ class de {
|
|
|
799
762
|
rawGlobalLocaleRoutes: this.rawGlobalLocaleRoutes
|
|
800
763
|
});
|
|
801
764
|
this.localizedPaths = t.localizedPaths;
|
|
802
|
-
const a =
|
|
765
|
+
const a = ne(this.strategy), o = [...e], s = [];
|
|
803
766
|
for (const r of o)
|
|
804
767
|
s.push(...a.processPage(r, t));
|
|
805
768
|
const i = a.postProcess(s, t);
|
|
806
769
|
e.length = 0, e.push(...i);
|
|
807
770
|
}
|
|
808
771
|
extractLocalizedPaths(e, t = "") {
|
|
809
|
-
return S(
|
|
810
|
-
e,
|
|
811
|
-
this.globalLocaleRoutes,
|
|
812
|
-
this.filesLocaleRoutes,
|
|
813
|
-
t
|
|
814
|
-
);
|
|
772
|
+
return S(e, this.globalLocaleRoutes, this.filesLocaleRoutes, t);
|
|
815
773
|
}
|
|
816
774
|
/**
|
|
817
775
|
* Creates global and per-page translation JSON files for the current resolved locales.
|
|
@@ -820,8 +778,8 @@ class de {
|
|
|
820
778
|
this.locales.forEach((s) => {
|
|
821
779
|
const i = N.join(a, t, `${s.code}.json`);
|
|
822
780
|
this.ensureFileExists(i), o || e.forEach((r) => {
|
|
823
|
-
const
|
|
824
|
-
this.ensureFileExists(
|
|
781
|
+
const c = N.join(a, t, "pages", `${r}/${s.code}.json`);
|
|
782
|
+
this.ensureFileExists(c);
|
|
825
783
|
});
|
|
826
784
|
});
|
|
827
785
|
}
|
|
@@ -839,9 +797,9 @@ class de {
|
|
|
839
797
|
let s;
|
|
840
798
|
const i = this.globalLocaleRoutes[o], r = this.globalLocaleRoutes[a];
|
|
841
799
|
i && typeof i == "object" && !Array.isArray(i) ? s = i[t] : r && typeof r == "object" && !Array.isArray(r) && (s = r[t]);
|
|
842
|
-
const
|
|
843
|
-
let
|
|
844
|
-
if (this.strategy === "no_prefix" ?
|
|
800
|
+
const c = !!s, d = s ? R(s) : R(a) || "";
|
|
801
|
+
let n = !1;
|
|
802
|
+
if (this.strategy === "no_prefix" ? n = !1 : this.strategy === "prefix" || this.strategy === "prefix_and_default" ? n = !0 : this.strategy === "prefix_except_default" && (n = t !== this.defaultLocale.code), t === this.defaultLocale.code && !c && (this.strategy === "prefix_except_default" || this.strategy === "no_prefix") && (n = !1), !n)
|
|
845
803
|
return R(d) || "/";
|
|
846
804
|
const h = y(d);
|
|
847
805
|
return h ? R(`/${t}/${h}`) : `/${t}`;
|
|
@@ -861,15 +819,15 @@ class de {
|
|
|
861
819
|
export {
|
|
862
820
|
de as RouteGenerator,
|
|
863
821
|
A as buildFullPath,
|
|
864
|
-
|
|
822
|
+
Z as buildFullPathNoPrefix,
|
|
865
823
|
j as buildRouteName,
|
|
866
824
|
$ as cloneArray,
|
|
867
825
|
S as extractLocalizedPaths,
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
826
|
+
K as isInternalPath,
|
|
827
|
+
J as isLocaleDefault,
|
|
828
|
+
q as isPageRedirectOnly,
|
|
871
829
|
R as normalizePath,
|
|
872
830
|
w as normalizeRouteKey,
|
|
873
831
|
y as removeLeadingSlash,
|
|
874
|
-
|
|
832
|
+
H as shouldAddLocalePrefix
|
|
875
833
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { NuxtPage } from '@nuxt/schema';
|
|
2
1
|
import { GlobalLocaleRoutes, Locale, Strategies } from '@i18n-micro/types';
|
|
3
|
-
import {
|
|
2
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
4
3
|
import { LocalizedPathsMap } from './core/localized-paths';
|
|
4
|
+
import { LocaleRoutesConfig } from './strategies/types';
|
|
5
5
|
export interface RouteGeneratorOptions {
|
|
6
6
|
locales: Locale[];
|
|
7
7
|
defaultLocaleCode: string;
|
package/dist/utils/common.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { NuxtPage } from '@nuxt/schema';
|
|
2
1
|
import { Locale } from '@i18n-micro/types';
|
|
2
|
+
import { NuxtPage } from '@nuxt/schema';
|
|
3
3
|
export declare const cloneArray: <T extends object>(array: T[]) => T[];
|
|
4
4
|
export declare const isPageRedirectOnly: (page: NuxtPage) => boolean;
|
|
5
5
|
export declare const buildRouteName: (baseName: string, localeCode: string, isCustom: boolean, prefix?: string) => string;
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { buildRouteName, buildRouteNameFromRoute, cloneArray, isInternalPath, isLocaleDefault, isPageRedirectOnly, shouldAddLocalePrefix, } from './common';
|
|
2
|
+
export { type ResolvedLocales, resolveLocales } from './locales';
|
|
3
|
+
export { buildFullPath, buildFullPathNoPrefix, joinPath, normalizePath, normalizeRouteKey, removeLeadingSlash, } from './path';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@i18n-micro/route-strategy",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
4
4
|
"description": "Locale-aware route generator for Nuxt i18n micro",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.cjs",
|
|
@@ -55,8 +55,8 @@
|
|
|
55
55
|
"sideEffects": false,
|
|
56
56
|
"dependencies": {
|
|
57
57
|
"@nuxt/schema": "^3.18.1 || ^4.0.0",
|
|
58
|
-
"@i18n-micro/types": "1.1.
|
|
59
|
-
"@i18n-micro/core": "1.1.
|
|
58
|
+
"@i18n-micro/types": "1.1.1",
|
|
59
|
+
"@i18n-micro/core": "1.1.2"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
62
|
"@types/jest": "^29.5.14",
|