File tree Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Expand file tree Collapse file tree 4 files changed +96
-1
lines changed Original file line number Diff line number Diff line change 8
8
type GracileHandler ,
9
9
} from '../server/request.js' ;
10
10
import type { GracileConfig } from '../user-config.js' ;
11
+ import { generateRoutesTypings } from './route-typings.js' ;
11
12
12
13
export async function createDevHandler ( {
13
14
vite,
@@ -24,6 +25,11 @@ export async function createDevHandler({
24
25
25
26
const routes = await collectRoutes ( root , gracileConfig . routes ?. exclude ) ;
26
27
28
+ if ( gracileConfig . experimental ?. generateRoutesTypings )
29
+ generateRoutesTypings ( root , routes ) . catch ( ( error ) =>
30
+ logger . error ( String ( error ) ) ,
31
+ ) ;
32
+
27
33
vite . watcher . on ( 'all' , ( event , file ) => {
28
34
// console.log({ event });
29
35
if (
@@ -33,6 +39,11 @@ export async function createDevHandler({
33
39
collectRoutes ( root , gracileConfig . routes ?. exclude )
34
40
. then ( ( ) => {
35
41
vite . hot . send ( 'vite:invalidate' ) ;
42
+
43
+ if ( gracileConfig . experimental ?. generateRoutesTypings )
44
+ generateRoutesTypings ( root , routes ) . catch ( ( error ) =>
45
+ logger . error ( String ( error ) ) ,
46
+ ) ;
36
47
} )
37
48
. catch ( ( e ) => logger . error ( String ( e ) ) ) ;
38
49
} ) ;
Original file line number Diff line number Diff line change
1
+ import { mkdir } from 'node:fs/promises' ;
2
+ import { join } from 'node:path' ;
3
+
4
+ import { writeFile } from 'fs/promises' ;
5
+
6
+ import type { RoutesManifest } from '../routes/route.js' ;
7
+
8
+ export async function generateRoutesTypings (
9
+ root : string ,
10
+ routes : RoutesManifest ,
11
+ ) {
12
+ // NOTE: For future, we'll provide parameters like:
13
+ // `route('/blog/:id', { id: 'foo' })`.
14
+ //
15
+ // export const route: (path: string, params?: Params) => string;
16
+ //
17
+
18
+ const typings = `declare module 'gracile:route' {
19
+ export const route: (path: Route) => string;
20
+ }
21
+
22
+ export type Route =
23
+ | ${ [ ...routes ]
24
+ . map (
25
+ ( [ v ] ) =>
26
+ `\`${ v
27
+ . replace (
28
+ / \{ : ( .* ) \} / ,
29
+ // eslint-disable-next-line no-template-curly-in-string
30
+ '${string}' ,
31
+ )
32
+ . replace (
33
+ / : ( .* ?) \* \/ / ,
34
+ // eslint-disable-next-line no-template-curly-in-string
35
+ '${string}' ,
36
+ ) } \``,
37
+ )
38
+ . join ( '\n | ' ) } ;
39
+ ` ;
40
+
41
+ await mkdir ( join ( root , '.gracile' ) ) . catch ( ( ) => null ) ;
42
+ await writeFile ( join ( root , '.gracile/routes.d.ts' ) , typings ) ;
43
+ }
Original file line number Diff line number Diff line change @@ -55,6 +55,37 @@ export const gracile = (config?: GracileConfig): any /* Plugin */[] => {
55
55
isClientBuilt = true ;
56
56
57
57
return [
58
+ {
59
+ name : 'gracile-routes-codegen' ,
60
+
61
+ // watchChange(change) {
62
+ // console.log({ change });
63
+ // },
64
+
65
+ resolveId ( id ) {
66
+ const virtualModuleId = 'gracile:route' ;
67
+ const resolvedVirtualModuleId = `\0${ virtualModuleId } ` ;
68
+
69
+ if ( id === virtualModuleId ) {
70
+ return resolvedVirtualModuleId ;
71
+ }
72
+ return null ;
73
+ } ,
74
+
75
+ load ( id ) {
76
+ const virtualModuleId = 'gracile:route' ;
77
+ const resolvedVirtualModuleId = `\0${ virtualModuleId } ` ;
78
+
79
+ if ( id === resolvedVirtualModuleId ) {
80
+ return `
81
+ export function route(input){
82
+ return input;
83
+ }` ;
84
+ }
85
+ return null ;
86
+ } ,
87
+ } ,
88
+
58
89
{
59
90
name : 'vite-plugin-gracile-serve-middleware' ,
60
91
@@ -117,7 +148,7 @@ export const gracile = (config?: GracileConfig): any /* Plugin */[] => {
117
148
root : viteConfig . root || process . cwd ( ) ,
118
149
119
150
server : { middlewareMode : true } ,
120
- // NOTE: Stub
151
+ // NOTE: Stub. KEEP IT!
121
152
optimizeDeps : { include : [ ] } ,
122
153
} ) ;
123
154
Original file line number Diff line number Diff line change @@ -61,4 +61,14 @@ export interface GracileConfig {
61
61
*/
62
62
exclude ?: string [ ] ;
63
63
} ;
64
+
65
+ /**
66
+ * Future, unstable features flags.
67
+ */
68
+ experimental ?: {
69
+ /**
70
+ * Exclude routes with an array of patterns. Useful for debugging.
71
+ */
72
+ generateRoutesTypings ?: boolean ;
73
+ } ;
64
74
}
You can’t perform that action at this time.
0 commit comments