ts-file-router 6.0.1 â 7.0.0
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/README.md +231 -52
- package/dist/index.cjs +456 -0
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -3
- package/dist/index.js +456 -2
- package/package.json +23 -15
- package/dist/generator.d.ts +0 -3
- package/dist/generator.d.ts.map +0 -1
- package/dist/generator.js +0 -129
- package/dist/helpers/fileHelper.d.ts +0 -6
- package/dist/helpers/fileHelper.d.ts.map +0 -1
- package/dist/helpers/fileHelper.js +0 -10
- package/dist/helpers/index.d.ts +0 -3
- package/dist/helpers/index.d.ts.map +0 -1
- package/dist/helpers/index.js +0 -2
- package/dist/helpers/serializeHelper.d.ts +0 -7
- package/dist/helpers/serializeHelper.d.ts.map +0 -1
- package/dist/helpers/serializeHelper.js +0 -79
- package/dist/index.d.ts.map +0 -1
- package/dist/plugins/index.d.ts +0 -2
- package/dist/plugins/index.d.ts.map +0 -1
- package/dist/plugins/index.js +0 -1
- package/dist/plugins/vite.d.ts +0 -4
- package/dist/plugins/vite.d.ts.map +0 -1
- package/dist/plugins/vite.js +0 -17
- package/dist/types.d.ts +0 -23
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -1
package/README.md
CHANGED
|
@@ -1,19 +1,20 @@
|
|
|
1
1
|
# ðĶ ts-file-router
|
|
2
2
|
|
|
3
|
-
A lightweight
|
|
4
|
-
Automatically scans folders and outputs a clean, structured `routes.ts` tree ready for dynamic imports (e.g., `React.lazy`).
|
|
3
|
+
A lightweight **filesystem router generator** for TypeScript projects. Automatically scans your folder structure and generates a clean, typed `routes.ts` tree ready for dynamic imports (e.g., `React.lazy`).
|
|
5
4
|
|
|
6
5
|
---
|
|
7
6
|
|
|
8
7
|
## âĻ Features
|
|
9
8
|
|
|
10
|
-
- ð
|
|
11
|
-
- ð
|
|
12
|
-
- âïļ Works
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
9
|
+
- ð **Automatic folder scanning** - Automatically scans your configured directory to build a complete route tree manifest.
|
|
10
|
+
- ð **Generated TypeScript routes** - Fully typed `routes.ts` file with `as const` assertions
|
|
11
|
+
- âïļ **Framework agnostic** - Works with React.lazy, Vue, Solid, or any framework with dynamic imports
|
|
12
|
+
- ðŠķ **Zero runtime dependencies** - The generated routes file is plain TypeScript/JavaScript; your final bundle won't include any extra library code.
|
|
13
|
+
- ð **File watcher support** - Auto-regenerate routes when files change (powered by Chokidar)
|
|
14
|
+
- ðĻ **Biome formatting** - Output files are automatically formatted with Biome
|
|
15
|
+
- ð§Đ **Vite plugin** - Seamless integration with Vite dev server
|
|
16
|
+
- ðŦ **Smart file filtering** - Ignores `index` files, `_` prefixed files (private routes), and output files
|
|
17
|
+
- ð **Full TypeScript support** - Complete `.d.ts` definitions included
|
|
17
18
|
|
|
18
19
|
---
|
|
19
20
|
|
|
@@ -21,85 +22,263 @@ Automatically scans folders and outputs a clean, structured `routes.ts` tree rea
|
|
|
21
22
|
|
|
22
23
|
```bash
|
|
23
24
|
npm install ts-file-router
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
**Optional dependencies:**
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# For file watcher support
|
|
31
|
+
npm install chokidar
|
|
26
32
|
|
|
33
|
+
# Vite is automatically supported as a peer dependency
|
|
27
34
|
```
|
|
28
35
|
|
|
29
36
|
---
|
|
30
37
|
|
|
31
|
-
##
|
|
38
|
+
## ð Quick Start
|
|
39
|
+
|
|
40
|
+
### 1. Basic Usage (One-time generation)
|
|
32
41
|
|
|
33
|
-
Create a script to generate your routes
|
|
42
|
+
Create a script to generate your routes:
|
|
34
43
|
|
|
35
44
|
```js
|
|
36
45
|
// scripts/generate-routes.mjs
|
|
37
|
-
import {
|
|
46
|
+
import { generateFileRouter } from 'ts-file-router';
|
|
38
47
|
|
|
39
|
-
|
|
40
|
-
baseFolder: 'src/screens',
|
|
41
|
-
outputFile: '
|
|
48
|
+
generateFileRouter({
|
|
49
|
+
baseFolder: './src/screens',
|
|
50
|
+
outputFile: 'routes.ts',
|
|
42
51
|
});
|
|
43
52
|
```
|
|
44
53
|
|
|
45
|
-
|
|
54
|
+
Run it:
|
|
46
55
|
|
|
47
|
-
|
|
56
|
+
```bash
|
|
57
|
+
node scripts/generate-routes.mjs
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
### 2. With File Watcher (Auto-regenerate on changes)
|
|
48
61
|
|
|
49
62
|
```js
|
|
50
|
-
// scripts/
|
|
51
|
-
import {
|
|
63
|
+
// scripts/generate-routes.mjs
|
|
64
|
+
import { generateFileRouter } from 'ts-file-router';
|
|
65
|
+
|
|
66
|
+
generateFileRouter({
|
|
67
|
+
dir: './src/screens',
|
|
68
|
+
outputFilename: 'routes.ts',
|
|
69
|
+
options: {
|
|
70
|
+
watcher: {
|
|
71
|
+
watch: true,
|
|
72
|
+
debounce: 500, // Wait 500ms after changes before regenerating
|
|
73
|
+
},
|
|
74
|
+
exitCodeOnResolution: false, // Don't exit process after generation
|
|
75
|
+
},
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
This will keep running and regenerate routes whenever you add, remove, or modify files in the `src/screens` folder.
|
|
80
|
+
|
|
81
|
+
### 3. With Vite Plugin
|
|
82
|
+
|
|
83
|
+
```ts
|
|
84
|
+
// vite.config.ts
|
|
85
|
+
import { defineConfig } from 'vite';
|
|
86
|
+
import { generateViteFileRouter } from 'ts-file-router';
|
|
52
87
|
|
|
53
|
-
// https://vite.dev/config/
|
|
54
88
|
export default defineConfig({
|
|
55
89
|
plugins: [
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
90
|
+
generateViteFileRouter({
|
|
91
|
+
dir: './src/screens',
|
|
92
|
+
outputFilename: 'routes.ts',
|
|
93
|
+
// Optional: customize watcher behavior
|
|
94
|
+
options: {
|
|
95
|
+
watcher: { watch: true, debounce: 500 },
|
|
96
|
+
exitCodeOnResolution: false,
|
|
97
|
+
},
|
|
59
98
|
}),
|
|
60
99
|
],
|
|
61
100
|
});
|
|
62
101
|
```
|
|
63
102
|
|
|
64
|
-
|
|
103
|
+
The plugin automatically runs during Vite's dev server (`vite dev`) and regenerates routes on file changes.
|
|
65
104
|
|
|
66
|
-
|
|
105
|
+
---
|
|
67
106
|
|
|
68
|
-
|
|
69
|
-
// scripts/generate-routes.mjs
|
|
70
|
-
import { generateRoutes } from 'ts-file-router';
|
|
107
|
+
## ð How It Works
|
|
71
108
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
109
|
+
Given this folder structure:
|
|
110
|
+
|
|
111
|
+
```
|
|
112
|
+
src/screens/ # Home page
|
|
113
|
+
âââ about/
|
|
114
|
+
â âââ page.tsx # About page
|
|
115
|
+
âââ users/
|
|
116
|
+
â âââ page.tsx # Users list
|
|
117
|
+
â âââ profile/
|
|
118
|
+
â â âââ page.tsx # User profile
|
|
119
|
+
â âââ _private/
|
|
120
|
+
â âââ page.tsx # Ignored (starts with _)
|
|
121
|
+
âââ index.tsx # Ignored (index file)
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
Generates:
|
|
125
|
+
|
|
126
|
+
```ts
|
|
127
|
+
// GENERATED WITH TS-FILE-ROUTER DO NOT EDIT
|
|
128
|
+
export const routes = {
|
|
129
|
+
about: {
|
|
130
|
+
path: '/about',
|
|
131
|
+
import: () => import('./about/page'),
|
|
78
132
|
},
|
|
79
|
-
|
|
133
|
+
users: {
|
|
134
|
+
page: {
|
|
135
|
+
path: '/users',
|
|
136
|
+
import: () => import('./users/page'),
|
|
137
|
+
},
|
|
138
|
+
profile: {
|
|
139
|
+
path: '/users/profile',
|
|
140
|
+
import: () => import('./users/profile/page'),
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
} as const;
|
|
80
144
|
```
|
|
81
145
|
|
|
82
|
-
|
|
146
|
+
---
|
|
83
147
|
|
|
84
|
-
|
|
148
|
+
## ð§ Configuration Options
|
|
85
149
|
|
|
86
|
-
|
|
150
|
+
### `generateFileRouter()` Parameters
|
|
87
151
|
|
|
88
|
-
|
|
152
|
+
| Parameter | Type | Required | Description |
|
|
153
|
+
| ---------------- | ------------------------ | -------- | ---------------------------------- |
|
|
154
|
+
| `dir` | `string` | â
Yes | Root directory to scan for routes |
|
|
155
|
+
| `outputFilename` | `string` | â
Yes | Path for the generated routes file |
|
|
156
|
+
| `options` | `TGenerateRoutesOptions` | â No | Additional configuration |
|
|
89
157
|
|
|
90
|
-
|
|
158
|
+
### Options Object
|
|
91
159
|
|
|
92
|
-
|
|
160
|
+
```ts
|
|
161
|
+
interface TGenerateRoutesOptions {
|
|
162
|
+
watcher?: {
|
|
163
|
+
watch: boolean; // Enable file watching
|
|
164
|
+
debounce?: number; // Debounce delay in ms (default: 500)
|
|
165
|
+
};
|
|
166
|
+
exitCodeOnResolution?: boolean; // Exit process after generation (default: true)
|
|
167
|
+
}
|
|
168
|
+
```
|
|
93
169
|
|
|
94
|
-
|
|
170
|
+
### `exitCodeOnResolution`
|
|
95
171
|
|
|
96
|
-
|
|
172
|
+
- **`true`** (default for CLI): Process exits with code `0` on success or `1` on error
|
|
173
|
+
- **`false`** (default for Vite/plugin): Process keeps running (required for watchers)
|
|
97
174
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## ðŊ Usage Examples
|
|
178
|
+
|
|
179
|
+
### With React Router And React Lazy Using
|
|
180
|
+
|
|
181
|
+
```tsx
|
|
182
|
+
import { routes } from './screens/routes';
|
|
183
|
+
import { BrowserRouter, Routes, Route } from 'react-router-dom';
|
|
184
|
+
import React from 'react';
|
|
185
|
+
|
|
186
|
+
const About = lazy(() =>
|
|
187
|
+
routes.about.import().then((r) => ({ default: res.About })),
|
|
188
|
+
);
|
|
189
|
+
|
|
190
|
+
const Profile = lazy(() =>
|
|
191
|
+
routes.users.profile.import().then((r) => ({ default: res.Profile })),
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
function App() {
|
|
195
|
+
return (
|
|
196
|
+
<BrowserRouter>
|
|
197
|
+
<Routes>
|
|
198
|
+
<Route path={routes.about.path} element={<About />} />
|
|
199
|
+
<Route path={routes.users.profile.path} element={<Profile />} />
|
|
200
|
+
</Routes>
|
|
201
|
+
</BrowserRouter>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
105
204
|
```
|
|
205
|
+
|
|
206
|
+
---
|
|
207
|
+
|
|
208
|
+
## ðŦ File Filtering Rules
|
|
209
|
+
|
|
210
|
+
The following files/folders are **automatically ignored**:
|
|
211
|
+
|
|
212
|
+
- **`index.*`** files - Index files are skipped
|
|
213
|
+
- **`_` prefixed** files/folders - Files starting with underscore are treated as private
|
|
214
|
+
- **Output file** - The generated routes file is ignored to prevent infinite loops
|
|
215
|
+
|
|
216
|
+
This allows you to have helper files, components, or private routes alongside your page files without polluting the routes tree.
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## ðĶ Output File Structure
|
|
221
|
+
|
|
222
|
+
The generated file is a TypeScript module with:
|
|
223
|
+
|
|
224
|
+
- â
`export const routes` - Named export with all routes
|
|
225
|
+
- â
`as const` assertion - Full type inference for route paths and imports
|
|
226
|
+
|
|
227
|
+
## ð Advanced Usage
|
|
228
|
+
|
|
229
|
+
### Custom Route File Names
|
|
230
|
+
|
|
231
|
+
By default, any file (except ignored ones) becomes a route. You can control this by:
|
|
232
|
+
|
|
233
|
+
1. Using a consistent naming convention (e.g., all pages named `page.tsx`)
|
|
234
|
+
2. Using `_` prefix for non-route files: `_components.tsx`, `_utils.ts`
|
|
235
|
+
|
|
236
|
+
### Watcher Events
|
|
237
|
+
|
|
238
|
+
When using the watcher, routes regenerate on:
|
|
239
|
+
|
|
240
|
+
- `add` - New file added
|
|
241
|
+
- `addDir` - New directory added
|
|
242
|
+
- `unlink` - File deleted
|
|
243
|
+
- `unlinkDir` - Directory deleted
|
|
244
|
+
|
|
245
|
+
If the output file is deleted, it's automatically regenerated.
|
|
246
|
+
|
|
247
|
+
### Graceful Shutdown
|
|
248
|
+
|
|
249
|
+
The watcher listens for:
|
|
250
|
+
|
|
251
|
+
- `SIGINT` (Ctrl+C) - Graceful cleanup
|
|
252
|
+
- `SIGTERM` - Container/process termination
|
|
253
|
+
|
|
254
|
+
Both trigger proper watcher cleanup before exit.
|
|
255
|
+
|
|
256
|
+
---
|
|
257
|
+
|
|
258
|
+
## ð Tips
|
|
259
|
+
|
|
260
|
+
1. **Add to `package.json`**:
|
|
261
|
+
|
|
262
|
+
```json
|
|
263
|
+
{
|
|
264
|
+
"scripts": {
|
|
265
|
+
"generate:routes": "node scripts/generate-routes.mjs",
|
|
266
|
+
"dev": "npm run generate:routes && vite",
|
|
267
|
+
"dev:watch": "node scripts/generate-routes.mjs --watch"
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
2. **Use relative paths in output**: The `outputFilename` path is relative to `dir`.
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## ðĪ Contributing
|
|
277
|
+
|
|
278
|
+
Found a bug or have a feature request? Open an issue or submit a PR!
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## ð License
|
|
283
|
+
|
|
284
|
+
ISC ÂĐ MatheusF10
|