fix(nuxt): remove absolute paths from routes objects (#9655)

This commit is contained in:
Daniel Roe 2022-12-12 12:25:00 +00:00 committed by GitHub
parent 050c6dfd8a
commit 23307cd8e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -223,28 +223,34 @@ function prepareRoutes (routes: NuxtPage[], parent?: NuxtPage) {
}
export function normalizeRoutes (routes: NuxtPage[], metaImports: Set<string> = new Set()): { imports: Set<string>, 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
}))
}
}