WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
File tree Expand file tree Collapse file tree 4 files changed +34
-1
lines changed
Expand file tree Collapse file tree 4 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,11 @@ VueRouter({
6868 async beforeWriteFiles(rootRoute ) {
6969 // ...
7070 },
71+
72+ // modify the generated DTS file before written to disk
73+ async postProcessDTS(dts ) {
74+ // ...
75+ },
7176})
7277```
7378
Original file line number Diff line number Diff line change 11import { describe , expect , it } from 'vitest'
22import { createRoutesContext } from '../src/core/context'
33import { resolveOptions } from '../src/options'
4+ import { readFileSync } from 'node:fs'
45import { fileURLToPath , URL } from 'node:url'
56import { normalize , join } from 'pathe'
67
@@ -34,6 +35,23 @@ describe('e2e routes', () => {
3435 ) . toMatchSnapshot ( )
3536 } )
3637
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+
3755 it . skip ( 'works with mixed extensions' , async ( ) => {
3856 const context = createRoutesContext (
3957 resolveOptions ( {
Original file line number Diff line number Diff line change @@ -263,7 +263,12 @@ if (import.meta.hot) {
263263
264264 logTree ( routeTree , logger . log )
265265 if ( dts ) {
266- const content = generateDTS ( )
266+ let content = generateDTS ( )
267+
268+ if ( options . postProcessDTS ) {
269+ content = await options . postProcessDTS ( content )
270+ }
271+
267272 if ( lastDTS !== content ) {
268273 await fs . mkdir ( dirname ( dts ) , { recursive : true } )
269274 await fs . writeFile ( dts , content , 'utf-8' )
Original file line number Diff line number Diff line change @@ -163,6 +163,11 @@ export interface Options {
163163 */
164164 beforeWriteFiles ?: ( rootRoute : EditableTreeNode ) => _Awaitable < void >
165165
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+
166171 /**
167172 * Defines how page components should be imported. Defaults to dynamic imports to enable lazy loading of pages.
168173 * @default `'async'`
You can’t perform that action at this time.
0 commit comments