fix(utils): include routes with children for generation (#7761)

This commit is contained in:
Ahad Birang 2020-07-26 20:17:56 +04:30 committed by GitHub
parent 45f204c12c
commit 6af76334c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 11 deletions

View File

@ -4,27 +4,38 @@ import consola from 'consola'
import { r } from './resolve' import { r } from './resolve'
const routeChildren = function (route) {
const hasChildWithEmptyPath = route.children.some(child => child.path === '')
if (hasChildWithEmptyPath) {
return route.children
}
return [
// Add default child to render parent page
{
...route,
children: undefined
},
...route.children
]
}
export const flatRoutes = function flatRoutes (router, fileName = '', routes = []) { export const flatRoutes = function flatRoutes (router, fileName = '', routes = []) {
router.forEach((r) => { router.forEach((r) => {
if ([':', '*'].some(c => r.path.includes(c))) { if ([':', '*'].some(c => r.path.includes(c))) {
return return
} }
const route = `${fileName}${r.path}/`.replace(/\/+/g, '/')
if (r.children) { if (r.children) {
if (fileName === '' && r.path === '/') { return flatRoutes(routeChildren(r), route, routes)
routes.push('/')
}
return flatRoutes(r.children, fileName + r.path + '/', routes)
} }
fileName = fileName.replace(/\/+/g, '/')
// if child path is already absolute, do not make any concatenations // if child path is already absolute, do not make any concatenations
if (r.path && r.path.startsWith('/')) { if (r.path && r.path.startsWith('/')) {
routes.push(r.path) routes.push(r.path)
} else if (r.path === '' && fileName[fileName.length - 1] === '/') { } else if (route !== '/' && route[route.length - 1] === '/') {
routes.push(fileName.slice(0, -1) + r.path) routes.push(route.slice(0, -1))
} else { } else {
routes.push(fileName + r.path) routes.push(route)
} }
}) })
return routes return routes

View File

@ -14,7 +14,7 @@ describe('util: route', () => {
] ]
} }
]) ])
expect(routes).toEqual(['/login', '/about', '', '/post']) expect(routes).toEqual(['/login', '/about', '/', '/post'])
}) })
test('should ignore route with * and :', () => { test('should ignore route with * and :', () => {
@ -52,7 +52,7 @@ describe('util: route', () => {
} }
]) ])
expect(routes).toEqual(['/foo/bar', '/foo/baz']) expect(routes).toEqual(['/foo', '/foo/bar', '/foo/baz'])
}) })
test('should flat absolute routes with empty path', () => { test('should flat absolute routes with empty path', () => {