Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow post-processing generated DTS file #601

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/guide/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ VueRouter({
async beforeWriteFiles(rootRoute) {
// ...
},

// modify the generated DTS file before written to disk
async postProcessDTS(dts) {
// ...
},
})
```

Expand Down
18 changes: 18 additions & 0 deletions e2e/routes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, expect, it } from 'vitest'
import { createRoutesContext } from '../src/core/context'
import { resolveOptions } from '../src/options'
import { readFileSync } from 'node:fs'
import { fileURLToPath, URL } from 'node:url'
import { normalize, join } from 'pathe'

Expand Down Expand Up @@ -34,6 +35,23 @@ describe('e2e routes', () => {
).toMatchSnapshot()
})

it('allows post-processing the generated DTS file', async () => {
const suffix = `export type Foo = 'bar'`
const dts = join(__dirname, './.types/__types.d.ts')
const context = createRoutesContext(
resolveOptions({
dts,
logs: false,
watch: false,
routesFolder: [{ src: join(__dirname, './fixtures/filenames/routes') }],
postProcessDTS: (dts) => `${dts}\n${suffix}\n`,
})
)

await context.scanPages()
expect(readFileSync(dts, 'utf-8')).toContain(suffix)
})

it.skip('works with mixed extensions', async () => {
const context = createRoutesContext(
resolveOptions({
Expand Down
7 changes: 6 additions & 1 deletion src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,12 @@ if (import.meta.hot) {

logTree(routeTree, logger.log)
if (dts) {
const content = generateDTS()
let content = generateDTS()

if (options.postProcessDTS) {
content = await options.postProcessDTS(content)
}

if (lastDTS !== content) {
await fs.mkdir(dirname(dts), { recursive: true })
await fs.writeFile(dts, content, 'utf-8')
Expand Down
5 changes: 5 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ export interface Options {
*/
beforeWriteFiles?: (rootRoute: EditableTreeNode) => _Awaitable<void>

/**
* Allows to post-process the generated d.ts files. This will be invoked **every time** the d.ts file needs to be written.
*/
postProcessDTS?: (dts: string) => _Awaitable<string>

/**
* Defines how page components should be imported. Defaults to dynamic imports to enable lazy loading of pages.
* @default `'async'`
Expand Down