react-file-based-routes
Version:
React Generate Router
2 lines (1 loc) • 2.63 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react/jsx-runtime"),require("react"),require("react-router-dom")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","react","react-router-dom"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactGenerateRouter={},e.jsxRuntime,e.React,e.reactRouterDom)}(this,(function(e,t,r,a){"use strict";const n={route:[/\/src\/pages\/|\.tsx$/g,""],splat:[/\[\.{3}\w+\]/g,"*"],param:[/\[([^\]]+)\]/g,":$1"],slash:[/^index$|\./g,"/"],lazy:[/lazy_+/g,""]};const s=(void 0)("/src/pages/(_app|_404).tsx",{eager:!0}),c=(void 0)(["/src/pages/**/[a-z[]*.tsx","!/**/(lazy_)[a-z[]*.tsx"],{eager:!0}),i=(void 0)("/src/pages/**/(lazy_)*"),l=(o=s,Object.keys(o).reduce(((e,t)=>({...e,[t.replace(...n.route)]:o[t]})),{}));var o;const d=function(e){return Object.keys(e).reduce(((a,s)=>{const c=/lazy_.+\.tsx$/.test(s),i=e[s];if(c){const t=s.replace(...n.lazy);if(e[t])throw new Error(`001: The router with path=${t} conflict with the lazy router with path=${s}`)}const l=c?((e,a)=>({.../index\.tsx$/.test(a)&&!a.includes("pages/index")?{index:!0}:{},id:a.replace(...n.route),lazy:async()=>{const a=await e(),n=a?.default||r.Fragment,s=a?.Pending,c=a?.Catch;return{Component:()=>s?t.jsx(r.Suspense,{fallback:t.jsx(s,{}),children:t.jsx(n,{})}):t.jsx(n,{}),errorElement:c?t.jsx(c,{}):void 0,loader:a?.Loader,action:a?.Action}}}))(i,s):((e,a)=>{const s=/index\.tsx$/.test(a)&&!a.includes("pages/index")?{index:!0}:{},c=e?.default||r.Fragment;return{...s,id:a.replace(...n.route),element:t.jsx((()=>e?.Pending?t.jsx(r.Suspense,{fallback:t.jsx(e.Pending,{}),children:t.jsx(c,{})}):t.jsx(c,{})),{}),errorElement:e?.Catch?t.jsx(e.Catch,{}):void 0,loader:e?.Loader,action:e?.Action}})(i,s),o=s.replace(...n.route).replace(...n.splat).replace(...n.param).split("/");return o.reduce(((e,t,r)=>{const s=t.replace(...n.lazy).replace(...n.slash),c=0===r,i=r===o.length-1&&o.length>1,d=!c&&!i,u=/^\w|\//.test(s)?"unshift":"push",p="layout"===t;if(c){if(s.startsWith(":")||"*"===s)return e;if(1===o.length)return a.push({path:s,...l}),e}if(c||d){const t=c?a:e.children,r=t?.find((e=>e.path===s));return r?r.children??=[]:t?.[u]({path:s,children:[]}),r||t?.["unshift"===u?0:t.length-1]}return p?Object.assign(e,l):(e?.children?.[u]({path:"/"===s?"":s,...l}),e)}),{}),a}),[])}({...c,...i}),u=l?._app,p=u?.default||a.Outlet,h=l?._404?.default||r.Fragment,x=u?.Catch||h,f=[{...{element:t.jsx(p,{}),errorElement:t.jsx(x,{}),loader:u?.Loader},children:[...d,{path:"*",element:t.jsx(h,{})}]}];e.default=f,Object.defineProperty(e,"__esModule",{value:!0})}));