@tanstack/router-generator 1.120.4-alpha.4 → 1.120.5

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.
Files changed (51) hide show
  1. package/dist/cjs/config.cjs +14 -14
  2. package/dist/cjs/config.cjs.map +1 -1
  3. package/dist/cjs/config.d.cts +31 -68
  4. package/dist/cjs/filesystem/physical/getRouteNodes.cjs +5 -1
  5. package/dist/cjs/filesystem/physical/getRouteNodes.cjs.map +1 -1
  6. package/dist/cjs/filesystem/physical/getRouteNodes.d.cts +2 -2
  7. package/dist/cjs/filesystem/virtual/getRouteNodes.cjs.map +1 -1
  8. package/dist/cjs/filesystem/virtual/getRouteNodes.d.cts +2 -2
  9. package/dist/cjs/generator.cjs +172 -160
  10. package/dist/cjs/generator.cjs.map +1 -1
  11. package/dist/cjs/generator.d.cts +59 -0
  12. package/dist/cjs/index.cjs +2 -23
  13. package/dist/cjs/index.cjs.map +1 -1
  14. package/dist/cjs/index.d.cts +4 -8
  15. package/dist/cjs/template.cjs +12 -4
  16. package/dist/cjs/template.cjs.map +1 -1
  17. package/dist/cjs/template.d.cts +1 -0
  18. package/dist/cjs/types.d.cts +1 -1
  19. package/dist/cjs/utils.cjs +2 -61
  20. package/dist/cjs/utils.cjs.map +1 -1
  21. package/dist/cjs/utils.d.cts +2 -4
  22. package/dist/esm/config.d.ts +31 -68
  23. package/dist/esm/config.js +14 -14
  24. package/dist/esm/config.js.map +1 -1
  25. package/dist/esm/filesystem/physical/getRouteNodes.d.ts +2 -2
  26. package/dist/esm/filesystem/physical/getRouteNodes.js +6 -2
  27. package/dist/esm/filesystem/physical/getRouteNodes.js.map +1 -1
  28. package/dist/esm/filesystem/virtual/getRouteNodes.d.ts +2 -2
  29. package/dist/esm/filesystem/virtual/getRouteNodes.js.map +1 -1
  30. package/dist/esm/generator.d.ts +59 -0
  31. package/dist/esm/generator.js +175 -163
  32. package/dist/esm/generator.js.map +1 -1
  33. package/dist/esm/index.d.ts +4 -8
  34. package/dist/esm/index.js +4 -25
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/template.d.ts +1 -0
  37. package/dist/esm/template.js +12 -4
  38. package/dist/esm/template.js.map +1 -1
  39. package/dist/esm/types.d.ts +1 -1
  40. package/dist/esm/utils.d.ts +2 -4
  41. package/dist/esm/utils.js +2 -61
  42. package/dist/esm/utils.js.map +1 -1
  43. package/package.json +3 -3
  44. package/src/config.ts +11 -14
  45. package/src/filesystem/physical/getRouteNodes.ts +14 -13
  46. package/src/filesystem/virtual/getRouteNodes.ts +3 -18
  47. package/src/generator.ts +221 -242
  48. package/src/index.ts +7 -32
  49. package/src/template.ts +15 -4
  50. package/src/types.ts +1 -0
  51. package/src/utils.ts +4 -85
package/src/index.ts CHANGED
@@ -1,34 +1,9 @@
1
- export {
2
- configSchema,
3
- getConfig,
4
- resolveConfigPath,
5
- baseConfigSchema,
6
- } from './config'
7
- export type { Config, BaseConfig } from './config'
8
-
9
- export { generator } from './generator'
1
+ export { configSchema, getConfig, resolveConfigPath } from './config'
2
+ export type { Config } from './config'
10
3
 
11
4
  export {
12
- logging,
13
- capitalize,
14
- cleanPath,
15
- trimPathLeft,
16
- removeLeadingSlash,
17
- removeTrailingSlash,
18
- determineInitialRoutePath,
19
- replaceBackslash,
20
- routePathToVariable,
21
- removeUnderscores,
22
- resetRegex,
23
- multiSortBy,
24
- writeIfDifferent,
25
- format,
26
- removeExt,
27
- } from './utils'
28
-
29
- export type { RouteNode, GetRouteNodesResult } from './types'
30
-
31
- export { getRouteNodes as physicalGetRouteNodes } from './filesystem/physical/getRouteNodes'
32
- export { getRouteNodes as virtualGetRouteNodes } from './filesystem/virtual/getRouteNodes'
33
-
34
- export { rootPathId } from './filesystem/physical/rootPathId'
5
+ generator,
6
+ startAPIRouteSegmentsFromTSRFilePath,
7
+ CONSTANTS,
8
+ } from './generator'
9
+ export type { StartAPIRoutePathSegment } from './generator'
package/src/template.ts CHANGED
@@ -77,8 +77,10 @@ export function getTargetTemplate(target: Config['target']): TargetTemplate {
77
77
  'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
78
78
  ].join(''),
79
79
  imports: {
80
- tsrImports: () => '',
81
- tsrExportStart: () => `export const Route = createFileRoute(`,
80
+ tsrImports: () =>
81
+ "import { createFileRoute } from '@tanstack/react-router';",
82
+ tsrExportStart: (routePath) =>
83
+ `export const Route = createFileRoute('${routePath}')(`,
82
84
  tsrExportEnd: () => ');',
83
85
  },
84
86
  },
@@ -128,8 +130,10 @@ export function getTargetTemplate(target: Config['target']): TargetTemplate {
128
130
  'function RouteComponent() { return <div>Hello "%%tsrPath%%"!</div> };\n',
129
131
  ].join(''),
130
132
  imports: {
131
- tsrImports: () => '',
132
- tsrExportStart: () => `export const Route = createFileRoute(`,
133
+ tsrImports: () =>
134
+ "import { createFileRoute } from '@tanstack/solid-router';",
135
+ tsrExportStart: (routePath) =>
136
+ `export const Route = createFileRoute('${routePath}')(`,
133
137
  tsrExportEnd: () => ');',
134
138
  },
135
139
  },
@@ -154,3 +158,10 @@ export function getTargetTemplate(target: Config['target']): TargetTemplate {
154
158
  throw new Error(`router-generator: Unknown target type: ${target}`)
155
159
  }
156
160
  }
161
+
162
+ export const defaultAPIRouteTemplate = [
163
+ 'import { json } from "@tanstack/react-start";\n',
164
+ '%%tsrImports%%',
165
+ '\n\n',
166
+ '%%tsrExportStart%%{ GET: ({ request, params }) => { return json({ message:\'Hello "%%tsrPath%%"!\' }) }}%%tsrExportEnd%%\n',
167
+ ].join('')
package/src/types.ts CHANGED
@@ -25,6 +25,7 @@ export type FsRouteType =
25
25
  | 'layout'
26
26
  | 'pathless_layout'
27
27
  | 'lazy'
28
+ | 'api'
28
29
  | 'loader' // @deprecated
29
30
  | 'component' // @deprecated
30
31
  | 'pendingComponent' // @deprecated
package/src/utils.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import * as fs from 'node:fs'
2
2
  import * as prettier from 'prettier'
3
+ import type { Config } from './config'
3
4
 
4
5
  export function multiSortBy<T>(
5
6
  arr: Array<T>,
@@ -85,55 +86,7 @@ export function removeTrailingSlash(s: string) {
85
86
  }
86
87
 
87
88
  export function determineInitialRoutePath(routePath: string) {
88
- const DISALLOWED_ESCAPE_CHARS = new Set([
89
- '/',
90
- '\\',
91
- '?',
92
- '#',
93
- ':',
94
- '*',
95
- '<',
96
- '>',
97
- '|',
98
- '!',
99
- '$',
100
- '%',
101
- ])
102
-
103
- const parts = routePath.split(/(?<!\[)\.(?!\])/g)
104
-
105
- // Escape any characters that in square brackets
106
- const escapedParts = parts.map((part) => {
107
- // Check if any disallowed characters are used in brackets
108
- const BRACKET_CONTENT_RE = /\[(.*?)\]/g
109
-
110
- let match
111
- while ((match = BRACKET_CONTENT_RE.exec(part)) !== null) {
112
- const character = match[1]
113
- if (character === undefined) continue
114
- if (DISALLOWED_ESCAPE_CHARS.has(character)) {
115
- console.error(
116
- `Error: Disallowed character "${character}" found in square brackets in route path "${routePath}".\nYou cannot use any of the following characters in square brackets: ${Array.from(
117
- DISALLOWED_ESCAPE_CHARS,
118
- ).join(', ')}\nPlease remove and/or replace them.`,
119
- )
120
- process.exit(1)
121
- }
122
- }
123
-
124
- // Since this split segment is safe at this point, we can
125
- // remove the brackets and replace them with the content inside
126
- return part.replace(/\[(.)\]/g, '$1')
127
- })
128
-
129
- // If the syntax for prefix/suffix is different, from the path
130
- // matching internals of router-core, we'd perform those changes here
131
- // on the `escapedParts` array before it is joined back together in
132
- // `final`
133
-
134
- const final = cleanPath(`/${escapedParts.join('/')}`) || ''
135
-
136
- return final
89
+ return cleanPath(`/${routePath.split('.').join('/')}`) || ''
137
90
  }
138
91
 
139
92
  export function replaceBackslash(s: string) {
@@ -141,43 +94,15 @@ export function replaceBackslash(s: string) {
141
94
  }
142
95
 
143
96
  export function routePathToVariable(routePath: string): string {
144
- const toVariableSafeChar = (char: string): string => {
145
- if (/[a-zA-Z0-9_]/.test(char)) {
146
- return char // Keep alphanumeric characters and underscores as is
147
- }
148
-
149
- // Replace special characters with meaningful text equivalents
150
- switch (char) {
151
- case '.':
152
- return 'Dot'
153
- case '-':
154
- return 'Dash'
155
- case '@':
156
- return 'At'
157
- case '(':
158
- return '' // Removed since route groups use parentheses
159
- case ')':
160
- return '' // Removed since route groups use parentheses
161
- case ' ':
162
- return '' // Remove spaces
163
- default:
164
- return `Char${char.charCodeAt(0)}` // For any other characters
165
- }
166
- }
167
-
168
97
  return (
169
98
  removeUnderscores(routePath)
170
99
  ?.replace(/\/\$\//g, '/splat/')
171
100
  .replace(/\$$/g, 'splat')
172
- .replace(/\$\{\$\}/g, 'splat')
173
101
  .replace(/\$/g, '')
174
102
  .split(/[/-]/g)
175
103
  .map((d, i) => (i > 0 ? capitalize(d) : d))
176
104
  .join('')
177
- .split('')
178
- .map(toVariableSafeChar)
179
- .join('')
180
- // .replace(/([^a-zA-Z0-9]|[.])/gm, '')
105
+ .replace(/([^a-zA-Z0-9]|[.])/gm, '')
181
106
  .replace(/^(\d)/g, 'R$1') ?? ''
182
107
  )
183
108
  }
@@ -226,13 +151,7 @@ export async function writeIfDifferent(
226
151
  * @param config The configuration object
227
152
  * @returns The formatted content
228
153
  */
229
- export async function format(
230
- source: string,
231
- config: {
232
- quoteStyle: 'single' | 'double'
233
- semicolons: boolean
234
- },
235
- ): Promise<string> {
154
+ export async function format(source: string, config: Config): Promise<string> {
236
155
  const prettierOptions: prettier.Config = {
237
156
  semi: config.semicolons,
238
157
  singleQuote: config.quoteStyle === 'single',