From b33008ee887c659170f3ca76db821b58d99dd42c Mon Sep 17 00:00:00 2001 From: Alexandre Chopin Date: Fri, 23 Dec 2016 17:31:42 +0100 Subject: [PATCH] fix dynamic routes --- lib/build/index.js | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/build/index.js b/lib/build/index.js index 8e5f488527..8d47588799 100644 --- a/lib/build/index.js +++ b/lib/build/index.js @@ -198,15 +198,23 @@ function createRoutes (files, srcDir) { let route = { name: '', path: '', component: r(srcDir, file), _name: null } let parent = routes keys.forEach((key, i) => { - route.name = route.name ? route.name + (key === 'index' ? '' : '-' + key.replace('_', '')) : key.replace('_', '') + route.name = route.name ? route.name + '-' + key.replace('_', '') : key.replace('_', '') let child = _.find(parent, { name: route.name }) if (child) { if (!child.children) { child.children = [] } parent = child.children + route.path = '' } else { - route.path = route.path + (key === 'index' ? (i > 0 ? '' : '/') : '/' + key.replace('_', ':')) + if (key === 'index' && (i + 1) === keys.length) { + route.path += (i > 0 ? '' : '/') + } else { + route.path += '/' + key.replace('_', ':') + if (key.includes('_')) { + route.path += '?' + } + } } }) route._name = '_' + hash(route.component) @@ -222,15 +230,22 @@ function createRoutes (files, srcDir) { } function cleanChildrenRoutes (routes, isChild = false) { - let isOptional = true + let hasIndex = false + let parents = [] routes.forEach((route) => { route.path = (isChild) ? route.path.replace('/', '') : route.path - if (route.path === '' || route.path === '/') { - isOptional = false + if ((isChild && /-index$/.test(route.name)) || (!isChild && route.name === 'index')) { + hasIndex = true } - if (isOptional && route.path.includes(':')) { - route.path += '?' + route.path = (hasIndex) ? route.path.replace('?', '') : route.path + if (/-index$/.test(route.name)) { + parents.push(route.name) + } else { + if (parents.indexOf(route.name.split('-').slice(0, -1).join('-') + '-index') > -1) { + route.path = route.path.replace('?', '') + } } + route.name = route.name.replace(/-index$/, '') if (route.children) { delete route.name route.children = cleanChildrenRoutes(route.children, true)