react-folder-router
Version:
File-system based routing for React applications that converts your folder structure into routes. Works as an extension to react-router-dom, which is required as a peer dependency.
2 lines (1 loc) • 816 B
JavaScript
import e,{useMemo as t}from"react";function r(){const t=import.meta.glob("/src/routes/**/Index.jsx");return function(e){return e.sort(((e,t)=>{if("/*"===e.path)return 1;if("/*"===t.path)return-1;const r=e.path.includes(":"),n=t.path.includes(":");return r&&!n?1:!r&&n?-1:e.path.split("/").length-t.path.split("/").length}))}(Object.keys(t).map((r=>{const n=function(e){return e.replace(/^\/src\/routes\//,"").replace(/(?:^|\/)Index\.jsx$/,"").replace(/\[\.\.\.(\w+)\]/g,"*").replace(/\[(\w+)\]/g,":$1").replace(/\/+/g,"/")}(r),a=e.lazy((()=>t[r]().then((e=>{const t=Object.keys(e).filter((e=>"default"!==e));return{default:e.default||e[t[0]]}}))));return{path:""===n?"/":`/${n.toLowerCase()}`,element:e.createElement(a,null)}})))}function n(){return t((()=>r()),[])}export{r as generateRoutes,n as useFolderRoutes};