Skip to content

Commit 1297a75

Browse files
committed
feat: allow post-processing generated DTS file
1 parent 70e454d commit 1297a75

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

docs/guide/configuration.md

+5
Original file line numberDiff line numberDiff 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

e2e/routes.spec.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, expect, it } from 'vitest'
22
import { createRoutesContext } from '../src/core/context'
33
import { resolveOptions } from '../src/options'
4+
import { readFileSync } from 'node:fs'
45
import { fileURLToPath, URL } from 'node:url'
56
import { 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({

src/core/context.ts

+6-1
Original file line numberDiff line numberDiff 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')

src/options.ts

+5
Original file line numberDiff line numberDiff 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'`

0 commit comments

Comments
 (0)