@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.
@@ -7,7 +7,7 @@ import { logging } from "./logger.js";
7
7
  import { getRouteNodes as getRouteNodes$1, isVirtualConfigFile } from "./filesystem/physical/getRouteNodes.js";
8
8
  import { getRouteNodes } from "./filesystem/virtual/getRouteNodes.js";
9
9
  import { rootPathId } from "./filesystem/physical/rootPathId.js";
10
- import { multiSortBy, format, getImportForRouteNode, isRouteNodeValidForAugmentation, buildRouteTreeConfig, findParent, replaceBackslash, removeExt, createRouteNodesByFullPath, createRouteNodesByTo, createRouteNodesById, getResolvedRouteNodeVariableName, buildFileRoutesByPathInterface, checkRouteFullPathUniqueness, mergeImportDeclarations, buildImportString, checkFileExists, resetRegex, hasParentRoute, determineNodePath, trimPathLeft, removeGroups, removeLeadingUnderscores, removeLayoutSegments, removeUnderscores, removeTrailingSlash, removeLastSegmentFromPath, getImportPath } from "./utils.js";
10
+ import { multiSortBy, RoutePrefixMap, format, getImportForRouteNode, isRouteNodeValidForAugmentation, buildRouteTreeConfig, findParent, replaceBackslash, removeExt, createRouteNodesByFullPath, createRouteNodesByTo, createRouteNodesById, getResolvedRouteNodeVariableName, buildFileRoutesByPathInterface, checkRouteFullPathUniqueness, mergeImportDeclarations, buildImportString, checkFileExists, hasParentRoute, determineNodePath, trimPathLeft, removeGroups, removeLeadingUnderscores, removeLayoutSegments, removeUnderscores, removeTrailingSlash, removeLastSegmentFromPath, getImportPath } from "./utils.js";
11
11
  import { getTargetTemplate, fillTemplate } from "./template.js";
12
12
  import { transform } from "./transform/transform.js";
13
13
  const DefaultFileSystem = {
@@ -63,6 +63,8 @@ const _Generator = class _Generator {
63
63
  this.targetTemplate = getTargetTemplate(this.config);
64
64
  this.routesDirectoryPath = this.getRoutesDirectoryPath();
65
65
  this.plugins.push(...opts.config.plugins || []);
66
+ this.indexTokenRegex = new RegExp(`[./]${this.config.indexToken}[.]`);
67
+ this.routeTokenRegex = new RegExp(`[./]${this.config.routeToken}[.]`);
66
68
  for (const plugin of this.plugins) {
67
69
  plugin.init?.({ generator: this });
68
70
  }
@@ -175,11 +177,9 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
175
177
  const preRouteNodes = multiSortBy(beforeRouteNodes, [
176
178
  (d) => d.routePath === "/" ? -1 : 1,
177
179
  (d) => d.routePath?.split("/").length,
178
- (d) => d.filePath.match(new RegExp(`[./]${this.config.indexToken}[.]`)) ? 1 : -1,
179
- (d) => d.filePath.match(
180
- /[./](component|errorComponent|notFoundComponent|pendingComponent|loader|lazy)[.]/
181
- ) ? 1 : -1,
182
- (d) => d.filePath.match(new RegExp(`[./]${this.config.routeToken}[.]`)) ? -1 : 1,
180
+ (d) => d.filePath.match(this.indexTokenRegex) ? 1 : -1,
181
+ (d) => d.filePath.match(_Generator.componentPieceRegex) ? 1 : -1,
182
+ (d) => d.filePath.match(this.routeTokenRegex) ? -1 : 1,
183
183
  (d) => d.routePath?.endsWith("/") ? -1 : 1,
184
184
  (d) => d.routePath
185
185
  ]).filter((d) => {
@@ -220,8 +220,9 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
220
220
  routePiecesByPath: {},
221
221
  routeNodesByPath: /* @__PURE__ */ new Map()
222
222
  };
223
+ const prefixMap = new RoutePrefixMap(routeFileResult);
223
224
  for (const node of routeFileResult) {
224
- _Generator.handleNode(node, acc, this.config);
225
+ _Generator.handleNode(node, acc, prefixMap, this.config);
225
226
  }
226
227
  this.crawlingResult = { rootRouteNode, routeFileResult, acc };
227
228
  if (!this.routeTreeFileCache) {
@@ -329,30 +330,45 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
329
330
  (d) => d.routePath?.endsWith(config.indexToken) ? -1 : 1,
330
331
  (d) => d
331
332
  ]);
332
- const routeImports = sortedRouteNodes.filter((d) => !d.isVirtual).flatMap(
333
- (node) => getImportForRouteNode(
334
- node,
335
- config,
336
- this.generatedRouteTreePath,
337
- this.root
338
- )
339
- );
340
- const virtualRouteNodes = sortedRouteNodes.filter((d) => d.isVirtual).map((node) => {
341
- return `const ${node.variableName}RouteImport = createFileRoute('${node.routePath}')()`;
342
- });
333
+ const routeImports = [];
334
+ const virtualRouteNodes = [];
335
+ for (const node of sortedRouteNodes) {
336
+ if (node.isVirtual) {
337
+ virtualRouteNodes.push(
338
+ `const ${node.variableName}RouteImport = createFileRoute('${node.routePath}')()`
339
+ );
340
+ } else {
341
+ routeImports.push(
342
+ getImportForRouteNode(
343
+ node,
344
+ config,
345
+ this.generatedRouteTreePath,
346
+ this.root
347
+ )
348
+ );
349
+ }
350
+ }
343
351
  const imports = [];
344
- if (acc.routeNodes.some((n) => n.isVirtual)) {
352
+ if (virtualRouteNodes.length > 0) {
345
353
  imports.push({
346
354
  specifiers: [{ imported: "createFileRoute" }],
347
355
  source: this.targetTemplate.fullPkg
348
356
  });
349
357
  }
350
- const hasComponentPieces = sortedRouteNodes.some(
351
- (node) => acc.routePiecesByPath[node.routePath]?.component || acc.routePiecesByPath[node.routePath]?.errorComponent || acc.routePiecesByPath[node.routePath]?.notFoundComponent || acc.routePiecesByPath[node.routePath]?.pendingComponent
352
- );
353
- const hasLoaderPieces = sortedRouteNodes.some(
354
- (node) => acc.routePiecesByPath[node.routePath]?.loader
355
- );
358
+ let hasComponentPieces = false;
359
+ let hasLoaderPieces = false;
360
+ for (const node of sortedRouteNodes) {
361
+ const pieces = acc.routePiecesByPath[node.routePath];
362
+ if (pieces) {
363
+ if (pieces.component || pieces.errorComponent || pieces.notFoundComponent || pieces.pendingComponent) {
364
+ hasComponentPieces = true;
365
+ }
366
+ if (pieces.loader) {
367
+ hasLoaderPieces = true;
368
+ }
369
+ if (hasComponentPieces && hasLoaderPieces) break;
370
+ }
371
+ }
356
372
  if (hasComponentPieces || hasLoaderPieces) {
357
373
  const runtimeImport = {
358
374
  specifiers: [],
@@ -372,14 +388,23 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
372
388
  source: this.targetTemplate.fullPkg,
373
389
  importKind: "type"
374
390
  };
375
- if (sortedRouteNodes.some(
376
- (d) => isRouteNodeValidForAugmentation(d) && d._fsRouteType !== "lazy"
377
- )) {
391
+ let needsCreateFileRoute = false;
392
+ let needsCreateLazyFileRoute = false;
393
+ for (const node of sortedRouteNodes) {
394
+ if (isRouteNodeValidForAugmentation(node)) {
395
+ if (node._fsRouteType !== "lazy") {
396
+ needsCreateFileRoute = true;
397
+ }
398
+ if (acc.routePiecesByPath[node.routePath]?.lazy) {
399
+ needsCreateLazyFileRoute = true;
400
+ }
401
+ }
402
+ if (needsCreateFileRoute && needsCreateLazyFileRoute) break;
403
+ }
404
+ if (needsCreateFileRoute) {
378
405
  typeImport.specifiers.push({ imported: "CreateFileRoute" });
379
406
  }
380
- if (sortedRouteNodes.some(
381
- (node) => acc.routePiecesByPath[node.routePath]?.lazy && isRouteNodeValidForAugmentation(node)
382
- )) {
407
+ if (needsCreateLazyFileRoute) {
383
408
  typeImport.specifiers.push({ imported: "CreateLazyFileRoute" });
384
409
  }
385
410
  if (typeImport.specifiers.length > 0) {
@@ -392,12 +417,13 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
392
417
  config.disableTypes
393
418
  );
394
419
  const createUpdateRoutes = sortedRouteNodes.map((node) => {
395
- const loaderNode = acc.routePiecesByPath[node.routePath]?.loader;
396
- const componentNode = acc.routePiecesByPath[node.routePath]?.component;
397
- const errorComponentNode = acc.routePiecesByPath[node.routePath]?.errorComponent;
398
- const notFoundComponentNode = acc.routePiecesByPath[node.routePath]?.notFoundComponent;
399
- const pendingComponentNode = acc.routePiecesByPath[node.routePath]?.pendingComponent;
400
- const lazyComponentNode = acc.routePiecesByPath[node.routePath]?.lazy;
420
+ const pieces = acc.routePiecesByPath[node.routePath];
421
+ const loaderNode = pieces?.loader;
422
+ const componentNode = pieces?.component;
423
+ const errorComponentNode = pieces?.errorComponent;
424
+ const notFoundComponentNode = pieces?.notFoundComponent;
425
+ const pendingComponentNode = pieces?.pendingComponent;
426
+ const lazyComponentNode = pieces?.lazy;
401
427
  return [
402
428
  [
403
429
  `const ${node.variableName}Route = ${node.variableName}RouteImport.update({
@@ -473,10 +499,11 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
473
499
  ].join("\n\n");
474
500
  });
475
501
  const rootRoutePath = `/${rootPathId}`;
476
- const rootComponentNode = acc.routePiecesByPath[rootRoutePath]?.component;
477
- const rootErrorComponentNode = acc.routePiecesByPath[rootRoutePath]?.errorComponent;
478
- const rootNotFoundComponentNode = acc.routePiecesByPath[rootRoutePath]?.notFoundComponent;
479
- const rootPendingComponentNode = acc.routePiecesByPath[rootRoutePath]?.pendingComponent;
502
+ const rootPieces = acc.routePiecesByPath[rootRoutePath];
503
+ const rootComponentNode = rootPieces?.component;
504
+ const rootErrorComponentNode = rootPieces?.errorComponent;
505
+ const rootNotFoundComponentNode = rootPieces?.notFoundComponent;
506
+ const rootPendingComponentNode = rootPieces?.pendingComponent;
480
507
  let rootRouteUpdate = "";
481
508
  if (rootComponentNode || rootErrorComponentNode || rootNotFoundComponentNode || rootPendingComponentNode) {
482
509
  rootRouteUpdate = `const rootRouteWithChildren = rootRouteImport${rootComponentNode || rootErrorComponentNode || rootNotFoundComponentNode || rootPendingComponentNode ? `.update({
@@ -510,29 +537,35 @@ Add the file in: "${this.config.routesDirectory}/${rootPathId}.${this.config.dis
510
537
  let fileRoutesByPathInterface = "";
511
538
  let fileRoutesByFullPath = "";
512
539
  if (!config.disableTypes) {
540
+ const routeNodesByFullPath = createRouteNodesByFullPath(
541
+ acc.routeNodes,
542
+ config
543
+ );
544
+ const routeNodesByTo = createRouteNodesByTo(acc.routeNodes, config);
545
+ const routeNodesById = createRouteNodesById(acc.routeNodes);
513
546
  fileRoutesByFullPath = [
514
547
  `export interface FileRoutesByFullPath {
515
- ${[...createRouteNodesByFullPath(acc.routeNodes, config).entries()].filter(([fullPath]) => fullPath).map(([fullPath, routeNode]) => {
548
+ ${[...routeNodesByFullPath.entries()].filter(([fullPath]) => fullPath).map(([fullPath, routeNode]) => {
516
549
  return `'${fullPath}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
517
550
  })}
518
551
  }`,
519
552
  `export interface FileRoutesByTo {
520
- ${[...createRouteNodesByTo(acc.routeNodes, config).entries()].filter(([to]) => to).map(([to, routeNode]) => {
553
+ ${[...routeNodesByTo.entries()].filter(([to]) => to).map(([to, routeNode]) => {
521
554
  return `'${to}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
522
555
  })}
523
556
  }`,
524
557
  `export interface FileRoutesById {
525
558
  '${rootRouteId}': typeof rootRouteImport,
526
- ${[...createRouteNodesById(acc.routeNodes).entries()].map(([id, routeNode]) => {
559
+ ${[...routeNodesById.entries()].map(([id, routeNode]) => {
527
560
  return `'${id}': typeof ${getResolvedRouteNodeVariableName(routeNode)}`;
528
561
  })}
529
562
  }`,
530
563
  `export interface FileRouteTypes {
531
564
  fileRoutesByFullPath: FileRoutesByFullPath
532
- fullPaths: ${acc.routeNodes.length > 0 ? [...createRouteNodesByFullPath(acc.routeNodes, config).keys()].filter((fullPath) => fullPath).map((fullPath) => `'${fullPath}'`).join("|") : "never"}
565
+ fullPaths: ${acc.routeNodes.length > 0 ? [...routeNodesByFullPath.keys()].filter((fullPath) => fullPath).map((fullPath) => `'${fullPath}'`).join("|") : "never"}
533
566
  fileRoutesByTo: FileRoutesByTo
534
- to: ${acc.routeNodes.length > 0 ? [...createRouteNodesByTo(acc.routeNodes, config).keys()].filter((to) => to).map((to) => `'${to}'`).join("|") : "never"}
535
- id: ${[`'${rootRouteId}'`, ...[...createRouteNodesById(acc.routeNodes).keys()].map((id) => `'${id}'`)].join("|")}
567
+ to: ${acc.routeNodes.length > 0 ? [...routeNodesByTo.keys()].filter((to) => to).map((to) => `'${to}'`).join("|") : "never"}
568
+ id: ${[`'${rootRouteId}'`, ...[...routeNodesById.keys()].map((id) => `'${id}'`)].join("|")}
536
569
  fileRoutesById: FileRoutesById
537
570
  }`,
538
571
  `export interface RootRouteChildren {
@@ -896,11 +929,10 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved
896
929
  await this.runPromise;
897
930
  return this.crawlingResult;
898
931
  }
899
- static handleNode(node, acc, config) {
932
+ static handleNode(node, acc, prefixMap, config) {
900
933
  const useExperimentalNonNestedRoutes = config?.experimental?.nonNestedRoutes ?? false;
901
- resetRegex(this.routeGroupPatternRegex);
902
934
  const parentRoute = hasParentRoute(
903
- acc.routeNodes,
935
+ prefixMap,
904
936
  node,
905
937
  node.routePath,
906
938
  node.originalRoutePath
@@ -940,6 +972,7 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved
940
972
  _fsRouteType: "static"
941
973
  },
942
974
  acc,
975
+ prefixMap,
943
976
  config
944
977
  );
945
978
  }
@@ -953,9 +986,10 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved
953
986
  const candidate = acc.routeNodesByPath.get(searchPath);
954
987
  if (candidate && !candidate.isVirtual && candidate.path !== "/") {
955
988
  node.parent = candidate;
956
- node.path = node.routePath;
989
+ node.path = node.routePath?.replace(candidate.routePath ?? "", "") || "/";
990
+ const pathRelativeToParent = immediateParentPath.replace(candidate.routePath ?? "", "") || "/";
957
991
  node.cleanedPath = removeGroups(
958
- removeUnderscores(removeLayoutSegments(immediateParentPath)) ?? ""
992
+ removeUnderscores(removeLayoutSegments(pathRelativeToParent)) ?? ""
959
993
  );
960
994
  break;
961
995
  }
@@ -997,7 +1031,8 @@ ${acc.routeTree.map((child) => `${child.variableName}Route: typeof ${getResolved
997
1031
  return false;
998
1032
  }
999
1033
  };
1000
- _Generator.routeGroupPatternRegex = /\(.+\)/g;
1034
+ _Generator.routeGroupPatternRegex = /\(.+\)/;
1035
+ _Generator.componentPieceRegex = /[./](component|errorComponent|notFoundComponent|pendingComponent|loader|lazy)[.]/;
1001
1036
  let Generator = _Generator;
1002
1037
  export {
1003
1038
  Generator