@buzola/vite-plugin 0.0.1

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 (56) hide show
  1. package/dist/__tests__/codegen.test.d.ts +2 -0
  2. package/dist/__tests__/codegen.test.d.ts.map +1 -0
  3. package/dist/__tests__/codegen.test.js +121 -0
  4. package/dist/__tests__/codegen.test.js.map +1 -0
  5. package/dist/__tests__/conventions.test.d.ts +2 -0
  6. package/dist/__tests__/conventions.test.d.ts.map +1 -0
  7. package/dist/__tests__/conventions.test.js +80 -0
  8. package/dist/__tests__/conventions.test.js.map +1 -0
  9. package/dist/__tests__/page-extractor.test.d.ts +2 -0
  10. package/dist/__tests__/page-extractor.test.d.ts.map +1 -0
  11. package/dist/__tests__/page-extractor.test.js +82 -0
  12. package/dist/__tests__/page-extractor.test.js.map +1 -0
  13. package/dist/__tests__/tree-builder.test.d.ts +2 -0
  14. package/dist/__tests__/tree-builder.test.d.ts.map +1 -0
  15. package/dist/__tests__/tree-builder.test.js +335 -0
  16. package/dist/__tests__/tree-builder.test.js.map +1 -0
  17. package/dist/conventions.d.ts +53 -0
  18. package/dist/conventions.d.ts.map +1 -0
  19. package/dist/conventions.js +82 -0
  20. package/dist/conventions.js.map +1 -0
  21. package/dist/generator/codegen.d.ts +23 -0
  22. package/dist/generator/codegen.d.ts.map +1 -0
  23. package/dist/generator/codegen.js +164 -0
  24. package/dist/generator/codegen.js.map +1 -0
  25. package/dist/generator/page-extractor.d.ts +21 -0
  26. package/dist/generator/page-extractor.d.ts.map +1 -0
  27. package/dist/generator/page-extractor.js +23 -0
  28. package/dist/generator/page-extractor.js.map +1 -0
  29. package/dist/generator/scanner.d.ts +11 -0
  30. package/dist/generator/scanner.d.ts.map +1 -0
  31. package/dist/generator/scanner.js +34 -0
  32. package/dist/generator/scanner.js.map +1 -0
  33. package/dist/generator/tree-builder.d.ts +50 -0
  34. package/dist/generator/tree-builder.d.ts.map +1 -0
  35. package/dist/generator/tree-builder.js +273 -0
  36. package/dist/generator/tree-builder.js.map +1 -0
  37. package/dist/index.d.ts +13 -0
  38. package/dist/index.d.ts.map +1 -0
  39. package/dist/index.js +7 -0
  40. package/dist/index.js.map +1 -0
  41. package/dist/plugin.d.ts +26 -0
  42. package/dist/plugin.d.ts.map +1 -0
  43. package/dist/plugin.js +101 -0
  44. package/dist/plugin.js.map +1 -0
  45. package/package.json +24 -0
  46. package/src/__tests__/codegen.test.ts +134 -0
  47. package/src/__tests__/conventions.test.ts +93 -0
  48. package/src/__tests__/page-extractor.test.ts +95 -0
  49. package/src/__tests__/tree-builder.test.ts +390 -0
  50. package/src/conventions.ts +116 -0
  51. package/src/generator/codegen.ts +214 -0
  52. package/src/generator/page-extractor.ts +48 -0
  53. package/src/generator/scanner.ts +45 -0
  54. package/src/generator/tree-builder.ts +354 -0
  55. package/src/index.ts +17 -0
  56. package/src/plugin.ts +132 -0
@@ -0,0 +1,390 @@
1
+ import { describe, expect, it } from 'bun:test'
2
+ import type { ModuleLoader } from '../generator/page-extractor'
3
+ import type { ScannedFile } from '../generator/scanner'
4
+ import { buildFileRouteTree } from '../generator/tree-builder'
5
+
6
+ /** Module loader that returns empty modules (no page exports). */
7
+ const emptyLoader: ModuleLoader = async () => ({})
8
+
9
+ describe('buildFileRouteTree', () => {
10
+ it('builds a tree from flat files', async () => {
11
+ const files: ScannedFile[] = [
12
+ { absolutePath: '/app/src/routes/index.tsx', relativePath: 'index.tsx' },
13
+ { absolutePath: '/app/src/routes/about.tsx', relativePath: 'about.tsx' },
14
+ ]
15
+
16
+ const tree = await buildFileRouteTree(files, emptyLoader)
17
+ expect(tree).toHaveLength(2)
18
+
19
+ const index = tree.find(n => n.isIndex)
20
+ expect(index).toBeDefined()
21
+ expect(index!.fullPath).toBe('/')
22
+
23
+ const about = tree.find(n => n.segment === 'about')
24
+ expect(about).toBeDefined()
25
+ expect(about!.fullPath).toBe('/about')
26
+ })
27
+
28
+ it('builds nested routes with layouts', async () => {
29
+ const files: ScannedFile[] = [
30
+ { absolutePath: '/app/src/routes/_layout.tsx', relativePath: '_layout.tsx' },
31
+ { absolutePath: '/app/src/routes/index.tsx', relativePath: 'index.tsx' },
32
+ { absolutePath: '/app/src/routes/users/_layout.tsx', relativePath: 'users/_layout.tsx' },
33
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
34
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
35
+ ]
36
+
37
+ const tree = await buildFileRouteTree(files, emptyLoader)
38
+
39
+ expect(tree).toHaveLength(1)
40
+ const rootLayout = tree[0]
41
+ expect(rootLayout.isLayout).toBe(true)
42
+ expect(rootLayout.filePath).toBe('/app/src/routes/_layout.tsx')
43
+
44
+ const rootIndex = rootLayout.children.find(n => n.isIndex)
45
+ expect(rootIndex).toBeDefined()
46
+
47
+ const usersLayout = rootLayout.children.find(n => n.isLayout && n.segment === 'users')
48
+ expect(usersLayout).toBeDefined()
49
+
50
+ expect(usersLayout!.children).toHaveLength(2)
51
+ const usersIndex = usersLayout!.children.find(n => n.isIndex)
52
+ expect(usersIndex).toBeDefined()
53
+ const detail = usersLayout!.children.find(n => n.segment === 'detail')
54
+ expect(detail).toBeDefined()
55
+ })
56
+
57
+ it('handles pathless groups', async () => {
58
+ const files: ScannedFile[] = [
59
+ { absolutePath: '/app/src/routes/(auth)/login.tsx', relativePath: '(auth)/login.tsx' },
60
+ ]
61
+
62
+ const tree = await buildFileRouteTree(files, emptyLoader)
63
+ const authGroup = tree.find(n => n.isPathlessGroup)
64
+ expect(authGroup).toBeDefined()
65
+ })
66
+
67
+ it('uses plain file names as segments', async () => {
68
+ const files: ScannedFile[] = [
69
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
70
+ ]
71
+
72
+ const tree = await buildFileRouteTree(files, emptyLoader)
73
+ const users = tree.find(n => n.segment === 'users')
74
+ expect(users).toBeDefined()
75
+ expect(users!.children).toHaveLength(1)
76
+ expect(users!.children[0].segment).toBe('detail')
77
+ expect(users!.children[0].fullPath).toBe('/users/detail')
78
+ })
79
+
80
+ it('derives segment from page .route() via module loader', async () => {
81
+ const files: ScannedFile[] = [
82
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
83
+ ]
84
+
85
+ const loader: ModuleLoader = async (p) => {
86
+ if (p === '/app/src/routes/users/detail.tsx') {
87
+ return {
88
+ default: {
89
+ __buzolaPage: true,
90
+ component: () => null,
91
+ route: '/users/:userId',
92
+ paramsMeta: [{ name: 'userId', optional: false, array: false }],
93
+ },
94
+ }
95
+ }
96
+ return {}
97
+ }
98
+
99
+ const tree = await buildFileRouteTree(files, loader)
100
+ const users = tree.find(n => n.segment === 'users')
101
+ expect(users).toBeDefined()
102
+ expect(users!.children).toHaveLength(1)
103
+ expect(users!.children[0].segment).toBe(':userId')
104
+ expect(users!.children[0].fullPath).toBe('/users/:userId')
105
+ expect(users!.children[0].pageExports).toEqual([
106
+ { pageId: 'users/detail', exportName: 'default', routePattern: '/users/:userId', params: [{ name: 'userId', optional: false, array: false }] },
107
+ ])
108
+ })
109
+
110
+ it('derives route from file path when no .route()', async () => {
111
+ const files: ScannedFile[] = [
112
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
113
+ ]
114
+
115
+ const loader: ModuleLoader = async () => ({
116
+ default: {
117
+ __buzolaPage: true,
118
+ component: () => null,
119
+ paramsMeta: [{ name: 'userId', optional: false, array: false }],
120
+ // no .route() — should derive /users/detail from file path
121
+ },
122
+ })
123
+
124
+ const tree = await buildFileRouteTree(files, loader)
125
+ const users = tree.find(n => n.segment === 'users')
126
+ expect(users).toBeDefined()
127
+ expect(users!.children[0].segment).toBe('detail')
128
+ expect(users!.children[0].fullPath).toBe('/users/detail')
129
+ expect(users!.children[0].pageExports).toEqual([
130
+ { pageId: 'users/detail', exportName: 'default', routePattern: '/users/detail', params: [{ name: 'userId', optional: false, array: false }] },
131
+ ])
132
+ })
133
+
134
+ it('derives route for index file when no .route()', async () => {
135
+ const files: ScannedFile[] = [
136
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
137
+ ]
138
+
139
+ const loader: ModuleLoader = async () => ({
140
+ default: {
141
+ __buzolaPage: true,
142
+ component: () => null,
143
+ paramsMeta: [],
144
+ },
145
+ })
146
+
147
+ const tree = await buildFileRouteTree(files, loader)
148
+ const users = tree.find(n => n.segment === 'users')
149
+ const index = users!.children.find(n => n.isIndex)
150
+ expect(index).toBeDefined()
151
+ expect(index!.pageExports).toEqual([
152
+ { pageId: 'users', exportName: 'default', routePattern: '/users', params: [] },
153
+ ])
154
+ })
155
+
156
+ it('sorts not-found routes last', async () => {
157
+ const files: ScannedFile[] = [
158
+ { absolutePath: '/app/src/routes/about.tsx', relativePath: 'about.tsx' },
159
+ { absolutePath: '/app/src/routes/_404.tsx', relativePath: '_404.tsx' },
160
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
161
+ ]
162
+
163
+ const tree = await buildFileRouteTree(files, emptyLoader)
164
+ const segments = tree.map(n => n.segment)
165
+
166
+ // _404 catch-all must be last
167
+ expect(segments[segments.length - 1]).toBe(':__notFound+')
168
+ const notFound = tree.find(n => n.isNotFound)
169
+ expect(notFound).toBeDefined()
170
+ expect(notFound!.fullPath).toBe('/:__notFound+')
171
+ })
172
+
173
+ // ─── Named exports ──────────────────────────────────────────────────────
174
+
175
+ it('creates child nodes for named exports from regular files', async () => {
176
+ const files: ScannedFile[] = [
177
+ { absolutePath: '/app/src/routes/users.tsx', relativePath: 'users.tsx' },
178
+ ]
179
+
180
+ const loader: ModuleLoader = async () => ({
181
+ default: {
182
+ __buzolaPage: true,
183
+ component: () => null,
184
+ paramsMeta: [],
185
+ },
186
+ detail: {
187
+ __buzolaPage: true,
188
+ component: () => null,
189
+ paramsMeta: [{ name: 'id', optional: false, array: false }],
190
+ },
191
+ settings: {
192
+ __buzolaPage: true,
193
+ component: () => null,
194
+ paramsMeta: [],
195
+ },
196
+ })
197
+
198
+ const tree = await buildFileRouteTree(files, loader)
199
+ const users = tree.find(n => n.segment === 'users')
200
+ expect(users).toBeDefined()
201
+ expect(users!.filePath).toBe('/app/src/routes/users.tsx')
202
+ expect(users!.exportName).toBe('default')
203
+ expect(users!.pageExports).toEqual([
204
+ { pageId: 'users', exportName: 'default', routePattern: '/users', params: [] },
205
+ ])
206
+
207
+ // Named exports become children
208
+ expect(users!.children).toHaveLength(2)
209
+ const detail = users!.children.find(n => n.segment === 'detail')
210
+ expect(detail).toBeDefined()
211
+ expect(detail!.filePath).toBe('/app/src/routes/users.tsx')
212
+ expect(detail!.exportName).toBe('detail')
213
+ expect(detail!.fullPath).toBe('/users/detail')
214
+ expect(detail!.pageExports).toEqual([
215
+ { pageId: 'users/detail', exportName: 'detail', routePattern: '/users/detail', params: [{ name: 'id', optional: false, array: false }] },
216
+ ])
217
+
218
+ const settings = users!.children.find(n => n.segment === 'settings')
219
+ expect(settings).toBeDefined()
220
+ expect(settings!.fullPath).toBe('/users/settings')
221
+ })
222
+
223
+ it('creates sibling nodes for named exports from index files', async () => {
224
+ const files: ScannedFile[] = [
225
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
226
+ ]
227
+
228
+ const loader: ModuleLoader = async () => ({
229
+ default: {
230
+ __buzolaPage: true,
231
+ component: () => null,
232
+ paramsMeta: [],
233
+ },
234
+ detail: {
235
+ __buzolaPage: true,
236
+ component: () => null,
237
+ paramsMeta: [{ name: 'id', optional: false, array: false }],
238
+ },
239
+ })
240
+
241
+ const tree = await buildFileRouteTree(files, loader)
242
+ const users = tree.find(n => n.segment === 'users')
243
+ expect(users).toBeDefined()
244
+
245
+ // Default export → index node
246
+ const index = users!.children.find(n => n.isIndex)
247
+ expect(index).toBeDefined()
248
+ expect(index!.filePath).toBe('/app/src/routes/users/index.tsx')
249
+
250
+ // Named export → sibling of index (child of users)
251
+ const detail = users!.children.find(n => n.segment === 'detail')
252
+ expect(detail).toBeDefined()
253
+ expect(detail!.filePath).toBe('/app/src/routes/users/index.tsx')
254
+ expect(detail!.exportName).toBe('detail')
255
+ expect(detail!.fullPath).toBe('/users/detail')
256
+ expect(detail!.pageExports).toEqual([
257
+ { pageId: 'users/detail', exportName: 'detail', routePattern: '/users/detail', params: [{ name: 'id', optional: false, array: false }] },
258
+ ])
259
+ })
260
+
261
+ it('creates container node when file has only named exports', async () => {
262
+ const files: ScannedFile[] = [
263
+ { absolutePath: '/app/src/routes/users.tsx', relativePath: 'users.tsx' },
264
+ ]
265
+
266
+ const loader: ModuleLoader = async () => ({
267
+ list: {
268
+ __buzolaPage: true,
269
+ component: () => null,
270
+ paramsMeta: [],
271
+ },
272
+ detail: {
273
+ __buzolaPage: true,
274
+ component: () => null,
275
+ paramsMeta: [],
276
+ },
277
+ })
278
+
279
+ const tree = await buildFileRouteTree(files, loader)
280
+ const users = tree.find(n => n.segment === 'users')
281
+ expect(users).toBeDefined()
282
+ expect(users!.filePath).toBeUndefined() // No default → no component
283
+ expect(users!.pageExports).toBeUndefined()
284
+
285
+ expect(users!.children).toHaveLength(2)
286
+ expect(users!.children.find(n => n.segment === 'detail')).toBeDefined()
287
+ expect(users!.children.find(n => n.segment === 'list')).toBeDefined()
288
+ })
289
+
290
+ it('named export with .route() uses explicit route pattern', async () => {
291
+ const files: ScannedFile[] = [
292
+ { absolutePath: '/app/src/routes/users.tsx', relativePath: 'users.tsx' },
293
+ ]
294
+
295
+ const loader: ModuleLoader = async () => ({
296
+ detail: {
297
+ __buzolaPage: true,
298
+ component: () => null,
299
+ route: '/users/:userId',
300
+ paramsMeta: [{ name: 'userId', optional: false, array: false }],
301
+ },
302
+ })
303
+
304
+ const tree = await buildFileRouteTree(files, loader)
305
+ const users = tree.find(n => n.segment === 'users')
306
+ expect(users!.children).toHaveLength(1)
307
+ expect(users!.children[0].segment).toBe(':userId')
308
+ expect(users!.children[0].fullPath).toBe('/users/:userId')
309
+ })
310
+
311
+ it('throws on collision: named export vs file in subdirectory', async () => {
312
+ const files: ScannedFile[] = [
313
+ { absolutePath: '/app/src/routes/users.tsx', relativePath: 'users.tsx' },
314
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
315
+ ]
316
+
317
+ const loader: ModuleLoader = async (p) => {
318
+ if (p === '/app/src/routes/users.tsx') {
319
+ return {
320
+ detail: {
321
+ __buzolaPage: true,
322
+ component: () => null,
323
+ paramsMeta: [],
324
+ },
325
+ }
326
+ }
327
+ return {
328
+ default: {
329
+ __buzolaPage: true,
330
+ component: () => null,
331
+ paramsMeta: [],
332
+ },
333
+ }
334
+ }
335
+
336
+ expect(buildFileRouteTree(files, loader)).rejects.toThrow(/Route collision/)
337
+ })
338
+
339
+ it('throws on collision: named export from index vs file in same directory', async () => {
340
+ const files: ScannedFile[] = [
341
+ { absolutePath: '/app/src/routes/users/detail.tsx', relativePath: 'users/detail.tsx' },
342
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
343
+ ]
344
+
345
+ const loader: ModuleLoader = async (p) => {
346
+ if (p === '/app/src/routes/users/index.tsx') {
347
+ return {
348
+ detail: {
349
+ __buzolaPage: true,
350
+ component: () => null,
351
+ paramsMeta: [],
352
+ },
353
+ }
354
+ }
355
+ return {
356
+ default: {
357
+ __buzolaPage: true,
358
+ component: () => null,
359
+ paramsMeta: [],
360
+ },
361
+ }
362
+ }
363
+
364
+ expect(buildFileRouteTree(files, loader)).rejects.toThrow(/Route collision/)
365
+ })
366
+
367
+ it('handles nested _404 within a directory', async () => {
368
+ const files: ScannedFile[] = [
369
+ { absolutePath: '/app/src/routes/users/index.tsx', relativePath: 'users/index.tsx' },
370
+ { absolutePath: '/app/src/routes/users/_404.tsx', relativePath: 'users/_404.tsx' },
371
+ ]
372
+
373
+ const tree = await buildFileRouteTree(files, emptyLoader)
374
+ const users = tree.find(n => n.segment === 'users')
375
+ expect(users).toBeDefined()
376
+
377
+ const notFound = users!.children.find(n => n.isNotFound)
378
+ expect(notFound).toBeDefined()
379
+ expect(notFound!.segment).toBe(':__notFound+')
380
+ expect(notFound!.fullPath).toBe('/users/:__notFound+')
381
+ expect(notFound!.pageExports).toEqual([
382
+ {
383
+ pageId: 'users/404',
384
+ exportName: 'default',
385
+ routePattern: '/users/:__notFound+',
386
+ params: [{ name: '__notFound', optional: false, array: false }],
387
+ },
388
+ ])
389
+ })
390
+ })
@@ -0,0 +1,116 @@
1
+ import * as path from 'node:path'
2
+
3
+ /**
4
+ * File naming conventions for page-centric routing.
5
+ *
6
+ * _layout.tsx → Layout wrapper (renders children via <Outlet />)
7
+ * _404.tsx → Not-found catch-all (auto-assigned catch-all pattern within parent scope)
8
+ * index.tsx → Index route (renders at parent's exact path)
9
+ * about.tsx → Named page file (page ID derived from file name)
10
+ * (group)/ → Pathless group (no URL segment)
11
+ *
12
+ * Files prefixed with `_` are special convention files, not regular pages.
13
+ * Route patterns (URL paths) come exclusively from `.route()` on `createPage()`.
14
+ * File names only determine page IDs and layout nesting.
15
+ */
16
+
17
+ export const ROUTE_FILE_EXTENSIONS = ['.tsx', '.jsx', '.ts', '.js']
18
+
19
+ export interface ParsedRouteFile {
20
+ /** The route segment this file contributes. */
21
+ segment: string
22
+ /** Whether this is a layout file. */
23
+ isLayout: boolean
24
+ /** Whether this is an index file. */
25
+ isIndex: boolean
26
+ /** Whether this is a not-found catch-all file. */
27
+ isNotFound: boolean
28
+ /** Whether this directory is a pathless group. */
29
+ isPathlessGroup: boolean
30
+ /** Original file path relative to routes dir. */
31
+ relativePath: string
32
+ }
33
+
34
+ /**
35
+ * Check if a file is a route file based on extension.
36
+ */
37
+ export function isRouteFile(filePath: string): boolean {
38
+ return ROUTE_FILE_EXTENSIONS.some(ext => filePath.endsWith(ext))
39
+ }
40
+
41
+ /**
42
+ * Parse a directory name for routing conventions.
43
+ */
44
+ export function parseDirName(name: string): { segment: string; isPathlessGroup: boolean } {
45
+ // Pathless group: (auth)
46
+ if (name.startsWith('(') && name.endsWith(')')) {
47
+ return { segment: '', isPathlessGroup: true }
48
+ }
49
+
50
+ return { segment: name, isPathlessGroup: false }
51
+ }
52
+
53
+ /**
54
+ * Parse a route file name (without extension) into routing info.
55
+ */
56
+ export function parseFileName(nameWithoutExt: string): {
57
+ segment: string
58
+ isLayout: boolean
59
+ isIndex: boolean
60
+ isNotFound: boolean
61
+ } {
62
+ if (nameWithoutExt === '_layout') {
63
+ return { segment: '', isLayout: true, isIndex: false, isNotFound: false }
64
+ }
65
+
66
+ if (nameWithoutExt === '_404') {
67
+ return { segment: '', isLayout: false, isIndex: false, isNotFound: true }
68
+ }
69
+
70
+ if (nameWithoutExt === 'index') {
71
+ return { segment: '', isLayout: false, isIndex: true, isNotFound: false }
72
+ }
73
+
74
+ return { segment: nameWithoutExt, isLayout: false, isIndex: false, isNotFound: false }
75
+ }
76
+
77
+ /**
78
+ * Parse a route file path relative to the routes directory.
79
+ */
80
+ export function parseRouteFile(relativePath: string): ParsedRouteFile {
81
+ const ext = path.extname(relativePath)
82
+ const withoutExt = relativePath.slice(0, -ext.length)
83
+ const parts = withoutExt.split(path.sep)
84
+ const fileName = parts.pop()!
85
+ const dirs = parts
86
+
87
+ const fileInfo = parseFileName(fileName)
88
+
89
+ // Build segment from directory path + file
90
+ const segments: string[] = []
91
+ let isPathlessGroup = false
92
+
93
+ for (const dir of dirs) {
94
+ const dirInfo = parseDirName(dir)
95
+ if (dirInfo.isPathlessGroup) {
96
+ isPathlessGroup = true
97
+ } else {
98
+ segments.push(dirInfo.segment)
99
+ }
100
+ }
101
+
102
+ if (fileInfo.segment) {
103
+ segments.push(fileInfo.segment)
104
+ }
105
+
106
+ const segment = segments.length > 0 ? segments.join('/') : ''
107
+
108
+ return {
109
+ segment,
110
+ isLayout: fileInfo.isLayout,
111
+ isIndex: fileInfo.isIndex,
112
+ isNotFound: fileInfo.isNotFound,
113
+ isPathlessGroup,
114
+ relativePath,
115
+ }
116
+ }