Skip to content

Commit 88dafb5

Browse files
committed
fix: file route sorting on windows
1 parent a0980b8 commit 88dafb5

File tree

1 file changed

+20
-28
lines changed

1 file changed

+20
-28
lines changed

packages/router-cli/src/generator.ts

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,10 @@ async function getRouteNodes(config: Config) {
6161
if (stat.isDirectory()) {
6262
await recurse(relativePath)
6363
} else {
64-
const filePath = path.join(dir, fileName)
64+
const filePath = replaceBackslash(path.join(dir, fileName))
6565
const filePathNoExt = removeExt(filePath)
6666
let routePath =
67-
replaceBackslash(
68-
cleanPath(`/${filePathNoExt.split('.').join('/')}`),
69-
) ?? ''
67+
cleanPath(`/${filePathNoExt.split('.').join('/')}`) || ''
7068
const variableName = routePathToVariable(routePath)
7169

7270
// Remove the index from the route path and
@@ -149,26 +147,20 @@ export async function generator(config: Config) {
149147
const start = Date.now()
150148
const routePathIdPrefix = config.routeFilePrefix ?? ''
151149

152-
let preRouteNodes = await getRouteNodes(config)
153-
154-
const sortRouteNodes = (nodes: RouteNode[]): RouteNode[] => {
155-
return multiSortBy(nodes, [
156-
(d) => (d.routePath === '/' ? -1 : 1),
157-
(d) => d.routePath?.split('/').length,
158-
(d) => (d.filePath?.match(/[./]index[.]/) ? 1 : -1),
159-
(d) =>
160-
d.filePath?.match(
161-
/[./](component|errorComponent|pendingComponent|loader)[.]/,
162-
)
163-
? 1
164-
: -1,
165-
(d) => (d.filePath?.match(/[./]route[.]/) ? -1 : 1),
166-
(d) => (d.routePath?.endsWith('/') ? -1 : 1),
167-
(d) => d.routePath,
168-
]).filter((d) => d.routePath !== `/${routePathIdPrefix + rootPathId}`)
169-
}
170-
171-
preRouteNodes = sortRouteNodes(preRouteNodes)
150+
const preRouteNodes = multiSortBy(await getRouteNodes(config), [
151+
(d) => (d.routePath === '/' ? -1 : 1),
152+
(d) => d.routePath?.split('/').length,
153+
(d) => (d.filePath?.match(/[./]index[.]/) ? 1 : -1),
154+
(d) =>
155+
d.filePath?.match(
156+
/[./](component|errorComponent|pendingComponent|loader)[.]/,
157+
)
158+
? 1
159+
: -1,
160+
(d) => (d.filePath?.match(/[./]route[.]/) ? -1 : 1),
161+
(d) => (d.routePath?.endsWith('/') ? -1 : 1),
162+
(d) => d.routePath,
163+
]).filter((d) => d.routePath !== `/${routePathIdPrefix + rootPathId}`)
172164

173165
const routeTree: RouteNode[] = []
174166
const routePiecesByPath: Record<string, RouteSubNode> = {}
@@ -518,8 +510,8 @@ function capitalize(s: string) {
518510
return s.charAt(0).toUpperCase() + s.slice(1)
519511
}
520512

521-
function sanitize(s?: string) {
522-
return replaceBackslash(s?.replace(/\\index/gi, ''))
513+
function sanitize(s: string) {
514+
return replaceBackslash(s.replace(/\\index/gi, ''))
523515
}
524516

525517
function removeUnderscores(s?: string) {
@@ -530,8 +522,8 @@ function removeTrailingUnderscores(s?: string) {
530522
return s?.replace(/(_$)/, '').replace(/(_\/)/, '/')
531523
}
532524

533-
function replaceBackslash(s?: string) {
534-
return s?.replace(/\\/gi, '/')
525+
function replaceBackslash(s: string) {
526+
return s.replace(/\\/gi, '/')
535527
}
536528

537529
export function hasParentRoute(

0 commit comments

Comments
 (0)