feat(nuxt): allow extending routes with custom alias (#7074)

Co-authored-by: Pooya Parsa <pooya@pi0.io>
This commit is contained in:
Kévin Schnekenburger 2022-09-05 09:53:01 +02:00 committed by GitHub
parent 74db9d525e
commit 4bd1be9174
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 8 additions and 1 deletions

View File

@ -232,11 +232,17 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
const file = normalize(route.file) const file = normalize(route.file)
const metaImportName = genSafeVariableName(file) + 'Meta' const metaImportName = genSafeVariableName(file) + 'Meta'
metaImports.add(genImport(`${file}?macro=true`, [{ name: 'meta', as: metaImportName }])) metaImports.add(genImport(`${file}?macro=true`, [{ name: 'meta', as: metaImportName }]))
let aliasCode = `${metaImportName}?.alias || []`
if (Array.isArray(route.alias) && route.alias.length) {
aliasCode = `${JSON.stringify(route.alias)}.concat(${aliasCode})`
}
return { return {
...Object.fromEntries(Object.entries(route).map(([key, value]) => [key, JSON.stringify(value)])), ...Object.fromEntries(Object.entries(route).map(([key, value]) => [key, JSON.stringify(value)])),
children: route.children ? normalizeRoutes(route.children, metaImports).routes : [], children: route.children ? normalizeRoutes(route.children, metaImports).routes : [],
meta: route.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(route.meta)}}` : metaImportName, meta: route.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(route.meta)}}` : metaImportName,
alias: `${metaImportName}?.alias || []`, alias: aliasCode,
component: genDynamicImport(file, { interopDefault: true }) component: genDynamicImport(file, { interopDefault: true })
} }
})) }))

View File

@ -42,6 +42,7 @@ export type NuxtPage = {
path: string path: string
file: string file: string
meta?: Record<string, any> meta?: Record<string, any>
alias?: string[]
children?: NuxtPage[] children?: NuxtPage[]
} }