From 6af76334c5d10102fdbce182e626f218739cca4a Mon Sep 17 00:00:00 2001 From: Ahad Birang Date: Sun, 26 Jul 2020 20:17:56 +0430 Subject: [PATCH] fix(utils): include routes with children for generation (#7761) --- packages/utils/src/route.js | 29 ++++++++++++++++++++--------- packages/utils/test/route.test.js | 4 ++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/packages/utils/src/route.js b/packages/utils/src/route.js index cc619c1877..1b43c855d8 100644 --- a/packages/utils/src/route.js +++ b/packages/utils/src/route.js @@ -4,27 +4,38 @@ import consola from 'consola' 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 = []) { router.forEach((r) => { if ([':', '*'].some(c => r.path.includes(c))) { return } + const route = `${fileName}${r.path}/`.replace(/\/+/g, '/') if (r.children) { - if (fileName === '' && r.path === '/') { - routes.push('/') - } - - return flatRoutes(r.children, fileName + r.path + '/', routes) + return flatRoutes(routeChildren(r), route, routes) } - fileName = fileName.replace(/\/+/g, '/') // if child path is already absolute, do not make any concatenations if (r.path && r.path.startsWith('/')) { routes.push(r.path) - } else if (r.path === '' && fileName[fileName.length - 1] === '/') { - routes.push(fileName.slice(0, -1) + r.path) + } else if (route !== '/' && route[route.length - 1] === '/') { + routes.push(route.slice(0, -1)) } else { - routes.push(fileName + r.path) + routes.push(route) } }) return routes diff --git a/packages/utils/test/route.test.js b/packages/utils/test/route.test.js index 7422d55c13..3d31b5fa79 100644 --- a/packages/utils/test/route.test.js +++ b/packages/utils/test/route.test.js @@ -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 :', () => { @@ -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', () => {