mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-28 16:42:04 +00:00
feat(schema): allow adding page routes without a matching file (#19173)
This commit is contained in:
parent
44068420da
commit
7131aef820
@ -146,8 +146,10 @@ export default defineNuxtModule({
|
|||||||
// Add router plugin
|
// Add router plugin
|
||||||
addPlugin(resolve(runtimeDir, 'plugins/router'))
|
addPlugin(resolve(runtimeDir, 'plugins/router'))
|
||||||
|
|
||||||
const getSources = (pages: NuxtPage[]): string[] => pages.flatMap(p =>
|
const getSources = (pages: NuxtPage[]): string[] => pages
|
||||||
[relative(nuxt.options.srcDir, p.file), ...getSources(p.children || [])]
|
.filter(p => Boolean(p.file))
|
||||||
|
.flatMap(p =>
|
||||||
|
[relative(nuxt.options.srcDir, p.file as string), ...getSources(p.children || [])]
|
||||||
)
|
)
|
||||||
|
|
||||||
// Do not prefetch page chunks
|
// Do not prefetch page chunks
|
||||||
|
@ -249,28 +249,40 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> =
|
|||||||
return {
|
return {
|
||||||
imports: metaImports,
|
imports: metaImports,
|
||||||
routes: genArrayFromRaw(routes.map((page) => {
|
routes: genArrayFromRaw(routes.map((page) => {
|
||||||
|
const route = Object.fromEntries(
|
||||||
|
Object.entries(page)
|
||||||
|
.filter(([key, value]) => key !== 'file' && (Array.isArray(value) ? value.length : value))
|
||||||
|
.map(([key, value]) => [key, JSON.stringify(value)])
|
||||||
|
) as Record<Exclude<keyof NuxtPage, 'file'>, string> & { component?: string }
|
||||||
|
|
||||||
|
if (page.children?.length) {
|
||||||
|
route.children = normalizeRoutes(page.children, metaImports).routes
|
||||||
|
}
|
||||||
|
|
||||||
|
// Without a file, we can't use `definePageMeta` to extract route-level meta from the file
|
||||||
|
if (!page.file) {
|
||||||
|
for (const key of ['name', 'path', 'meta', 'alias', 'redirect'] as const) {
|
||||||
|
if (page[key]) { route[key] = JSON.stringify(page[key]) }
|
||||||
|
}
|
||||||
|
return route
|
||||||
|
}
|
||||||
|
|
||||||
const file = normalize(page.file)
|
const file = normalize(page.file)
|
||||||
const metaImportName = genSafeVariableName(filename(file) + hash(file)) + 'Meta'
|
const metaImportName = genSafeVariableName(filename(file) + hash(file)) + 'Meta'
|
||||||
metaImports.add(genImport(`${file}?macro=true`, [{ name: 'default', as: metaImportName }]))
|
metaImports.add(genImport(`${file}?macro=true`, [{ name: 'default', as: metaImportName }]))
|
||||||
|
|
||||||
let aliasCode = `${metaImportName}?.alias || []`
|
let aliasCode = `${metaImportName}?.alias || []`
|
||||||
if (Array.isArray(page.alias) && page.alias.length) {
|
const alias = Array.isArray(page.alias) ? page.alias : [page.alias].filter(Boolean)
|
||||||
aliasCode = `${JSON.stringify(page.alias)}.concat(${aliasCode})`
|
if (alias.length) {
|
||||||
|
aliasCode = `${JSON.stringify(alias)}.concat(${aliasCode})`
|
||||||
}
|
}
|
||||||
|
|
||||||
const route = {
|
route.name = `${metaImportName}?.name ?? ${page.name ? JSON.stringify(page.name) : 'undefined'}`
|
||||||
...Object.fromEntries(Object.entries(page).map(([key, value]) => [key, JSON.stringify(value)])),
|
route.path = `${metaImportName}?.path ?? ${JSON.stringify(page.path)}`
|
||||||
file: undefined,
|
route.meta = page.meta && Object.values(page.meta).filter(value => value !== undefined).length ? `{...(${metaImportName} || {}), ...${JSON.stringify(page.meta)}}` : `${metaImportName} || {}`
|
||||||
name: `${metaImportName}?.name ?? ${page.name ? JSON.stringify(page.name) : 'undefined'}`,
|
route.alias = aliasCode
|
||||||
path: `${metaImportName}?.path ?? ${JSON.stringify(page.path)}`,
|
route.redirect = page.redirect ? JSON.stringify(page.redirect) : `${metaImportName}?.redirect || undefined`
|
||||||
children: page.children ? normalizeRoutes(page.children, metaImports).routes : [],
|
route.component = genDynamicImport(file, { interopDefault: true })
|
||||||
meta: page.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(page.meta)}}` : `${metaImportName} || {}`,
|
|
||||||
alias: aliasCode,
|
|
||||||
redirect: page.redirect ? JSON.stringify(page.redirect) : `${metaImportName}?.redirect || undefined`,
|
|
||||||
component: genDynamicImport(file, { interopDefault: true })
|
|
||||||
}
|
|
||||||
|
|
||||||
delete route.file
|
|
||||||
|
|
||||||
return route
|
return route
|
||||||
}))
|
}))
|
||||||
|
Binary file not shown.
@ -22,7 +22,7 @@ export type WatchEvent = 'add' | 'addDir' | 'change' | 'unlink' | 'unlinkDir'
|
|||||||
export type NuxtPage = {
|
export type NuxtPage = {
|
||||||
name?: string
|
name?: string
|
||||||
path: string
|
path: string
|
||||||
file: string
|
file?: string
|
||||||
meta?: Record<string, any>
|
meta?: Record<string, any>
|
||||||
alias?: string[] | string
|
alias?: string[] | string
|
||||||
redirect?: string
|
redirect?: string
|
||||||
|
Loading…
Reference in New Issue
Block a user