diff --git a/packages/nuxt/src/pages/utils.ts b/packages/nuxt/src/pages/utils.ts index 36a7541d5a..18e146ae54 100644 --- a/packages/nuxt/src/pages/utils.ts +++ b/packages/nuxt/src/pages/utils.ts @@ -223,28 +223,34 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage) { } export function normalizeRoutes (routes: NuxtPage[], metaImports: Set = new Set()): { imports: Set, routes: string } { + const nuxt = useNuxt() return { imports: metaImports, - routes: genArrayFromRaw(routes.map((route) => { - const file = normalize(route.file) - const metaImportName = genSafeVariableName(file) + 'Meta' + routes: genArrayFromRaw(routes.map((page) => { + const file = normalize(page.file) + const metaImportName = genSafeVariableName(relative(nuxt.options.rootDir, file)) + 'Meta' metaImports.add(genImport(`${file}?macro=true`, [{ name: 'default', as: metaImportName }])) let aliasCode = `${metaImportName}?.alias || []` - if (Array.isArray(route.alias) && route.alias.length) { - aliasCode = `${JSON.stringify(route.alias)}.concat(${aliasCode})` + if (Array.isArray(page.alias) && page.alias.length) { + aliasCode = `${JSON.stringify(page.alias)}.concat(${aliasCode})` } - return { - ...Object.fromEntries(Object.entries(route).map(([key, value]) => [key, JSON.stringify(value)])), - name: `${metaImportName}?.name ?? ${route.name ? JSON.stringify(route.name) : 'undefined'}`, - path: `${metaImportName}?.path ?? ${JSON.stringify(route.path)}`, - children: route.children ? normalizeRoutes(route.children, metaImports).routes : [], - meta: route.meta ? `{...(${metaImportName} || {}), ...${JSON.stringify(route.meta)}}` : metaImportName, + 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: route.redirect ? JSON.stringify(route.redirect) : `${metaImportName}?.redirect || undefined`, + redirect: page.redirect ? JSON.stringify(page.redirect) : `${metaImportName}?.redirect || undefined`, component: genDynamicImport(file, { interopDefault: true }) } + + delete route.file + + return route })) } }