@astrolicious/i18n
Version:
Yet another i18n integration for Astro with server and client utilities, type safety and translations built-in.
2 lines • 3.1 kB
JavaScript
import{join as K,relative as Q}from"node:path";import{fileURLToPath as x}from"node:url";import{defineUtility as V,watchDirectory as X}from"astro-integration-kit";import{normalizePath as U}from"vite";import{defineUtility as q}from"astro-integration-kit";import{mkdirSync as k,readFileSync as T,rmSync as _,writeFileSync as H}from"node:fs";import{dirname as D,join as W,relative as L,resolve as O}from"node:path";import{fileURLToPath as A}from"node:url";import{defineUtility as b}from"astro-integration-kit";import{addPageDir as M}from"astro-pages";import{AstroError as z}from"astro/errors";import{withLeadingSlash as C}from"ufo";import{normalizePath as $}from"vite";var F=t=>{let r=t.match(/export const prerender = (\w+)/);if(r)return r[1]==="true"},G=t=>Object.entries(M({...t,dir:h}).pages).map(([r,o])=>({pattern:r,entrypoint:o})),B=b("astro:config:setup")(({config:t})=>{let r=A(new URL(h,t.srcDir)),o=O(A(t.root),"./.astro/astro-i18n/entrypoints");return{routesDir:r,entrypointsDir:o}}),N=({strategy:t,defaultLocale:r,locales:o,pages:c},a,e,u,g)=>{let y=()=>{let s=a===r,m=s&&t==="prefixExceptDefault"?"":`/${a}`,n=C(s?e.pattern:c?.[e.pattern]?.[a]??e.pattern);return m+n},d=s=>{let m=(i,j,l)=>{let f=O(D(j),i),p=L(D(l),f);return $(p)};k(D(s),{recursive:!0});let n=T(e.entrypoint,"utf-8");if(e.entrypoint.endsWith(".astro"))try{n=n.replaceAll("getLocalePlaceholder()",`"${a}"`).replaceAll("getLocalesPlaceholder()",`[${o.map(l=>`"${l}"`).join(", ")}]`).replaceAll("getDefaultLocalePlaceholder()",`"${r}"`);let[,i,...j]=n.split("---");if(!i)throw new Error("No frontmatter found");i=i.replace(/import\s+([\s\S]*?)\s+from\s+['"](.+?)['"]/g,(l,f,p)=>{let E=p.startsWith("./")||p.startsWith("../")?m(p,e.entrypoint,s):p;return`import ${f} from '${E}'`}),i=i.replace(/import\s*\(\s*['"](.+?)['"]\s*\)/g,(l,f)=>`import('${f.startsWith("./")||f.startsWith("../")?m(f,e.entrypoint,s):f}')`),n=`---${i}---${j.join("---")}`}catch{throw new z(`An error occured while transforming "${e.entrypoint}".`,"Make sure it has a valid frontmatter, even empty")}return H(s,n,"utf-8"),{prerender:F(n)}},R=s=>{let m=[],n=s.match(/\[([^\]]+)]/g);if(n)for(let i of n)m.push(i.slice(1,-1));return m},P=y(),I=W(u.entrypointsDir,a,$(L(u.routesDir,e.entrypoint))),{prerender:w}=d(I);return g.info(`Injecting "${P}" route`),{locale:a,params:R(P),pattern:e.pattern,injectedRoute:{pattern:P,entrypoint:I,...w?{prerender:w}:{}}}},S=(t,r,o)=>{let{config:c,injectRoute:a}=t,{locales:e}=r;o.info("Starting routes injection...");let u=B(t);_(u.entrypointsDir,{recursive:!0,force:!0}),o.info(`Cleaned "${$(L(A(c.root),u.entrypointsDir))}" directory`);let g=[],y=G(t);for(let d of e)for(let R of y)g.push(N(r,d,R,u,o));for(let{injectedRoute:d}of g)a(d);return{routes:g}};var h="routes",J="astro-i18n/routing",pt=q("astro:config:setup")((t,r)=>{let o=t.logger.fork(J);v(t,o);let{routes:c}=S(t,r,o);return{routes:c}});var v=V("astro:config:setup")((t,r)=>{let{config:o}=t,c=U(K(x(o.srcDir),h));X(t,c),r.info(`Registered watcher for "${U(Q(x(t.config.root),c))}" directory`)});export{v as handleRoutesHMR};
//# sourceMappingURL=hmr.js.map