From 7131aef82045b297530f679782a1ce50b53daceb Mon Sep 17 00:00:00 2001 From: Ivan Date: Fri, 3 Mar 2023 17:07:42 +0300 Subject: [PATCH] feat(schema): allow adding page routes without a matching file (#19173) --- packages/nuxt/src/pages/module.ts | 8 ++-- packages/nuxt/src/pages/utils.ts | 42 +++++++++++------- .../treeshake-client.test.ts.snap | Bin 7081 -> 7121 bytes packages/schema/src/types/hooks.ts | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/packages/nuxt/src/pages/module.ts b/packages/nuxt/src/pages/module.ts index f7e85439ab..46fa4cc62d 100644 --- a/packages/nuxt/src/pages/module.ts +++ b/packages/nuxt/src/pages/module.ts @@ -146,9 +146,11 @@ export default defineNuxtModule({ // Add router plugin addPlugin(resolve(runtimeDir, 'plugins/router')) - const getSources = (pages: NuxtPage[]): string[] => pages.flatMap(p => - [relative(nuxt.options.srcDir, p.file), ...getSources(p.children || [])] - ) + const getSources = (pages: NuxtPage[]): string[] => pages + .filter(p => Boolean(p.file)) + .flatMap(p => + [relative(nuxt.options.srcDir, p.file as string), ...getSources(p.children || [])] + ) // Do not prefetch page chunks nuxt.hook('build:manifest', async (manifest) => { diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 946b67b5b0..66070c53e3 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -249,28 +249,40 @@ export function normalizeRoutes (routes: NuxtPage[], metaImports: Set = return { imports: metaImports, 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, 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 metaImportName = genSafeVariableName(filename(file) + hash(file)) + 'Meta' metaImports.add(genImport(`${file}?macro=true`, [{ name: 'default', as: metaImportName }])) let aliasCode = `${metaImportName}?.alias || []` - if (Array.isArray(page.alias) && page.alias.length) { - aliasCode = `${JSON.stringify(page.alias)}.concat(${aliasCode})` + const alias = Array.isArray(page.alias) ? page.alias : [page.alias].filter(Boolean) + if (alias.length) { + aliasCode = `${JSON.stringify(alias)}.concat(${aliasCode})` } - const route = { - ...Object.fromEntries(Object.entries(page).map(([key, value]) => [key, JSON.stringify(value)])), - file: undefined, - name: `${metaImportName}?.name ?? ${page.name ? JSON.stringify(page.name) : 'undefined'}`, - path: `${metaImportName}?.path ?? ${JSON.stringify(page.path)}`, - children: page.children ? normalizeRoutes(page.children, metaImports).routes : [], - 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 + route.name = `${metaImportName}?.name ?? ${page.name ? JSON.stringify(page.name) : 'undefined'}` + route.path = `${metaImportName}?.path ?? ${JSON.stringify(page.path)}` + route.meta = page.meta && Object.values(page.meta).filter(value => value !== undefined).length ? `{...(${metaImportName} || {}), ...${JSON.stringify(page.meta)}}` : `${metaImportName} || {}` + route.alias = aliasCode + route.redirect = page.redirect ? JSON.stringify(page.redirect) : `${metaImportName}?.redirect || undefined` + route.component = genDynamicImport(file, { interopDefault: true }) return route })) diff --git a/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap b/packages/nuxt/test/__snapshots__/treeshake-client.test.ts.snap index 76ed86bfa42083749de3bd61bc1e9c61cde343c0..8ca4bc8af731d57177b6607d112cfb5505f7ff6f 100644 GIT binary patch delta 55 zcmZ2!e$jk_sD_S0MoCFQv6a4lS!PLUafx0^YMFj|X=X~QesNx6L2*WYiC#uYZqDR& Ke8C$t9Hjv|wG^)a delta 10 Rcmca;zS4Yx=tkQq(f}EA1S
alias?: string[] | string redirect?: string