@tanstack/router-generator 1.112.0 → 1.112.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,6 +6,7 @@ import {
6
6
  routePathToVariable,
7
7
  } from '../../utils'
8
8
  import { getRouteNodes as getRouteNodesPhysical } from '../physical/getRouteNodes'
9
+ import { rootPathId } from '../physical/rootPathId'
9
10
  import { virtualRootRouteSchema } from './config'
10
11
  import { loadConfigFile } from './loadConfigFile'
11
12
  import type {
@@ -64,8 +65,8 @@ export async function getRouteNodes(
64
65
  filePath: virtualRouteConfig.file,
65
66
  fullPath: join(fullDir, virtualRouteConfig.file),
66
67
  variableName: 'rootRoute',
67
- routePath: '/',
68
- isRoot: true,
68
+ routePath: `/${rootPathId}`,
69
+ _fsRouteType: '__root',
69
70
  })
70
71
 
71
72
  const rootRouteNode = allNodes[0]
@@ -150,7 +151,7 @@ export async function getRouteNodesRecursive(
150
151
  return { filePath, variableName, fullPath }
151
152
  }
152
153
  const parentRoutePath = removeTrailingSlash(parent?.routePath ?? '/')
153
- const isLayout = node.type === 'layout'
154
+
154
155
  switch (node.type) {
155
156
  case 'index': {
156
157
  const { filePath, variableName, fullPath } = getFile(node.file)
@@ -160,7 +161,7 @@ export async function getRouteNodesRecursive(
160
161
  fullPath,
161
162
  variableName,
162
163
  routePath,
163
- isLayout,
164
+ _fsRouteType: 'static',
164
165
  } satisfies RouteNode
165
166
  }
166
167
 
@@ -176,7 +177,7 @@ export async function getRouteNodesRecursive(
176
177
  fullPath,
177
178
  variableName,
178
179
  routePath,
179
- isLayout,
180
+ _fsRouteType: 'static',
180
181
  }
181
182
  } else {
182
183
  routeNode = {
@@ -184,8 +185,8 @@ export async function getRouteNodesRecursive(
184
185
  fullPath: '',
185
186
  variableName: routePathToVariable(routePath),
186
187
  routePath,
187
- isLayout,
188
188
  isVirtual: true,
189
+ _fsRouteType: 'static',
189
190
  }
190
191
  }
191
192
 
@@ -198,6 +199,9 @@ export async function getRouteNodesRecursive(
198
199
  routeNode,
199
200
  )
200
201
  routeNode.children = children
202
+
203
+ // If the route has children, it should be a layout
204
+ routeNode._fsRouteType = 'layout'
201
205
  }
202
206
  return routeNode
203
207
  }
@@ -216,10 +220,10 @@ export async function getRouteNodesRecursive(
216
220
 
217
221
  const routeNode: RouteNode = {
218
222
  fullPath,
219
- isLayout,
220
223
  filePath,
221
224
  variableName,
222
225
  routePath,
226
+ _fsRouteType: 'pathless_layout',
223
227
  }
224
228
 
225
229
  if (node.children !== undefined) {
package/src/generator.ts CHANGED
@@ -23,7 +23,7 @@ import {
23
23
  fillTemplate,
24
24
  getTargetTemplate,
25
25
  } from './template'
26
- import type { GetRouteNodesResult, RouteNode } from './types'
26
+ import type { FsRouteType, GetRouteNodesResult, RouteNode } from './types'
27
27
  import type { Config } from './config'
28
28
 
29
29
  export const CONSTANTS = {
@@ -113,6 +113,16 @@ export async function generator(config: Config, root: string) {
113
113
  const routeTree: Array<RouteNode> = []
114
114
  const routePiecesByPath: Record<string, RouteSubNode> = {}
115
115
 
116
+ // Filtered API Route nodes
117
+ const onlyAPIRouteNodes = preRouteNodes.filter(
118
+ (d) => d._fsRouteType === 'api',
119
+ )
120
+
121
+ // Filtered Generator Route nodes
122
+ const onlyGeneratorRouteNodes = preRouteNodes.filter(
123
+ (d) => d._fsRouteType !== 'api',
124
+ )
125
+
116
126
  // Loop over the flat list of routeNodes and
117
127
  // build up a tree based on the routeNodes' routePath
118
128
  const routeNodes: Array<RouteNode> = []
@@ -204,7 +214,8 @@ export async function generator(config: Config, root: string) {
204
214
  const tLazyRouteTemplate = ROUTE_TEMPLATE.lazyRoute
205
215
 
206
216
  if (!routeCode) {
207
- if (node.isLazy) {
217
+ // Creating a new lazy route file
218
+ if (node._fsRouteType === 'lazy') {
208
219
  // Check by default check if the user has a specific lazy route template
209
220
  // If not, check if the user has a route template and use that instead
210
221
  replaced = await fillTemplate(
@@ -221,11 +232,18 @@ export async function generator(config: Config, root: string) {
221
232
  },
222
233
  )
223
234
  } else if (
224
- node.isRoute ||
225
- (!node.isComponent &&
226
- !node.isErrorComponent &&
227
- !node.isPendingComponent &&
228
- !node.isLoader)
235
+ // Creating a new normal route file
236
+ (['layout', 'static'] satisfies Array<FsRouteType>).some(
237
+ (d) => d === node._fsRouteType,
238
+ ) ||
239
+ (
240
+ [
241
+ 'component',
242
+ 'pendingComponent',
243
+ 'errorComponent',
244
+ 'loader',
245
+ ] satisfies Array<FsRouteType>
246
+ ).every((d) => d !== node._fsRouteType)
229
247
  ) {
230
248
  replaced = await fillTemplate(
231
249
  config,
@@ -241,6 +259,7 @@ export async function generator(config: Config, root: string) {
241
259
  )
242
260
  }
243
261
  } else {
262
+ // Update the existing route file
244
263
  replaced = routeCode
245
264
  .replace(
246
265
  /(FileRoute\(\s*['"])([^\s]*)(['"],?\s*\))/g,
@@ -252,12 +271,12 @@ export async function generator(config: Config, root: string) {
252
271
  'gs',
253
272
  ),
254
273
  (_, p1, __, ___, p4) =>
255
- `${p1}${node.isLazy ? 'createLazyFileRoute' : 'createFileRoute'}${p4}`,
274
+ `${p1}${node._fsRouteType === 'lazy' ? 'createLazyFileRoute' : 'createFileRoute'}${p4}`,
256
275
  )
257
276
  .replace(
258
277
  /create(Lazy)?FileRoute(\(\s*['"])([^\s]*)(['"],?\s*\))/g,
259
278
  (_, __, p2, ___, p4) =>
260
- `${node.isLazy ? 'createLazyFileRoute' : 'createFileRoute'}${p2}${escapedRoutePath}${p4}`,
279
+ `${node._fsRouteType === 'lazy' ? 'createLazyFileRoute' : 'createFileRoute'}${p2}${escapedRoutePath}${p4}`,
261
280
  )
262
281
  }
263
282
 
@@ -270,23 +289,27 @@ export async function generator(config: Config, root: string) {
270
289
 
271
290
  if (
272
291
  !node.isVirtual &&
273
- (node.isLoader ||
274
- node.isComponent ||
275
- node.isErrorComponent ||
276
- node.isPendingComponent ||
277
- node.isLazy)
292
+ (
293
+ [
294
+ 'lazy',
295
+ 'loader',
296
+ 'component',
297
+ 'pendingComponent',
298
+ 'errorComponent',
299
+ ] satisfies Array<FsRouteType>
300
+ ).some((d) => d === node._fsRouteType)
278
301
  ) {
279
302
  routePiecesByPath[node.routePath!] =
280
303
  routePiecesByPath[node.routePath!] || {}
281
304
 
282
305
  routePiecesByPath[node.routePath!]![
283
- node.isLazy
306
+ node._fsRouteType === 'lazy'
284
307
  ? 'lazy'
285
- : node.isLoader
308
+ : node._fsRouteType === 'loader'
286
309
  ? 'loader'
287
- : node.isErrorComponent
310
+ : node._fsRouteType === 'errorComponent'
288
311
  ? 'errorComponent'
289
- : node.isPendingComponent
312
+ : node._fsRouteType === 'pendingComponent'
290
313
  ? 'pendingComponent'
291
314
  : 'component'
292
315
  ] = node
@@ -297,20 +320,21 @@ export async function generator(config: Config, root: string) {
297
320
  await handleNode({
298
321
  ...node,
299
322
  isVirtual: true,
300
- isLazy: false,
301
- isLoader: false,
302
- isComponent: false,
303
- isErrorComponent: false,
304
- isPendingComponent: false,
323
+ _fsRouteType: 'static',
305
324
  })
306
325
  }
307
326
  return
308
327
  }
309
328
 
310
329
  const cleanedPathIsEmpty = (node.cleanedPath || '').length === 0
311
- const nonPathRoute = node.isRoute && node.isNonPath
330
+ const nonPathRoute =
331
+ node._fsRouteType === 'pathless_layout' && node.isNonPath
332
+
312
333
  node.isVirtualParentRequired =
313
- node.isLayout || nonPathRoute ? !cleanedPathIsEmpty : false
334
+ node._fsRouteType === 'pathless_layout' || nonPathRoute
335
+ ? !cleanedPathIsEmpty
336
+ : false
337
+
314
338
  if (!node.isVirtual && node.isVirtualParentRequired) {
315
339
  const parentRoutePath = removeLastSegmentFromPath(node.routePath) || '/'
316
340
  const parentVariableName = routePathToVariable(parentRoutePath)
@@ -320,7 +344,7 @@ export async function generator(config: Config, root: string) {
320
344
  )
321
345
 
322
346
  if (!anchorRoute) {
323
- const parentNode = {
347
+ const parentNode: RouteNode = {
324
348
  ...node,
325
349
  path: removeLastSegmentFromPath(node.path) || '/',
326
350
  filePath: removeLastSegmentFromPath(node.filePath) || '/',
@@ -328,7 +352,7 @@ export async function generator(config: Config, root: string) {
328
352
  routePath: parentRoutePath,
329
353
  variableName: parentVariableName,
330
354
  isVirtual: true,
331
- isLayout: false,
355
+ _fsRouteType: 'layout', // layout since this route will wrap other routes
332
356
  isVirtualParentRoute: true,
333
357
  isVirtualParentRequired: false,
334
358
  }
@@ -338,7 +362,7 @@ export async function generator(config: Config, root: string) {
338
362
 
339
363
  node.parent = parentNode
340
364
 
341
- if (node.isLayout) {
365
+ if (node._fsRouteType === 'pathless_layout') {
342
366
  // since `node.path` is used as the `id` on the route definition, we need to update it
343
367
  node.path = determineNodePath(node)
344
368
  }
@@ -364,18 +388,22 @@ export async function generator(config: Config, root: string) {
364
388
  routeNodes.push(node)
365
389
  }
366
390
 
367
- for (const node of preRouteNodes.filter((d) => !d.isAPIRoute)) {
391
+ for (const node of onlyGeneratorRouteNodes) {
368
392
  await handleNode(node)
369
393
  }
370
394
  checkRouteFullPathUniqueness(
371
395
  preRouteNodes.filter(
372
- (d) => !d.isAPIRoute && d.children === undefined && d.isLazy !== true,
396
+ (d) =>
397
+ d.children === undefined &&
398
+ (['api', 'lazy'] satisfies Array<FsRouteType>).every(
399
+ (type) => type !== d._fsRouteType,
400
+ ),
373
401
  ),
374
402
  config,
375
403
  )
376
404
 
377
405
  const startAPIRouteNodes: Array<RouteNode> = checkStartAPIRoutes(
378
- preRouteNodes.filter((d) => d.isAPIRoute),
406
+ onlyAPIRouteNodes,
379
407
  config,
380
408
  )
381
409
 
@@ -430,11 +458,11 @@ export async function generator(config: Config, root: string) {
430
458
 
431
459
  function buildRouteTreeConfig(nodes: Array<RouteNode>, depth = 1): string {
432
460
  const children = nodes.map((node) => {
433
- if (node.isRoot) {
461
+ if (node._fsRouteType === '__root') {
434
462
  return
435
463
  }
436
464
 
437
- if (node.isLayout && !node.children?.length) {
465
+ if (node._fsRouteType === 'pathless_layout' && !node.children?.length) {
438
466
  return
439
467
  }
440
468
 
package/src/types.ts CHANGED
@@ -2,22 +2,14 @@ export type RouteNode = {
2
2
  filePath: string
3
3
  fullPath: string
4
4
  variableName: string
5
+ _fsRouteType: FsRouteType
5
6
  routePath?: string
6
7
  cleanedPath?: string
7
8
  path?: string
8
9
  isNonPath?: boolean
9
- isLayout?: boolean
10
10
  isVirtualParentRequired?: boolean
11
11
  isVirtualParentRoute?: boolean
12
- isRoute?: boolean
13
- isAPIRoute?: boolean
14
- isLoader?: boolean
15
- isComponent?: boolean
16
- isErrorComponent?: boolean
17
- isPendingComponent?: boolean
18
12
  isVirtual?: boolean
19
- isLazy?: boolean
20
- isRoot?: boolean
21
13
  children?: Array<RouteNode>
22
14
  parent?: RouteNode
23
15
  }
@@ -26,3 +18,15 @@ export interface GetRouteNodesResult {
26
18
  rootRouteNode?: RouteNode
27
19
  routeNodes: Array<RouteNode>
28
20
  }
21
+
22
+ export type FsRouteType =
23
+ | '__root'
24
+ | 'static'
25
+ | 'layout'
26
+ | 'pathless_layout'
27
+ | 'lazy'
28
+ | 'api'
29
+ | 'loader' // @deprecated
30
+ | 'component' // @deprecated
31
+ | 'pendingComponent' // @deprecated
32
+ | 'errorComponent' // @deprecated