Files
tuono/packages/fs-router-vite-plugin/src/sort-route-nodes.ts
T
Valerio Ageno 1726312d34 Support nested roots (#18)
* feat: remove isLoader route property

* fix: has-parent-route fs-router util

* feat: handle routeGenerator nested __root

* feat: enable multiple roots with nested dynamic paths

* feat: enable nested routes

* fix: prevent route path creation for __root

* feat: prevent wrong path on subfolder index routes

* chore: cleaned tutorial example folder

* feat: update version to v0.8.0
2024-07-23 19:13:18 +02:00

18 lines
804 B
TypeScript

import type { RouteNode } from './types'
import { multiSortBy } from './utils'
import { ROOT_PATH_ID } from './constants'
// Routes need to be sorted in order to iterate over the handleNode fn
// with first the items that might be parent routes
export const sortRouteNodes = (routes: RouteNode[]): RouteNode[] =>
multiSortBy(routes, [
(d): number => (d.routePath === '/' ? -1 : 1),
(d): number => d.routePath.split('/').length,
// Dynamic route
(d): number => (d.routePath.endsWith(']') ? 1 : -1),
(d): number => (d.filePath.match(/[./]index[.]/) ? 1 : -1),
(d): number => (d.filePath.match(/[./]route[.]/) ? -1 : 1),
(d): number => (d.routePath.endsWith('/') ? -1 : 1),
(d): string => d.routePath,
]).filter((d) => ![`/${ROOT_PATH_ID}`].includes(d.routePath || ''))