File tree 4 files changed +34
-1
lines changed
4 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,11 @@ VueRouter({
68
68
async beforeWriteFiles(rootRoute ) {
69
69
// ...
70
70
},
71
+
72
+ // modify the generated DTS file before written to disk
73
+ async postProcessDTS(dts ) {
74
+ // ...
75
+ },
71
76
})
72
77
```
73
78
Original file line number Diff line number Diff line change 1
1
import { describe , expect , it } from 'vitest'
2
2
import { createRoutesContext } from '../src/core/context'
3
3
import { resolveOptions } from '../src/options'
4
+ import { readFileSync } from 'node:fs'
4
5
import { fileURLToPath , URL } from 'node:url'
5
6
import { normalize , join } from 'pathe'
6
7
@@ -34,6 +35,23 @@ describe('e2e routes', () => {
34
35
) . toMatchSnapshot ( )
35
36
} )
36
37
38
+ it ( 'allows post-processing the generated DTS file' , async ( ) => {
39
+ const suffix = `export type Foo = 'bar'`
40
+ const dts = join ( __dirname , './.types/__types.d.ts' )
41
+ const context = createRoutesContext (
42
+ resolveOptions ( {
43
+ dts,
44
+ logs : false ,
45
+ watch : false ,
46
+ routesFolder : [ { src : join ( __dirname , './fixtures/filenames/routes' ) } ] ,
47
+ postProcessDTS : ( dts ) => `${ dts } \n${ suffix } \n` ,
48
+ } )
49
+ )
50
+
51
+ await context . scanPages ( )
52
+ expect ( readFileSync ( dts , 'utf-8' ) ) . toContain ( suffix )
53
+ } )
54
+
37
55
it . skip ( 'works with mixed extensions' , async ( ) => {
38
56
const context = createRoutesContext (
39
57
resolveOptions ( {
Original file line number Diff line number Diff line change @@ -263,7 +263,12 @@ if (import.meta.hot) {
263
263
264
264
logTree ( routeTree , logger . log )
265
265
if ( dts ) {
266
- const content = generateDTS ( )
266
+ let content = generateDTS ( )
267
+
268
+ if ( options . postProcessDTS ) {
269
+ content = await options . postProcessDTS ( content )
270
+ }
271
+
267
272
if ( lastDTS !== content ) {
268
273
await fs . mkdir ( dirname ( dts ) , { recursive : true } )
269
274
await fs . writeFile ( dts , content , 'utf-8' )
Original file line number Diff line number Diff line change @@ -163,6 +163,11 @@ export interface Options {
163
163
*/
164
164
beforeWriteFiles ?: ( rootRoute : EditableTreeNode ) => _Awaitable < void >
165
165
166
+ /**
167
+ * Allows to post-process the generated d.ts files. This will be invoked **every time** the d.ts file needs to be written.
168
+ */
169
+ postProcessDTS ?: ( dts : string ) => _Awaitable < string >
170
+
166
171
/**
167
172
* Defines how page components should be imported. Defaults to dynamic imports to enable lazy loading of pages.
168
173
* @default `'async'`
You can’t perform that action at this time.
0 commit comments