@tanstack/virtual-file-routes 1.161.6 → 1.161.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.
- package/bin/intent.js +25 -0
- package/package.json +9 -2
- package/skills/virtual-file-routes/SKILL.md +218 -0
package/bin/intent.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Auto-generated by @tanstack/intent setup
|
|
3
|
+
// Exposes the intent end-user CLI for consumers of this library.
|
|
4
|
+
// Commit this file, then add to your package.json:
|
|
5
|
+
// "bin": { "intent": "./bin/intent.js" }
|
|
6
|
+
try {
|
|
7
|
+
await import('@tanstack/intent/intent-library')
|
|
8
|
+
} catch (e) {
|
|
9
|
+
const isModuleNotFound =
|
|
10
|
+
e?.code === 'ERR_MODULE_NOT_FOUND' || e?.code === 'MODULE_NOT_FOUND'
|
|
11
|
+
const missingIntentLibrary =
|
|
12
|
+
typeof e?.message === 'string' && e.message.includes('@tanstack/intent')
|
|
13
|
+
|
|
14
|
+
if (isModuleNotFound && missingIntentLibrary) {
|
|
15
|
+
console.error('@tanstack/intent is not installed.')
|
|
16
|
+
console.error('')
|
|
17
|
+
console.error('Install it as a dev dependency:')
|
|
18
|
+
console.error(' npm add -D @tanstack/intent')
|
|
19
|
+
console.error('')
|
|
20
|
+
console.error('Or run directly:')
|
|
21
|
+
console.error(' npx @tanstack/intent@latest list')
|
|
22
|
+
process.exit(1)
|
|
23
|
+
}
|
|
24
|
+
throw e
|
|
25
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/virtual-file-routes",
|
|
3
|
-
"version": "1.161.
|
|
3
|
+
"version": "1.161.7",
|
|
4
4
|
"description": "Modern and scalable routing for React applications",
|
|
5
5
|
"author": "Tanner Linsley",
|
|
6
6
|
"license": "MIT",
|
|
@@ -43,14 +43,21 @@
|
|
|
43
43
|
"sideEffects": false,
|
|
44
44
|
"files": [
|
|
45
45
|
"dist",
|
|
46
|
-
"src"
|
|
46
|
+
"src",
|
|
47
|
+
"skills",
|
|
48
|
+
"bin",
|
|
49
|
+
"!skills/_artifacts"
|
|
47
50
|
],
|
|
48
51
|
"engines": {
|
|
49
52
|
"node": ">=20.19"
|
|
50
53
|
},
|
|
51
54
|
"devDependencies": {
|
|
55
|
+
"@tanstack/intent": "^0.0.14",
|
|
52
56
|
"vite": "*"
|
|
53
57
|
},
|
|
58
|
+
"bin": {
|
|
59
|
+
"intent": "./bin/intent.js"
|
|
60
|
+
},
|
|
54
61
|
"scripts": {
|
|
55
62
|
"clean": "rimraf ./dist && rimraf ./coverage",
|
|
56
63
|
"test:eslint": "eslint ./src",
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: virtual-file-routes
|
|
3
|
+
description: >-
|
|
4
|
+
Programmatic route tree building as an alternative to filesystem
|
|
5
|
+
conventions: rootRoute, index, route, layout, physical,
|
|
6
|
+
defineVirtualSubtreeConfig. Use with TanStack Router plugin's
|
|
7
|
+
virtualRouteConfig option.
|
|
8
|
+
type: core
|
|
9
|
+
library: tanstack-router
|
|
10
|
+
library_version: '1.161.4'
|
|
11
|
+
sources:
|
|
12
|
+
- TanStack/router:packages/virtual-file-routes/src
|
|
13
|
+
- TanStack/router:docs/router/routing/virtual-file-routes.md
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
# Virtual File Routes (`@tanstack/virtual-file-routes`)
|
|
17
|
+
|
|
18
|
+
Build route trees programmatically instead of relying on filesystem conventions. Useful when you want explicit control over route structure, need to mix virtual and physical routes, or want to define route subtrees within file-based routing directories.
|
|
19
|
+
|
|
20
|
+
> **CRITICAL**: Types are FULLY INFERRED. Never cast, never annotate inferred values.
|
|
21
|
+
|
|
22
|
+
## Install
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @tanstack/virtual-file-routes
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## API Reference
|
|
29
|
+
|
|
30
|
+
### `rootRoute(file, children?)`
|
|
31
|
+
|
|
32
|
+
Creates the root of a virtual route tree.
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import { rootRoute, index, route } from '@tanstack/virtual-file-routes'
|
|
36
|
+
|
|
37
|
+
const routes = rootRoute('root.tsx', [
|
|
38
|
+
index('index.tsx'),
|
|
39
|
+
route('/about', 'about.tsx'),
|
|
40
|
+
])
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### `index(file)`
|
|
44
|
+
|
|
45
|
+
Creates an index route — the default rendered when the parent path matches exactly.
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { index } from '@tanstack/virtual-file-routes'
|
|
49
|
+
|
|
50
|
+
index('home.tsx')
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### `route(path, ...)`
|
|
54
|
+
|
|
55
|
+
Creates a route node. Three call signatures:
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import { route, index } from '@tanstack/virtual-file-routes'
|
|
59
|
+
|
|
60
|
+
// Leaf route: path + file
|
|
61
|
+
route('/about', 'about.tsx')
|
|
62
|
+
|
|
63
|
+
// Branch route: path + file + children
|
|
64
|
+
route('/dashboard', 'dashboard.tsx', [
|
|
65
|
+
index('dashboard-index.tsx'),
|
|
66
|
+
route('/settings', 'settings.tsx'),
|
|
67
|
+
])
|
|
68
|
+
|
|
69
|
+
// Path prefix only (no file): groups children under a URL segment
|
|
70
|
+
route('/api', [route('/users', 'users.tsx'), route('/posts', 'posts.tsx')])
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### `layout(file, children)` or `layout(id, file, children)`
|
|
74
|
+
|
|
75
|
+
Creates a pathless layout route — wraps children without adding a URL segment.
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
import { layout, route, index } from '@tanstack/virtual-file-routes'
|
|
79
|
+
|
|
80
|
+
// ID derived from filename
|
|
81
|
+
layout('authLayout.tsx', [
|
|
82
|
+
route('/dashboard', 'dashboard.tsx'),
|
|
83
|
+
route('/settings', 'settings.tsx'),
|
|
84
|
+
])
|
|
85
|
+
|
|
86
|
+
// Explicit ID
|
|
87
|
+
layout('admin-layout', 'adminLayout.tsx', [route('/admin', 'admin.tsx')])
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### `physical(pathPrefix, directory)` or `physical(directory)`
|
|
91
|
+
|
|
92
|
+
Mounts a directory of file-based routes at a URL prefix. Uses TanStack Router's standard file-based routing conventions within that directory.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
import { physical } from '@tanstack/virtual-file-routes'
|
|
96
|
+
|
|
97
|
+
// Mount posts/ directory under /posts
|
|
98
|
+
physical('/posts', 'posts')
|
|
99
|
+
|
|
100
|
+
// Merge features/ directory at the current level
|
|
101
|
+
physical('features')
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### `defineVirtualSubtreeConfig(config)`
|
|
105
|
+
|
|
106
|
+
Type helper for `__virtual.ts` files inside file-based routing directories. Identity function that provides type inference.
|
|
107
|
+
|
|
108
|
+
```ts
|
|
109
|
+
// src/routes/admin/__virtual.ts
|
|
110
|
+
import {
|
|
111
|
+
defineVirtualSubtreeConfig,
|
|
112
|
+
index,
|
|
113
|
+
route,
|
|
114
|
+
} from '@tanstack/virtual-file-routes'
|
|
115
|
+
|
|
116
|
+
export default defineVirtualSubtreeConfig([
|
|
117
|
+
index('home.tsx'),
|
|
118
|
+
route('$id', 'details.tsx'),
|
|
119
|
+
])
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Integration with Router Plugin
|
|
123
|
+
|
|
124
|
+
Pass the virtual route config to the TanStack Router plugin:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// vite.config.ts
|
|
128
|
+
import { defineConfig } from 'vite'
|
|
129
|
+
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
130
|
+
import { routes } from './routes'
|
|
131
|
+
|
|
132
|
+
export default defineConfig({
|
|
133
|
+
plugins: [
|
|
134
|
+
tanstackRouter({
|
|
135
|
+
target: 'react', // or 'solid', 'vue'
|
|
136
|
+
virtualRouteConfig: routes,
|
|
137
|
+
}),
|
|
138
|
+
// Add your framework's Vite plugin here
|
|
139
|
+
],
|
|
140
|
+
})
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Or reference a file path:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
tanstackRouter({
|
|
147
|
+
target: 'react',
|
|
148
|
+
virtualRouteConfig: './routes.ts',
|
|
149
|
+
})
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Full Example
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
// routes.ts
|
|
156
|
+
import {
|
|
157
|
+
rootRoute,
|
|
158
|
+
route,
|
|
159
|
+
index,
|
|
160
|
+
layout,
|
|
161
|
+
physical,
|
|
162
|
+
} from '@tanstack/virtual-file-routes'
|
|
163
|
+
|
|
164
|
+
export const routes = rootRoute('root.tsx', [
|
|
165
|
+
index('index.tsx'),
|
|
166
|
+
|
|
167
|
+
layout('authLayout.tsx', [
|
|
168
|
+
route('/dashboard', 'app/dashboard.tsx', [
|
|
169
|
+
index('app/dashboard-index.tsx'),
|
|
170
|
+
route('/invoices', 'app/dashboard-invoices.tsx', [
|
|
171
|
+
index('app/invoices-index.tsx'),
|
|
172
|
+
route('$id', 'app/invoice-detail.tsx'),
|
|
173
|
+
]),
|
|
174
|
+
]),
|
|
175
|
+
]),
|
|
176
|
+
|
|
177
|
+
// Mount file-based routing from posts/ directory
|
|
178
|
+
physical('/posts', 'posts'),
|
|
179
|
+
])
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Common Mistakes
|
|
183
|
+
|
|
184
|
+
### 1. HIGH: Forgetting that file paths are relative to routesDirectory
|
|
185
|
+
|
|
186
|
+
File paths in `rootRoute`, `index`, `route`, and `layout` are relative to the `routesDirectory` configured in the router plugin (default: `./src/routes`). Do not use absolute paths or paths relative to the project root.
|
|
187
|
+
|
|
188
|
+
```ts
|
|
189
|
+
// WRONG — absolute path
|
|
190
|
+
route('/about', '/src/routes/about.tsx')
|
|
191
|
+
|
|
192
|
+
// CORRECT — relative to routesDirectory
|
|
193
|
+
route('/about', 'about.tsx')
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### 2. MEDIUM: Using physical() without matching directory structure
|
|
197
|
+
|
|
198
|
+
The directory passed to `physical()` must exist inside `routesDirectory` and follow TanStack Router's file-based routing conventions.
|
|
199
|
+
|
|
200
|
+
```ts
|
|
201
|
+
// WRONG — directory doesn't exist or wrong location
|
|
202
|
+
physical('/blog', 'src/blog')
|
|
203
|
+
|
|
204
|
+
// CORRECT — relative to routesDirectory
|
|
205
|
+
physical('/blog', 'blog')
|
|
206
|
+
// Expects: src/routes/blog/ (with route files inside)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### 3. MEDIUM: Confusing layout() with route()
|
|
210
|
+
|
|
211
|
+
`layout()` creates a pathless wrapper — it does NOT add a URL segment. Use `route()` for URL segments.
|
|
212
|
+
|
|
213
|
+
```ts
|
|
214
|
+
// This does NOT create a /dashboard URL
|
|
215
|
+
layout('dashboardLayout.tsx', [route('/dashboard', 'dashboard.tsx')])
|
|
216
|
+
|
|
217
|
+
// The URL is /dashboard, and dashboardLayout.tsx wraps it
|
|
218
|
+
```
|