@tanstack/router-generator 1.141.5 → 1.141.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.
@@ -82,6 +82,8 @@ const _Generator = class _Generator {
82
82
  this.targetTemplate = template.getTargetTemplate(this.config);
83
83
  this.routesDirectoryPath = this.getRoutesDirectoryPath();
84
84
  this.plugins.push(...opts.config.plugins || []);
85
+ this.indexTokenRegex = new RegExp(`[./]${this.config.indexToken}[.]`);
86
+ this.routeTokenRegex = new RegExp(`[./]${this.config.routeToken}[.]`);
85
87
  for (const plugin of this.plugins) {
86
88
  plugin.init?.({ generator: this });
87
89
  }
@@ -194,11 +196,9 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
194
196
  const preRouteNodes = utils.multiSortBy(beforeRouteNodes, [
195
197
  (d) => d.routePath === "/" ? -1 : 1,
196
198
  (d) => d.routePath?.split("/").length,
197
- (d) => d.filePath.match(new RegExp(`[./]${this.config.indexToken}[.]`)) ? 1 : -1,
198
- (d) => d.filePath.match(
199
- /[./](component|errorComponent|notFoundComponent|pendingComponent|loader|lazy)[.]/
200
- ) ? 1 : -1,
201
- (d) => d.filePath.match(new RegExp(`[./]${this.config.routeToken}[.]`)) ? -1 : 1,
199
+ (d) => d.filePath.match(this.indexTokenRegex) ? 1 : -1,
200
+ (d) => d.filePath.match(_Generator.componentPieceRegex) ? 1 : -1,
201
+ (d) => d.filePath.match(this.routeTokenRegex) ? -1 : 1,
202
202
  (d) => d.routePath?.endsWith("/") ? -1 : 1,
203
203
  (d) => d.routePath
204
204
  ]).filter((d) => {
@@ -239,8 +239,9 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
239
239
  routePiecesByPath: {},
240
240
  routeNodesByPath: /* @__PURE__ */ new Map()
241
241
  };
242
+ const prefixMap = new utils.RoutePrefixMap(routeFileResult);
242
243
  for (const node of routeFileResult) {
243
- _Generator.handleNode(node, acc, this.config);
244
+ _Generator.handleNode(node, acc, prefixMap, this.config);
244
245
  }
245
246
  this.crawlingResult = { rootRouteNode, routeFileResult, acc };
246
247
  if (!this.routeTreeFileCache) {
@@ -348,30 +349,45 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
348
349
  (d) => d.routePath?.endsWith(config.indexToken) ? -1 : 1,
349
350
  (d) => d
350
351
  ]);
351
- const routeImports = sortedRouteNodes.filter((d) => !d.isVirtual).flatMap(
352
- (node) => utils.getImportForRouteNode(
353
- node,
354
- config,
355
- this.generatedRouteTreePath,
356
- this.root
357
- )
358
- );
359
- const virtualRouteNodes = sortedRouteNodes.filter((d) => d.isVirtual).map((node) => {
360
- return `const ${node.variableName}RouteImport = createFileRoute('${node.routePath}')()`;
361
- });
352
+ const routeImports = [];
353
+ const virtualRouteNodes = [];
354
+ for (const node of sortedRouteNodes) {
355
+ if (node.isVirtual) {
356
+ virtualRouteNodes.push(
357
+ `const ${node.variableName}RouteImport = createFileRoute('${node.routePath}')()`
358
+ );
359
+ } else {
360
+ routeImports.push(
361
+ utils.getImportForRouteNode(
362
+ node,
363
+ config,
364
+ this.generatedRouteTreePath,
365
+ this.root
366
+ )
367
+ );
368
+ }
369
+ }
362
370
  const imports = [];
363
- if (acc.routeNodes.some((n) => n.isVirtual)) {
371
+ if (virtualRouteNodes.length > 0) {
364
372
  imports.push({
365
373
  specifiers: [{ imported: "createFileRoute" }],
366
374
  source: this.targetTemplate.fullPkg
367
375
  });
368
376
  }
369
- const hasComponentPieces = sortedRouteNodes.some(
370
- (node) => acc.routePiecesByPath[node.routePath]?.component || acc.routePiecesByPath[node.routePath]?.errorComponent || acc.routePiecesByPath[node.routePath]?.notFoundComponent || acc.routePiecesByPath[node.routePath]?.pendingComponent
371
- );
372
- const hasLoaderPieces = sortedRouteNodes.some(
373
- (node) => acc.routePiecesByPath[node.routePath]?.loader
374
- );
377
+ let hasComponentPieces = false;
378
+ let hasLoaderPieces = false;
379
+ for (const node of sortedRouteNodes) {
380
+ const pieces = acc.routePiecesByPath[node.routePath];
381
+ if (pieces) {
382
+ if (pieces.component || pieces.errorComponent || pieces.notFoundComponent || pieces.pendingComponent) {
383
+ hasComponentPieces = true;
384
+ }
385
+ if (pieces.loader) {
386
+ hasLoaderPieces = true;
387
+ }
388
+ if (hasComponentPieces && hasLoaderPieces) break;
389
+ }
390
+ }
375
391
  if (hasComponentPieces || hasLoaderPieces) {
376
392
  const runtimeImport = {
377
393
  specifiers: [],
@@ -391,14 +407,23 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
391
407
  source: this.targetTemplate.fullPkg,
392
408
  importKind: "type"
393
409
  };
394
- if (sortedRouteNodes.some(
395
- (d) => utils.isRouteNodeValidForAugmentation(d) && d._fsRouteType !== "lazy"
396
- )) {
410
+ let needsCreateFileRoute = false;
411
+ let needsCreateLazyFileRoute = false;
412
+ for (const node of sortedRouteNodes) {
413
+ if (utils.isRouteNodeValidForAugmentation(node)) {
414
+ if (node._fsRouteType !== "lazy") {
415
+ needsCreateFileRoute = true;
416
+ }
417
+ if (acc.routePiecesByPath[node.routePath]?.lazy) {
418
+ needsCreateLazyFileRoute = true;
419
+ }
420
+ }
421
+ if (needsCreateFileRoute && needsCreateLazyFileRoute) break;
422
+ }
423
+ if (needsCreateFileRoute) {
397
424
  typeImport.specifiers.push({ imported: "CreateFileRoute" });
398
425
  }
399
- if (sortedRouteNodes.some(
400
- (node) => acc.routePiecesByPath[node.routePath]?.lazy && utils.isRouteNodeValidForAugmentation(node)
401
- )) {
426
+ if (needsCreateLazyFileRoute) {
402
427
  typeImport.specifiers.push({ imported: "CreateLazyFileRoute" });
403
428
  }
404
429
  if (typeImport.specifiers.length > 0) {
@@ -411,12 +436,13 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
411
436
  config.disableTypes
412
437
  );
413
438
  const createUpdateRoutes = sortedRouteNodes.map((node) => {
414
- const loaderNode = acc.routePiecesByPath[node.routePath]?.loader;
415
- const componentNode = acc.routePiecesByPath[node.routePath]?.component;
416
- const errorComponentNode = acc.routePiecesByPath[node.routePath]?.errorComponent;
417
- const notFoundComponentNode = acc.routePiecesByPath[node.routePath]?.notFoundComponent;
418
- const pendingComponentNode = acc.routePiecesByPath[node.routePath]?.pendingComponent;
419
- const lazyComponentNode = acc.routePiecesByPath[node.routePath]?.lazy;
439
+ const pieces = acc.routePiecesByPath[node.routePath];
440
+ const loaderNode = pieces?.loader;
441
+ const componentNode = pieces?.component;
442
+ const errorComponentNode = pieces?.errorComponent;
443
+ const notFoundComponentNode = pieces?.notFoundComponent;
444
+ const pendingComponentNode = pieces?.pendingComponent;
445
+ const lazyComponentNode = pieces?.lazy;
420
446
  return [
421
447
  [
422
448
  `const ${node.variableName}Route = ${node.variableName}RouteImport.update({
@@ -492,10 +518,11 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
492
518
  ].join("\n\n");
493
519
  });
494
520
  const rootRoutePath = `/${rootPathId.rootPathId}`;
495
- const rootComponentNode = acc.routePiecesByPath[rootRoutePath]?.component;
496
- const rootErrorComponentNode = acc.routePiecesByPath[rootRoutePath]?.errorComponent;
497
- const rootNotFoundComponentNode = acc.routePiecesByPath[rootRoutePath]?.notFoundComponent;
498
- const rootPendingComponentNode = acc.routePiecesByPath[rootRoutePath]?.pendingComponent;
521
+ const rootPieces = acc.routePiecesByPath[rootRoutePath];
522
+ const rootComponentNode = rootPieces?.component;
523
+ const rootErrorComponentNode = rootPieces?.errorComponent;
524
+ const rootNotFoundComponentNode = rootPieces?.notFoundComponent;
525
+ const rootPendingComponentNode = rootPieces?.pendingComponent;
499
526
  let rootRouteUpdate = "";
500
527
  if (rootComponentNode || rootErrorComponentNode || rootNotFoundComponentNode || rootPendingComponentNode) {
501
528
  rootRouteUpdate = `const rootRouteWithChildren = rootRouteImport${rootComponentNode || rootErrorComponentNode || rootNotFoundComponentNode || rootPendingComponentNode ? `.update({
@@ -529,29 +556,35 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId.rootPathId}.${this
529
556
  let fileRoutesByPathInterface = "";
530
557
  let fileRoutesByFullPath = "";
531
558
  if (!config.disableTypes) {
559
+ const routeNodesByFullPath = utils.createRouteNodesByFullPath(
560
+ acc.routeNodes,
561
+ config
562
+ );
563
+ const routeNodesByTo = utils.createRouteNodesByTo(acc.routeNodes, config);
564
+ const routeNodesById = utils.createRouteNodesById(acc.routeNodes);
532
565
  fileRoutesByFullPath = [
533
566
  `export interface FileRoutesByFullPath {
534
- ${[...utils.createRouteNodesByFullPath(acc.routeNodes, config).entries()].filter(([fullPath]) => fullPath).map(([fullPath, routeNode]) => {
567
+ ${[...routeNodesByFullPath.entries()].filter(([fullPath]) => fullPath).map(([fullPath, routeNode]) => {
535
568
  return `'${fullPath}': typeof ${utils.getResolvedRouteNodeVariableName(routeNode)}`;
536
569
  })}
537
570
  }`,
538
571
  `export interface FileRoutesByTo {
539
- ${[...utils.createRouteNodesByTo(acc.routeNodes, config).entries()].filter(([to]) => to).map(([to, routeNode]) => {
572
+ ${[...routeNodesByTo.entries()].filter(([to]) => to).map(([to, routeNode]) => {
540
573
  return `'${to}': typeof ${utils.getResolvedRouteNodeVariableName(routeNode)}`;
541
574
  })}
542
575
  }`,
543
576
  `export interface FileRoutesById {
544
577
  '${routerCore.rootRouteId}': typeof rootRouteImport,
545
- ${[...utils.createRouteNodesById(acc.routeNodes).entries()].map(([id, routeNode]) => {
578
+ ${[...routeNodesById.entries()].map(([id, routeNode]) => {
546
579
  return `'${id}': typeof ${utils.getResolvedRouteNodeVariableName(routeNode)}`;
547
580
  })}
548
581
  }`,
549
582
  `export interface FileRouteTypes {
550
583
  fileRoutesByFullPath: FileRoutesByFullPath
551
- fullPaths: ${acc.routeNodes.length > 0 ? [...utils.createRouteNodesByFullPath(acc.routeNodes, config).keys()].filter((fullPath) => fullPath).map((fullPath) => `'${fullPath}'`).join("|") : "never"}
584
+ fullPaths: ${acc.routeNodes.length > 0 ? [...routeNodesByFullPath.keys()].filter((fullPath) => fullPath).map((fullPath) => `'${fullPath}'`).join("|") : "never"}
552
585
  fileRoutesByTo: FileRoutesByTo
553
- to: ${acc.routeNodes.length > 0 ? [...utils.createRouteNodesByTo(acc.routeNodes, config).keys()].filter((to) => to).map((to) => `'${to}'`).join("|") : "never"}
554
- id: ${[`'${routerCore.rootRouteId}'`, ...[...utils.createRouteNodesById(acc.routeNodes).keys()].map((id) => `'${id}'`)].join("|")}
586
+ to: ${acc.routeNodes.length > 0 ? [...routeNodesByTo.keys()].filter((to) => to).map((to) => `'${to}'`).join("|") : "never"}
587
+ id: ${[`'${routerCore.rootRouteId}'`, ...[...routeNodesById.keys()].map((id) => `'${id}'`)].join("|")}
555
588
  fileRoutesById: FileRoutesById
556
589
  }`,
557
590
  `export interface RootRouteChildren {
@@ -915,11 +948,10 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${utils.getRe
915
948
  await this.runPromise;
916
949
  return this.crawlingResult;
917
950
  }
918
- static handleNode(node, acc, config) {
951
+ static handleNode(node, acc, prefixMap, config) {
919
952
  const useExperimentalNonNestedRoutes = config?.experimental?.nonNestedRoutes ?? false;
920
- utils.resetRegex(this.routeGroupPatternRegex);
921
953
  const parentRoute = utils.hasParentRoute(
922
- acc.routeNodes,
954
+ prefixMap,
923
955
  node,
924
956
  node.routePath,
925
957
  node.originalRoutePath
@@ -959,6 +991,7 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${utils.getRe
959
991
  _fsRouteType: "static"
960
992
  },
961
993
  acc,
994
+ prefixMap,
962
995
  config
963
996
  );
964
997
  }
@@ -972,9 +1005,10 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${utils.getRe
972
1005
  const candidate = acc.routeNodesByPath.get(searchPath);
973
1006
  if (candidate && !candidate.isVirtual && candidate.path !== "/") {
974
1007
  node.parent = candidate;
975
- node.path = node.routePath;
1008
+ node.path = node.routePath?.replace(candidate.routePath ?? "", "") || "/";
1009
+ const pathRelativeToParent = immediateParentPath.replace(candidate.routePath ?? "", "") || "/";
976
1010
  node.cleanedPath = utils.removeGroups(
977
- utils.removeUnderscores(utils.removeLayoutSegments(immediateParentPath)) ?? ""
1011
+ utils.removeUnderscores(utils.removeLayoutSegments(pathRelativeToParent)) ?? ""
978
1012
  );
979
1013
  break;
980
1014
  }
@@ -1016,7 +1050,8 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${utils.getRe
1016
1050
  return false;
1017
1051
  }
1018
1052
  };
1019
- _Generator.routeGroupPatternRegex = /\(.+\)/g;
1053
+ _Generator.routeGroupPatternRegex = /\(.+\)/;
1054
+ _Generator.componentPieceRegex = /[./](component|errorComponent|notFoundComponent|pendingComponent|loader|lazy)[.]/;
1020
1055
  let Generator = _Generator;
1021
1056
  exports.Generator = Generator;
1022
1057
  //# sourceMappingURL=generator.cjs.map