@i18n-micro/route-strategy 1.1.28 → 1.1.30
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/builder.d.ts +11 -0
- package/dist/index.cjs +5 -1
- package/dist/index.mjs +199 -157
- package/dist/strategies/abstract.d.ts +17 -0
- package/dist/strategies/no-prefix.d.ts +4 -4
- package/package.json +4 -4
- package/dist/core/alias.d.ts +0 -6
package/dist/core/builder.d.ts
CHANGED
|
@@ -5,6 +5,17 @@ import { NuxtPage } from '@nuxt/schema';
|
|
|
5
5
|
* Otherwise, performs join(parentPath, childSegment) with normalization.
|
|
6
6
|
*/
|
|
7
7
|
export declare function resolveChildPath(parentPath: string, childSegment: string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Route path segment of a child from its custom value. Child values are relative to the
|
|
10
|
+
* parent (`book-activity/skiing` under `/activity`); a value that already starts with the
|
|
11
|
+
* parent's localized path is a full URL and is taken from there (`/bereich/kategorie` under
|
|
12
|
+
* `/bereich` is `kategorie`, `/bereich` itself is the parent's own path) — the same rule the
|
|
13
|
+
* runtime applies (joinNestedCustomPath). A full URL that leaves the parent's path below a
|
|
14
|
+
* shared ancestor cannot be nested; what follows the shared part is kept instead of
|
|
15
|
+
* doubling it (`/bereich/artikel` under `/bereich/kategorie` is `artikel`). Only absolute
|
|
16
|
+
* values can be full URLs: a relative value (`bereich/artikel`) is always kept as written.
|
|
17
|
+
*/
|
|
18
|
+
export declare function childSegment(parentLocalizedPath: string, customValue: string): string;
|
|
8
19
|
export interface CreateRouteOptions {
|
|
9
20
|
path: string;
|
|
10
21
|
name?: string;
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1,5 @@
|
|
|
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;
|
|
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),r=this.localizedPaths[n]??this.localizedPaths[t];if(!r)return;let i={};for(let e in r){let t=r[e];i[e]=t.startsWith(`/`)?t:`/${t}`}return i}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))}var L=e=>e.replace(/^\/?:locale\([^)]*\)/,``)||`/`;function R(e,t){if(!t.startsWith(`/`))return b(t).replace(/^\/+/,``);let n=b(`/${t.replace(/^\/+/,``)}`),r=b(`/${L(e).replace(/^\/+/,``)}`);if(r===`/`||r===``)return n.replace(/^\/+/,``);if(n===r)return``;if(n.startsWith(`${r}/`))return n.slice(r.length+1);let i=n.split(`/`).filter(Boolean),a=r.split(`/`).filter(Boolean),o=0;for(;o<i.length&&o<a.length&&i[o]===a[o];)o++;return i.slice(o).join(`/`)}function z(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}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,a=!0){let o=I(n,e.path??``),s=h(e.name,e.path);if(M(i.globalLocaleRoutes,o,s))return[e];let c=I(t,e.path??``),l=i.localizedPaths[A(o)];if(l&&a){let a=r.filter(e=>!l[e]),o=r.filter(e=>l[e]);return[...a.length?this.localizeChildAllLocales({...e},t,n,a,i,!1):[],...o.flatMap(r=>this.localizeChild(e,t,n,r,i,!0))]}let u=x(b(e.path??``)),d=this.localizeChildrenAllLocales(f(e.children??[]),c,o,r,i);return[z(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=c?R(t,c):x(b(e.path??``)),u=I(t,l),d=this.localizeChildren(f(e.children??[]),u,o,r,i,a),p=h(e.name,e.path);return[z(e,{path:l,name:`${i.localizedRouteNamePrefix}${p}-${r}`,children:d})]}declaredAliases(e){let t=e.alias??e.meta?.alias;return typeof t==`string`?[t]:Array.isArray(t)?t.filter(e=>typeof e==`string`):[]}routeAliases(e,t,n,r){let i=new Set(E(t));for(let t of this.declaredAliases(e)){let e=n?D(n,t,r.customRegex):b(t);i.add(e);for(let t of E(e))i.add(t)}return[...i]}localizeChildPathsKeepNames(e,t,n,r,i){return e.map(e=>{let a=I(n,e.path??``);if(M(i.globalLocaleRoutes,a,h(e.name,e.path)))return e;let o=i.getCustomPath(a,r),s=o?R(t,o):e.path??``,c=e.children?.length?this.localizeChildPathsKeepNames(e.children,I(t,s),a,r,i):e.children;return o||c!==e.children?z(e,{path:s,children:c}):e})}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=[],c=this.routeAliases(e,b(n),null,t);c.length&&c.join(`
|
|
2
|
+
`)!==this.declaredAliases(e).join(`
|
|
3
|
+
`)&&(e.alias=c,e.meta={...e.meta,alias:c}),s.push(e);let l=new Set;if(a)for(let c of i){let i=a[c];if(!i)continue;let u=b(i),d=b(n);if(u===d)continue;l.add(c);let f=O(i),p=m(r,c,!0),h=this.localizeChildrenForNoPrefix(o,u,n,c,t);s.push(z(e,{path:f,name:p,children:h,alias:[],meta:{alias:[]}})),t.noPrefixRedirect&&c===t.defaultLocale.code&&u!==d&&(e.redirect=u)}for(let a of i)l.has(a)||this.hasChangedChildPath(o,n,a,t)&&s.push(z(e,{path:O(n),name:m(r,a,!0),children:this.localizeChildrenForNoPrefix(o,b(n),n,a,t),alias:[],meta:{alias:[]}}));return s}hasChangedChildPath(e,t,n,r){return e.some(e=>{let i=I(t,e.path??``),a=r.getCustomPath(i,n);return a&&R(t,a)!==x(b(e.path??``))?!0:this.hasChangedChildPath(e.children??[],i,n,r)})}localizeChildrenForNoPrefix(e,t,n,r,i){return e.map(e=>{let a=I(n,e.path??``),o=i.getCustomPath(a,r),s=o?R(t,o):x(b(e.path??``)),c=I(t,s),l=this.localizeChildrenForNoPrefix(f(e.children??[]),c,a,r,i);return z(e,{path:s,name:m(h(e.name,e.path),r,!0),children:l})})}},H=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){let c=i.filter(e=>!a[e]);if(c.length){let i=D(c,n,t.customRegex),a=this.routeAliases(e,i,c,t);s.push(z(e,{path:i,name:m(r,c[0],!1,t.localizedRouteNamePrefix),children:this.localizeChildrenAllLocales(o,n,n,c,t),alias:a.length?a:void 0,meta:{alias:a.length?a:[]}}))}for(let c of i){let i=a[c];if(!i)continue;let l=D(c,b(i),t.customRegex),u=m(r,c,!!i,t.localizedRouteNamePrefix),d=i??n,f=this.localizeChildren(o,d,n,c,t,!0),p=this.routeAliases(e,l,[c],t);s.push(z(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),u=this.routeAliases(e,a,i,t);s.push(z(e,{path:a,name:c,children:l,alias:u,meta:{alias:u}}))}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}`,redirect:o})}return t.fallbackRedirectComponentPath&&n.push({path:`/:pathMatch(.*)*`,name:`custom-fallback-route`,file:t.fallbackRedirectComponentPath,meta:{globalLocaleRoutes:t.rawGlobalLocaleRoutes??t.globalLocaleRoutes}}),n}},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=[],c=t.defaultLocale.code;if(i.includes(c)){let r=a?.[c],i=r?b(r):n,l=o.length?this.localizeChildPathsKeepNames(o,i,n,c,t):void 0,u=this.routeAliases(e,i,null,t),d=u.length?{alias:u,meta:{alias:u}}:{},f=u.join(`
|
|
4
|
+
`)!==this.declaredAliases(e).join(`
|
|
5
|
+
`);s.push(r||l||f?z(e,{path:i,children:l,...d}):e)}else 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,!0,r.code,n,t);a&&s.push(a)}else{let a=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!1,r.code,n,t);a&&s.push(a)}}}let r=l.filter(e=>!a[e.code]).map(e=>e.code);if(r.length){let i=this.createLocalizedRoute(e,r,o,!1,``,t.customRegex,!1,r.length!==1||r[0],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)}}return 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,_=t.length===1?this.localizeChildren(n,u,f,d,l,g):this.localizeChildrenAllLocales(n,u,f,t,l),v=this.routeAliases(e,u,t,l);return z(e,{path:u,name:p,children:_,alias:v,meta:{alias:v}})}},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;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),d=this.routeAliases(e,i,null,t);s.push(z(e,{path:i,name:e.name,children:u,...d.length?{alias:d,meta:{alias:d}}:{}}))}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)continue;let c=this.createLocalizedRoute(e,[r.code],o,!0,i,t.customRegex,!1,r.code,n,t);c&&s.push(c)}let r=l.filter(e=>!a[e.code]).map(e=>e.code);if(r.length){let i=this.createLocalizedRoute(e,r,o,!1,``,t.customRegex,!1,r.length!==1||r[0],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)}}return 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={};for(let e in o)u[e]=I(o[e],l);let d=this.createLocalizedChildren(f(e.children??[]),I(t,e.path??``),n,r,i,a,u,s),m=this.buildChildRouteName(e.name,a,s);return p.push(z(e,{path:l,name:m,children:d})),p}for(let a of n){let n=o?.[a]||t,c=u?.[a],l=c?R(n,c):b(e.path??``),d=x(g(a,s.defaultLocale,i)?D(a,l,s.customRegex):l),m=I(n,x(l)),_=this.createLocalizedChildren(f(e.children??[]),I(t,e.path??``),[a],r,i,a,{...o,[a]:m},s),v=this.buildLocalizedRouteName(h(e.name,e.path),a,r,!!u,s);p.push(z(e,{path:d,name:v,children:_}))}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??``,p=m(h(e.name??``,f),d,r,l.localizedRouteNamePrefix),g=this.routeAliases(e,u,t,l);return z(e,{path:u,name:p,children:this.createLocalizedChildren(n,f,t,!0,!1,s,{[d]:i},l),alias:g,meta:{alias:g}})}buildLocalizedRouteName(e,t,n,r=!1,i){return n?r||t&&!_(t,i.defaultLocale)?`${i.localizedRouteNamePrefix}${e}-${t}`:`${i.localizedRouteNamePrefix}${e}`:e}};function G(e){switch(e){case`no_prefix`:return new V;case`prefix`:return new H;case`prefix_and_default`:return new U;case`prefix_except_default`:return new W;default:throw Error(`Unknown route strategy: ${e}`)}}var K=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=G(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=K,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;
|
package/dist/index.mjs
CHANGED
|
@@ -119,8 +119,14 @@ var A = class {
|
|
|
119
119
|
return this.localizedPaths[n]?.[t];
|
|
120
120
|
}
|
|
121
121
|
getCustomPathsForPage(e, t) {
|
|
122
|
-
let n = T(e);
|
|
123
|
-
|
|
122
|
+
let n = T(e), r = this.localizedPaths[n] ?? this.localizedPaths[t];
|
|
123
|
+
if (!r) return;
|
|
124
|
+
let i = {};
|
|
125
|
+
for (let e in r) {
|
|
126
|
+
let t = r[e];
|
|
127
|
+
i[e] = t.startsWith("/") ? t : `/${t}`;
|
|
128
|
+
}
|
|
129
|
+
return i;
|
|
124
130
|
}
|
|
125
131
|
hasLocaleRestrictions(e, t) {
|
|
126
132
|
return !!(this.routeLocales[e] ?? this.routeLocales[t]);
|
|
@@ -136,7 +142,18 @@ function j(e, n) {
|
|
|
136
142
|
let r = (n ?? "").trim();
|
|
137
143
|
return r.startsWith("/") ? h(r) : h(t.posix.join(e || "", r));
|
|
138
144
|
}
|
|
139
|
-
|
|
145
|
+
var M = (e) => e.replace(/^\/?:locale\([^)]*\)/, "") || "/";
|
|
146
|
+
function N(e, t) {
|
|
147
|
+
if (!t.startsWith("/")) return h(t).replace(/^\/+/, "");
|
|
148
|
+
let n = h(`/${t.replace(/^\/+/, "")}`), r = h(`/${M(e).replace(/^\/+/, "")}`);
|
|
149
|
+
if (r === "/" || r === "") return n.replace(/^\/+/, "");
|
|
150
|
+
if (n === r) return "";
|
|
151
|
+
if (n.startsWith(`${r}/`)) return n.slice(r.length + 1);
|
|
152
|
+
let i = n.split("/").filter(Boolean), a = r.split("/").filter(Boolean), o = 0;
|
|
153
|
+
for (; o < i.length && o < a.length && i[o] === a[o];) o++;
|
|
154
|
+
return i.slice(o).join("/");
|
|
155
|
+
}
|
|
156
|
+
function P(e, t) {
|
|
140
157
|
let { path: n, name: r, children: i, meta: a, alias: o } = t, s = {
|
|
141
158
|
...e,
|
|
142
159
|
path: n
|
|
@@ -153,28 +170,6 @@ function M(e, t) {
|
|
|
153
170
|
}), s;
|
|
154
171
|
}
|
|
155
172
|
//#endregion
|
|
156
|
-
//#region src/core/alias.ts
|
|
157
|
-
function N(e) {
|
|
158
|
-
let t = e.alias, n = e.meta?.alias, r = t ?? n;
|
|
159
|
-
return Array.isArray(r) ? r : [];
|
|
160
|
-
}
|
|
161
|
-
function P(e, t, n, r = "localized-") {
|
|
162
|
-
let i = N(e);
|
|
163
|
-
if (!i.length) return [];
|
|
164
|
-
let a = [];
|
|
165
|
-
for (let o of i) {
|
|
166
|
-
let i = S(t, o, n);
|
|
167
|
-
a.push(M(e, {
|
|
168
|
-
path: i,
|
|
169
|
-
name: `${r}${e.name ?? ""}`,
|
|
170
|
-
alias: void 0,
|
|
171
|
-
meta: { alias: void 0 },
|
|
172
|
-
children: e.children
|
|
173
|
-
}));
|
|
174
|
-
}
|
|
175
|
-
return a;
|
|
176
|
-
}
|
|
177
|
-
//#endregion
|
|
178
173
|
//#region src/strategies/abstract.ts
|
|
179
174
|
var F = class {
|
|
180
175
|
processPage(t, n) {
|
|
@@ -192,16 +187,16 @@ var F = class {
|
|
|
192
187
|
localizeChildrenAllLocales(e, t, n, r, i) {
|
|
193
188
|
return e.flatMap((e) => this.localizeChildAllLocales(e, t, n, r, i));
|
|
194
189
|
}
|
|
195
|
-
localizeChildAllLocales(e, t, n, r, i) {
|
|
196
|
-
let
|
|
197
|
-
if (D(i.globalLocaleRoutes,
|
|
198
|
-
let
|
|
199
|
-
if (d
|
|
200
|
-
let a = d[
|
|
201
|
-
return this.
|
|
202
|
-
}
|
|
203
|
-
let f = g(h(e.path ?? "")), p = this.localizeChildrenAllLocales(s(e.children ?? []),
|
|
204
|
-
return [
|
|
190
|
+
localizeChildAllLocales(e, t, n, r, i, a = !0) {
|
|
191
|
+
let o = j(n, e.path ?? ""), c = u(e.name, e.path);
|
|
192
|
+
if (D(i.globalLocaleRoutes, o, c)) return [e];
|
|
193
|
+
let l = j(t, e.path ?? ""), d = i.localizedPaths[T(o)];
|
|
194
|
+
if (d && a) {
|
|
195
|
+
let a = r.filter((e) => !d[e]), o = r.filter((e) => d[e]);
|
|
196
|
+
return [...a.length ? this.localizeChildAllLocales({ ...e }, t, n, a, i, !1) : [], ...o.flatMap((r) => this.localizeChild(e, t, n, r, i, !0))];
|
|
197
|
+
}
|
|
198
|
+
let f = g(h(e.path ?? "")), p = this.localizeChildrenAllLocales(s(e.children ?? []), l, o, r, i);
|
|
199
|
+
return [P(e, {
|
|
205
200
|
path: f,
|
|
206
201
|
name: `${i.localizedRouteNamePrefix}${e.name ?? ""}`,
|
|
207
202
|
children: p
|
|
@@ -210,13 +205,37 @@ var F = class {
|
|
|
210
205
|
localizeChild(e, t, n, r, i, a) {
|
|
211
206
|
let o = j(n, e.path ?? ""), c = u(e.name, e.path);
|
|
212
207
|
if (D(i.globalLocaleRoutes, o, c)) return [e];
|
|
213
|
-
let l = i.getCustomPath(o, r), d =
|
|
214
|
-
return [
|
|
208
|
+
let l = i.getCustomPath(o, r), d = l ? N(t, l) : g(h(e.path ?? "")), f = j(t, d), p = this.localizeChildren(s(e.children ?? []), f, o, r, i, a), m = u(e.name, e.path);
|
|
209
|
+
return [P(e, {
|
|
215
210
|
path: d,
|
|
216
211
|
name: `${i.localizedRouteNamePrefix}${m}-${r}`,
|
|
217
212
|
children: p
|
|
218
213
|
})];
|
|
219
214
|
}
|
|
215
|
+
declaredAliases(e) {
|
|
216
|
+
let t = e.alias ?? e.meta?.alias;
|
|
217
|
+
return typeof t == "string" ? [t] : Array.isArray(t) ? t.filter((e) => typeof e == "string") : [];
|
|
218
|
+
}
|
|
219
|
+
routeAliases(e, t, n, r) {
|
|
220
|
+
let i = new Set(x(t));
|
|
221
|
+
for (let t of this.declaredAliases(e)) {
|
|
222
|
+
let e = n ? S(n, t, r.customRegex) : h(t);
|
|
223
|
+
i.add(e);
|
|
224
|
+
for (let t of x(e)) i.add(t);
|
|
225
|
+
}
|
|
226
|
+
return [...i];
|
|
227
|
+
}
|
|
228
|
+
localizeChildPathsKeepNames(e, t, n, r, i) {
|
|
229
|
+
return e.map((e) => {
|
|
230
|
+
let a = j(n, e.path ?? "");
|
|
231
|
+
if (D(i.globalLocaleRoutes, a, u(e.name, e.path))) return e;
|
|
232
|
+
let o = i.getCustomPath(a, r), s = o ? N(t, o) : e.path ?? "", c = e.children?.length ? this.localizeChildPathsKeepNames(e.children, j(t, s), a, r, i) : e.children;
|
|
233
|
+
return o || c !== e.children ? P(e, {
|
|
234
|
+
path: s,
|
|
235
|
+
children: c
|
|
236
|
+
}) : e;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
220
239
|
buildRoutePathForLocales(e, t, n, r, i, a, o) {
|
|
221
240
|
if (r) {
|
|
222
241
|
let t = e.includes(o);
|
|
@@ -226,79 +245,87 @@ var F = class {
|
|
|
226
245
|
}
|
|
227
246
|
}, I = class extends F {
|
|
228
247
|
generateVariants(e, t) {
|
|
229
|
-
let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [];
|
|
230
|
-
|
|
248
|
+
let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], d = this.routeAliases(e, h(n), null, t);
|
|
249
|
+
d.length && d.join("\n") !== this.declaredAliases(e).join("\n") && (e.alias = d, e.meta = {
|
|
250
|
+
...e.meta,
|
|
251
|
+
alias: d
|
|
252
|
+
}), c.push(e);
|
|
253
|
+
let f = /* @__PURE__ */ new Set();
|
|
254
|
+
if (a) for (let s of i) {
|
|
231
255
|
let i = a[s];
|
|
232
256
|
if (!i) continue;
|
|
233
257
|
let u = h(i), d = h(n);
|
|
234
258
|
if (u === d) continue;
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
259
|
+
f.add(s);
|
|
260
|
+
let p = C(i), m = l(r, s, !0), g = this.localizeChildrenForNoPrefix(o, u, n, s, t);
|
|
261
|
+
c.push(P(e, {
|
|
262
|
+
path: p,
|
|
263
|
+
name: m,
|
|
264
|
+
children: g,
|
|
240
265
|
alias: [],
|
|
241
266
|
meta: { alias: [] }
|
|
242
267
|
})), t.noPrefixRedirect && s === t.defaultLocale.code && u !== d && (e.redirect = u);
|
|
243
268
|
}
|
|
244
|
-
|
|
269
|
+
for (let a of i) f.has(a) || this.hasChangedChildPath(o, n, a, t) && c.push(P(e, {
|
|
270
|
+
path: C(n),
|
|
271
|
+
name: l(r, a, !0),
|
|
272
|
+
children: this.localizeChildrenForNoPrefix(o, h(n), n, a, t),
|
|
273
|
+
alias: [],
|
|
274
|
+
meta: { alias: [] }
|
|
275
|
+
}));
|
|
276
|
+
return c;
|
|
245
277
|
}
|
|
246
|
-
|
|
278
|
+
hasChangedChildPath(e, t, n, r) {
|
|
279
|
+
return e.some((e) => {
|
|
280
|
+
let i = j(t, e.path ?? ""), a = r.getCustomPath(i, n);
|
|
281
|
+
return a && N(t, a) !== g(h(e.path ?? "")) ? !0 : this.hasChangedChildPath(e.children ?? [], i, n, r);
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
localizeChildrenForNoPrefix(e, t, n, r, i) {
|
|
247
285
|
return e.map((e) => {
|
|
248
|
-
let
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
d = g(e), f = e;
|
|
254
|
-
} else d = g(h(e.path ?? "")), f = j(t, e.path ?? "");
|
|
255
|
-
} else d = g(h(e.path ?? "")), f = j(t, e.path ?? "");
|
|
256
|
-
let p = this.localizeChildrenForNoPrefix(s(e.children ?? []), f, c, r, i, a, o + 1), m = l(u(e.name, e.path), i, !0);
|
|
257
|
-
return M(e, {
|
|
258
|
-
path: d,
|
|
259
|
-
name: m,
|
|
260
|
-
children: p
|
|
286
|
+
let a = j(n, e.path ?? ""), o = i.getCustomPath(a, r), c = o ? N(t, o) : g(h(e.path ?? "")), d = j(t, c), f = this.localizeChildrenForNoPrefix(s(e.children ?? []), d, a, r, i);
|
|
287
|
+
return P(e, {
|
|
288
|
+
path: c,
|
|
289
|
+
name: l(u(e.name, e.path), r, !0),
|
|
290
|
+
children: f
|
|
261
291
|
});
|
|
262
292
|
});
|
|
263
293
|
}
|
|
264
|
-
}
|
|
265
|
-
//#endregion
|
|
266
|
-
//#region src/strategies/prefix.ts
|
|
267
|
-
function L(e) {
|
|
268
|
-
let t = e.alias ?? e.meta?.alias;
|
|
269
|
-
return Array.isArray(t) ? t : [];
|
|
270
|
-
}
|
|
271
|
-
var R = class extends F {
|
|
294
|
+
}, L = class extends F {
|
|
272
295
|
generateVariants(e, t) {
|
|
273
296
|
let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [];
|
|
274
|
-
if (a)
|
|
275
|
-
let
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
297
|
+
if (a) {
|
|
298
|
+
let s = i.filter((e) => !a[e]);
|
|
299
|
+
if (s.length) {
|
|
300
|
+
let i = S(s, n, t.customRegex), a = this.routeAliases(e, i, s, t);
|
|
301
|
+
c.push(P(e, {
|
|
302
|
+
path: i,
|
|
303
|
+
name: l(r, s[0], !1, t.localizedRouteNamePrefix),
|
|
304
|
+
children: this.localizeChildrenAllLocales(o, n, n, s, t),
|
|
305
|
+
alias: a.length ? a : void 0,
|
|
306
|
+
meta: { alias: a.length ? a : [] }
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
for (let s of i) {
|
|
310
|
+
let i = a[s];
|
|
311
|
+
if (!i) continue;
|
|
312
|
+
let u = S(s, h(i), t.customRegex), d = l(r, s, !!i, t.localizedRouteNamePrefix), f = i ?? n, p = this.localizeChildren(o, f, n, s, t, !0), m = this.routeAliases(e, u, [s], t);
|
|
313
|
+
c.push(P(e, {
|
|
314
|
+
path: u,
|
|
315
|
+
name: d,
|
|
316
|
+
children: p,
|
|
317
|
+
alias: m.length ? m : void 0,
|
|
318
|
+
meta: { alias: m.length ? m : [] }
|
|
319
|
+
}));
|
|
320
|
+
}
|
|
321
|
+
} else {
|
|
322
|
+
let a = S(i, n, t.customRegex), s = l(r, i[0], !1, t.localizedRouteNamePrefix), u = this.localizeChildrenAllLocales(o, n, n, i, t), d = this.routeAliases(e, a, i, t);
|
|
323
|
+
c.push(P(e, {
|
|
287
324
|
path: a,
|
|
288
325
|
name: s,
|
|
289
326
|
children: u,
|
|
290
|
-
alias:
|
|
291
|
-
meta: { alias:
|
|
292
|
-
}));
|
|
293
|
-
}
|
|
294
|
-
let d = L(e);
|
|
295
|
-
if (d.length) for (let n of d) {
|
|
296
|
-
let r = S(i, n, t.customRegex);
|
|
297
|
-
c.push(M(e, {
|
|
298
|
-
path: r,
|
|
299
|
-
name: `${t.localizedRouteNamePrefix}${e.name ?? ""}`,
|
|
300
|
-
alias: void 0,
|
|
301
|
-
meta: { alias: void 0 }
|
|
327
|
+
alias: d,
|
|
328
|
+
meta: { alias: d }
|
|
302
329
|
}));
|
|
303
330
|
}
|
|
304
331
|
return c;
|
|
@@ -316,7 +343,7 @@ var R = class extends F {
|
|
|
316
343
|
let a = e.startsWith("/") ? e : `/${e}`, o = `/${i}${n.startsWith("/") ? n : `/${n}`}`;
|
|
317
344
|
a !== o && r.push({
|
|
318
345
|
path: a,
|
|
319
|
-
name: `redirect-${e
|
|
346
|
+
name: `redirect-${e}`,
|
|
320
347
|
redirect: o
|
|
321
348
|
});
|
|
322
349
|
}
|
|
@@ -327,36 +354,46 @@ var R = class extends F {
|
|
|
327
354
|
meta: { globalLocaleRoutes: n.rawGlobalLocaleRoutes ?? n.globalLocaleRoutes }
|
|
328
355
|
}), r;
|
|
329
356
|
}
|
|
330
|
-
},
|
|
357
|
+
}, R = class extends F {
|
|
331
358
|
generateVariants(e, t) {
|
|
332
359
|
let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], l = t.defaultLocale.code;
|
|
333
|
-
|
|
360
|
+
if (i.includes(l)) {
|
|
361
|
+
let r = a?.[l], i = r ? h(r) : n, s = o.length ? this.localizeChildPathsKeepNames(o, i, n, l, t) : void 0, u = this.routeAliases(e, i, null, t), d = u.length ? {
|
|
362
|
+
alias: u,
|
|
363
|
+
meta: { alias: u }
|
|
364
|
+
} : {}, f = u.join("\n") !== this.declaredAliases(e).join("\n");
|
|
365
|
+
c.push(r || s || f ? P(e, {
|
|
366
|
+
path: i,
|
|
367
|
+
children: s,
|
|
368
|
+
...d
|
|
369
|
+
}) : e);
|
|
370
|
+
} else c.push(e);
|
|
334
371
|
let d = t.locales.filter((e) => i.includes(e.code));
|
|
335
372
|
if (d.length > 0) {
|
|
336
|
-
if (a)
|
|
337
|
-
let
|
|
338
|
-
|
|
339
|
-
if (
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
373
|
+
if (a) {
|
|
374
|
+
for (let r of d) {
|
|
375
|
+
let i = a[r.code];
|
|
376
|
+
if (i) {
|
|
377
|
+
if (r.code === l) {
|
|
378
|
+
let a = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !0, r.code, n, t);
|
|
379
|
+
a && c.push(a);
|
|
380
|
+
} else {
|
|
381
|
+
let a = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !1, r.code, n, t);
|
|
382
|
+
a && c.push(a);
|
|
383
|
+
}
|
|
347
384
|
}
|
|
348
|
-
}
|
|
349
|
-
|
|
385
|
+
}
|
|
386
|
+
let r = d.filter((e) => !a[e.code]).map((e) => e.code);
|
|
387
|
+
if (r.length) {
|
|
388
|
+
let i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, r.length !== 1 || r[0], n, t);
|
|
350
389
|
i && c.push(i);
|
|
351
390
|
}
|
|
352
|
-
}
|
|
353
|
-
else {
|
|
391
|
+
} else {
|
|
354
392
|
let r = d.map((e) => e.code), i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, !0, n, t);
|
|
355
393
|
i && c.push(i);
|
|
356
394
|
}
|
|
357
395
|
}
|
|
358
|
-
|
|
359
|
-
return f.length && c.push(...f), c;
|
|
396
|
+
return c;
|
|
360
397
|
}
|
|
361
398
|
postProcess(e, t) {
|
|
362
399
|
let n = [], r = [];
|
|
@@ -371,45 +408,50 @@ var R = class extends F {
|
|
|
371
408
|
if (!f || !(r && i) && f === e.path || t.length === 0) return null;
|
|
372
409
|
let p = t[0];
|
|
373
410
|
if (!p) return null;
|
|
374
|
-
let m = c ?? e.path ?? "", h = l(u(e.name ?? "", m), p, r, d.localizedRouteNamePrefix), g = o || p !== d.defaultLocale.code;
|
|
375
|
-
return
|
|
411
|
+
let m = c ?? e.path ?? "", h = l(u(e.name ?? "", m), p, r, d.localizedRouteNamePrefix), g = o || p !== d.defaultLocale.code, _ = t.length === 1 ? this.localizeChildren(n, f, m, p, d, g) : this.localizeChildrenAllLocales(n, f, m, t, d), v = this.routeAliases(e, f, t, d);
|
|
412
|
+
return P(e, {
|
|
376
413
|
path: f,
|
|
377
414
|
name: h,
|
|
378
|
-
children:
|
|
379
|
-
alias:
|
|
380
|
-
meta: { alias:
|
|
415
|
+
children: _,
|
|
416
|
+
alias: v,
|
|
417
|
+
meta: { alias: v }
|
|
381
418
|
});
|
|
382
419
|
}
|
|
383
|
-
},
|
|
420
|
+
}, z = class extends F {
|
|
384
421
|
generateVariants(e, t) {
|
|
385
422
|
let n = e.path ?? "", r = u(e.name, e.path), i = t.getAllowedLocales(n, r), a = t.getCustomPathsForPage(n, r), o = s(e.children ?? []), c = [], l = t.defaultLocale.code;
|
|
386
423
|
if (i.includes(l)) {
|
|
387
|
-
let r = a?.[l], i = r ? h(r) : n, s = r ? { [l]: r } : {}, u = this.createLocalizedChildren(o, n, [l], !1, !1, !1, s, t);
|
|
388
|
-
c.push(
|
|
424
|
+
let r = a?.[l], i = r ? h(r) : n, s = r ? { [l]: r } : {}, u = this.createLocalizedChildren(o, n, [l], !1, !1, !1, s, t), d = this.routeAliases(e, i, null, t);
|
|
425
|
+
c.push(P(e, {
|
|
389
426
|
path: i,
|
|
390
427
|
name: e.name,
|
|
391
|
-
children: u
|
|
428
|
+
children: u,
|
|
429
|
+
...d.length ? {
|
|
430
|
+
alias: d,
|
|
431
|
+
meta: { alias: d }
|
|
432
|
+
} : {}
|
|
392
433
|
}));
|
|
393
434
|
}
|
|
394
435
|
let d = t.locales.filter((e) => i.includes(e.code) && e.code !== l);
|
|
395
436
|
if (d.length) {
|
|
396
|
-
if (a)
|
|
397
|
-
let
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
437
|
+
if (a) {
|
|
438
|
+
for (let r of d) {
|
|
439
|
+
let i = a[r.code];
|
|
440
|
+
if (!i) continue;
|
|
441
|
+
let s = this.createLocalizedRoute(e, [r.code], o, !0, i, t.customRegex, !1, r.code, n, t);
|
|
442
|
+
s && c.push(s);
|
|
443
|
+
}
|
|
444
|
+
let r = d.filter((e) => !a[e.code]).map((e) => e.code);
|
|
445
|
+
if (r.length) {
|
|
446
|
+
let i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, r.length !== 1 || r[0], n, t);
|
|
403
447
|
i && c.push(i);
|
|
404
448
|
}
|
|
405
|
-
}
|
|
406
|
-
else {
|
|
449
|
+
} else {
|
|
407
450
|
let r = d.map((e) => e.code), i = this.createLocalizedRoute(e, r, o, !1, "", t.customRegex, !1, !0, n, t);
|
|
408
451
|
i && c.push(i);
|
|
409
452
|
}
|
|
410
453
|
}
|
|
411
|
-
|
|
412
|
-
return f.length && c.push(...f), c;
|
|
454
|
+
return c;
|
|
413
455
|
}
|
|
414
456
|
postProcess(e, t) {
|
|
415
457
|
let n = [], r = [];
|
|
@@ -431,24 +473,24 @@ var R = class extends F {
|
|
|
431
473
|
createLocalizedVariants(e, t, n, r, i, a = !1, o, c) {
|
|
432
474
|
let l = h(e.path ?? ""), f = T(j(t, e.path ?? "")), p = c.localizedPaths[f], m = !!p, _ = [];
|
|
433
475
|
if (!m) {
|
|
434
|
-
let u = g(l), d =
|
|
435
|
-
|
|
476
|
+
let u = g(l), d = {};
|
|
477
|
+
for (let e in o) d[e] = j(o[e], u);
|
|
478
|
+
let f = this.createLocalizedChildren(s(e.children ?? []), j(t, e.path ?? ""), n, r, i, a, d, c), p = this.buildChildRouteName(e.name, a, c);
|
|
479
|
+
return _.push(P(e, {
|
|
436
480
|
path: u,
|
|
437
|
-
name:
|
|
438
|
-
children:
|
|
481
|
+
name: p,
|
|
482
|
+
children: f
|
|
439
483
|
})), _;
|
|
440
484
|
}
|
|
441
485
|
for (let a of n) {
|
|
442
|
-
let n = o?.[a]
|
|
443
|
-
l && n && (m = f ? h(f) : j(n, e.path ?? ""));
|
|
444
|
-
let v = g(d(a, c.defaultLocale, i) ? S(a, m, c.customRegex) : m), y = f ? h(f) : l ? n : j(t, e.path ?? ""), b = this.createLocalizedChildren(s(e.children ?? []), y, [a], r, i, a, {
|
|
486
|
+
let n = o?.[a] || t, l = p?.[a], f = l ? N(n, l) : h(e.path ?? ""), m = g(d(a, c.defaultLocale, i) ? S(a, f, c.customRegex) : f), v = j(n, g(f)), y = this.createLocalizedChildren(s(e.children ?? []), j(t, e.path ?? ""), [a], r, i, a, {
|
|
445
487
|
...o,
|
|
446
|
-
[a]:
|
|
447
|
-
}, c),
|
|
448
|
-
_.push(
|
|
449
|
-
path:
|
|
450
|
-
name:
|
|
451
|
-
children:
|
|
488
|
+
[a]: v
|
|
489
|
+
}, c), b = this.buildLocalizedRouteName(u(e.name, e.path), a, r, !!p, c);
|
|
490
|
+
_.push(P(e, {
|
|
491
|
+
path: m,
|
|
492
|
+
name: b,
|
|
493
|
+
children: y
|
|
452
494
|
}));
|
|
453
495
|
}
|
|
454
496
|
return _;
|
|
@@ -461,13 +503,13 @@ var R = class extends F {
|
|
|
461
503
|
if (!f || f === e.path || t.length === 0) return null;
|
|
462
504
|
let p = t[0];
|
|
463
505
|
if (!p) return null;
|
|
464
|
-
let m = c ?? e.path ?? "";
|
|
465
|
-
return
|
|
506
|
+
let m = c ?? e.path ?? "", h = l(u(e.name ?? "", m), p, r, d.localizedRouteNamePrefix), g = this.routeAliases(e, f, t, d);
|
|
507
|
+
return P(e, {
|
|
466
508
|
path: f,
|
|
467
|
-
name:
|
|
509
|
+
name: h,
|
|
468
510
|
children: this.createLocalizedChildren(n, m, t, !0, !1, s, { [p]: i }, d),
|
|
469
|
-
alias:
|
|
470
|
-
meta: { alias:
|
|
511
|
+
alias: g,
|
|
512
|
+
meta: { alias: g }
|
|
471
513
|
});
|
|
472
514
|
}
|
|
473
515
|
buildLocalizedRouteName(e, t, n, r = !1, i) {
|
|
@@ -476,18 +518,18 @@ var R = class extends F {
|
|
|
476
518
|
};
|
|
477
519
|
//#endregion
|
|
478
520
|
//#region src/strategies/factory.ts
|
|
479
|
-
function
|
|
521
|
+
function B(e) {
|
|
480
522
|
switch (e) {
|
|
481
523
|
case "no_prefix": return new I();
|
|
482
|
-
case "prefix": return new
|
|
483
|
-
case "prefix_and_default": return new
|
|
484
|
-
case "prefix_except_default": return new
|
|
524
|
+
case "prefix": return new L();
|
|
525
|
+
case "prefix_and_default": return new R();
|
|
526
|
+
case "prefix_except_default": return new z();
|
|
485
527
|
default: throw Error(`Unknown route strategy: ${e}`);
|
|
486
528
|
}
|
|
487
529
|
}
|
|
488
530
|
//#endregion
|
|
489
531
|
//#region src/route-generator.ts
|
|
490
|
-
var
|
|
532
|
+
var V = class {
|
|
491
533
|
constructor(e) {
|
|
492
534
|
this.localizedPaths = {};
|
|
493
535
|
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 = p(t, n);
|
|
@@ -526,7 +568,7 @@ var H = class {
|
|
|
526
568
|
rawGlobalLocaleRoutes: this.rawGlobalLocaleRoutes
|
|
527
569
|
});
|
|
528
570
|
this.localizedPaths = t.localizedPaths;
|
|
529
|
-
let n =
|
|
571
|
+
let n = B(this.strategy), r = [...e], i = [];
|
|
530
572
|
for (let e of r) i.push(...n.processPage(e, t));
|
|
531
573
|
let a = n.postProcess(i, t);
|
|
532
574
|
e.length = 0, e.push(...a);
|
|
@@ -570,4 +612,4 @@ var H = class {
|
|
|
570
612
|
}
|
|
571
613
|
};
|
|
572
614
|
//#endregion
|
|
573
|
-
export {
|
|
615
|
+
export { V as RouteGenerator, x as buildEncodedPathAliases, S as buildFullPath, C as buildFullPathNoPrefix, l as buildRouteName, s as cloneArray, E as extractLocalizedPaths, e as isInternalPath, O as isLocaleAllowedForUnlocalizedRoute, f as isLocaleDefault, D as isLocalizationDisabledForPage, c as isPageRedirectOnly, h as normalizePath, m as normalizeRouteKey, g as removeLeadingSlash, d as shouldAddLocalePrefix };
|
|
@@ -19,6 +19,23 @@ export declare abstract class BaseStrategy implements RouteStrategy {
|
|
|
19
19
|
protected localizeChildrenAllLocales(children: NuxtPage[], parentPath: string, parentOriginalPath: string, localeCodes: string[], context: GeneratorContext): NuxtPage[];
|
|
20
20
|
private localizeChildAllLocales;
|
|
21
21
|
private localizeChild;
|
|
22
|
+
/** The page's declared aliases as a list: vue-router accepts a single string as well. */
|
|
23
|
+
protected declaredAliases(page: NuxtPage): string[];
|
|
24
|
+
/**
|
|
25
|
+
* vue-router record aliases of a generated route: the page's own aliases (`alias` /
|
|
26
|
+
* `definePageMeta({ alias })`) localized like the route path — under the route's `:locale(...)`
|
|
27
|
+
* param (`localeCodes`), or as written for an unprefixed route (`null`) — plus percent-encoded
|
|
28
|
+
* variants of non-ASCII paths. vue-router applies record aliases to the children and an alias
|
|
29
|
+
* shares the record's name, so alias URLs follow the localized child tree and names stay
|
|
30
|
+
* unique (separate alias routes duplicated both).
|
|
31
|
+
*/
|
|
32
|
+
protected routeAliases(page: NuxtPage, routePath: string, localeCodes: string[] | null, context: GeneratorContext): string[];
|
|
33
|
+
/**
|
|
34
|
+
* Children of an unprefixed route with `locale`'s custom paths applied, keeping their own
|
|
35
|
+
* names (the localized names belong to the prefixed variant). Unchanged when no descendant
|
|
36
|
+
* has a custom path.
|
|
37
|
+
*/
|
|
38
|
+
protected localizeChildPathsKeepNames(children: NuxtPage[], parentLocalizedPath: string, parentOriginalPath: string, locale: string, context: GeneratorContext): NuxtPage[];
|
|
22
39
|
/**
|
|
23
40
|
* Shared helper for strategies that build prefix-based paths (:locale(...)):
|
|
24
41
|
* - for custom paths, decides whether a prefix is required,
|
|
@@ -3,11 +3,11 @@ import { GeneratorContext } from '../core/context';
|
|
|
3
3
|
import { BaseStrategy } from './abstract';
|
|
4
4
|
export declare class NoPrefixStrategy extends BaseStrategy {
|
|
5
5
|
protected generateVariants(page: NuxtPage, context: GeneratorContext): NuxtPage[];
|
|
6
|
+
/** Whether a descendant has a custom path for `locale` that differs from its own path. */
|
|
7
|
+
private hasChangedChildPath;
|
|
6
8
|
/**
|
|
7
|
-
* Localizes the children tree for the no_prefix strategy
|
|
8
|
-
*
|
|
9
|
-
* - on the first level, custom paths (globalLocaleRoutes/filesLocaleRoutes) are applied;
|
|
10
|
-
* - on deeper levels, path stays as a segment (item, detail, etc.).
|
|
9
|
+
* Localizes the children tree for the no_prefix strategy: custom paths
|
|
10
|
+
* (globalLocaleRoutes/filesLocaleRoutes) apply at every level.
|
|
11
11
|
*/
|
|
12
12
|
private localizeChildrenForNoPrefix;
|
|
13
13
|
}
|
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.30",
|
|
4
4
|
"description": "Locale-aware route generator for Nuxt i18n micro",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"i18n",
|
|
@@ -63,9 +63,9 @@
|
|
|
63
63
|
},
|
|
64
64
|
"dependencies": {
|
|
65
65
|
"@nuxt/schema": "^3.18.1 || ^4.5.2",
|
|
66
|
-
"@i18n-micro/
|
|
67
|
-
"@i18n-micro/
|
|
68
|
-
"@i18n-micro/
|
|
66
|
+
"@i18n-micro/utils": "^1.0.27",
|
|
67
|
+
"@i18n-micro/core": "^1.3.22",
|
|
68
|
+
"@i18n-micro/types": "^1.2.18"
|
|
69
69
|
},
|
|
70
70
|
"devDependencies": {
|
|
71
71
|
"publint": "^0.3.24",
|
package/dist/core/alias.d.ts
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
import { NuxtPage } from '@nuxt/schema';
|
|
2
|
-
/**
|
|
3
|
-
* Generates dedicated routes for page aliases.
|
|
4
|
-
* Mirrors the legacy handleAliasRoutes behavior in the original RouteGenerator.
|
|
5
|
-
*/
|
|
6
|
-
export declare function generateAliasRoutes(page: NuxtPage, localeCodes: string[], customRegex?: string | RegExp, localizedRouteNamePrefix?: string): NuxtPage[];
|