Skip to content

Commit 16b39ff

Browse files
committed
fix: param parsers when dts is not at root
1 parent 06e1658 commit 16b39ff

File tree

5 files changed

+39
-13
lines changed

5 files changed

+39
-13
lines changed

packages/playground-file-based/src/pages/[...path].vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useRoute, useRouter } from 'vue-router'
33
import doc from '../main.ts?raw'
44
55
const route = useRoute()
6+
console.log('route.params', route.params)
67
const router = useRouter()
78
// router.push({
89
// name: 'not-found',
@@ -49,10 +50,10 @@ definePage({
4950
default: -1,
5051
},
5152
52-
// optionalWhen: {
53-
// parser: 'date',
54-
// },
55-
//
53+
optionalWhen: {
54+
parser: 'date',
55+
},
56+
5657
when: {
5758
parser: 'date',
5859
default: () => new Date(),

packages/playground-file-based/src/routes.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import type {
1616
} from 'vue-router'
1717

1818
// Custom route params parsers
19-
type Param_date = ReturnType<NonNullable<typeof import('./src/params/date.ts').parser['get']>>
19+
type Param_date = ReturnType<NonNullable<typeof import('./params/date.ts').parser['get']>>
2020

2121
declare module 'vue-router' {
2222
interface TypesConfig {
@@ -46,8 +46,8 @@ declare module 'vue-router/auto-routes' {
4646
'not-found': RouteRecordInfo<
4747
'not-found',
4848
'/:path(.*)',
49-
{ path: string, page?: number, other?: boolean, active?: boolean, multi?: string[], req?: number, when?: Exclude<Param_date, unknown[]> },
50-
{ path: string, page: number, other: boolean | undefined, active: boolean, multi: string[] | undefined, req: number, when: Exclude<Param_date, unknown[]> },
49+
{ path: string, page?: number, other?: boolean, active?: boolean, multi?: string[], req?: number, optionalWhen?: Exclude<Param_date, unknown[]>, when?: Exclude<Param_date, unknown[]> },
50+
{ path: string, page: number, other: boolean | undefined, active: boolean, multi: string[] | undefined, req: number, optionalWhen: Exclude<Param_date, unknown[]> | undefined, when: Exclude<Param_date, unknown[]> },
5151
| never
5252
>,
5353
'/a.[b].c.[d]': RouteRecordInfo<

packages/router/src/unplugin/codegen/generateParamParsers.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,28 @@ describe('generateParamParsersTypesDeclarations', () => {
9797
)
9898
})
9999

100+
it('generates correct import path when parser is outside dts directory', () => {
101+
// This tests the case where dts is in a subfolder (e.g., types/typed-router.d.ts)
102+
// and parsers are at project root (e.g., parsers/uuid.ts)
103+
// The relativePath should be computed relative to the dts directory
104+
const paramParsers: ParamParsersMap = new Map([
105+
[
106+
'uuid',
107+
{
108+
name: 'uuid',
109+
typeName: 'Param_uuid',
110+
relativePath: '../parsers/uuid',
111+
absolutePath: '/project/parsers/uuid',
112+
},
113+
],
114+
])
115+
116+
const result = generateParamParsersTypesDeclarations(paramParsers)
117+
expect(result).toBe(
118+
`type Param_uuid = ReturnType<NonNullable<typeof import('../parsers/uuid').parser['get']>>`
119+
)
120+
})
121+
100122
it('generates multiple param parsers type declarations', () => {
101123
const paramParsers: ParamParsersMap = new Map([
102124
[

packages/router/src/unplugin/codegen/generateParamParsers.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ export function generateParamParsersTypesDeclarations(
4141
paramParsers: ParamParsersMap
4242
) {
4343
return Array.from(paramParsers.values())
44-
.map(
45-
({ typeName, relativePath }) =>
46-
`type ${typeName} = ReturnType<NonNullable<typeof import('./${relativePath}').parser['get']>>`
47-
)
44+
.map(({ typeName, relativePath }) => {
45+
const importPath = relativePath.startsWith('.')
46+
? relativePath
47+
: './' + relativePath
48+
return `type ${typeName} = ReturnType<NonNullable<typeof import('${importPath}').parser['get']>>`
49+
})
4850
.join('\n')
4951
}
5052

packages/router/src/unplugin/core/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function createRoutesContext(options: ResolvedOptions) {
3838
: preferDTS === true
3939
? resolve(root, 'typed-router.d.ts')
4040
: resolve(root, preferDTS)
41+
const dtsDir = dts ? dirname(dts) : root
4142

4243
const routeTree = new PrefixTree(options)
4344
const editableRoutes = new EditableTreeNode(routeTree)
@@ -142,7 +143,7 @@ export function createRoutesContext(options: ResolvedOptions) {
142143
name,
143144
typeName: `Param_${name}`,
144145
absolutePath,
145-
relativePath: relative(options.root, absolutePath),
146+
relativePath: relative(dtsDir, absolutePath),
146147
})
147148
}
148149
logger.log(
@@ -227,7 +228,7 @@ export function createRoutesContext(options: ResolvedOptions) {
227228
name,
228229
typeName: `Param_${name}`,
229230
absolutePath,
230-
relativePath: './' + relative(options.root, absolutePath),
231+
relativePath: relative(dtsDir, absolutePath),
231232
})
232233
writeConfigFiles()
233234
})

0 commit comments

Comments
 (0)