2018-03-16 19:11:24 +00:00
|
|
|
import path from 'path'
|
2018-10-18 15:43:44 +00:00
|
|
|
import get from 'lodash/get'
|
2018-09-14 07:06:44 +00:00
|
|
|
import consola from 'consola'
|
2020-12-17 11:51:14 +00:00
|
|
|
import { normalizeURL, withTrailingSlash, withoutTrailingSlash } from '@nuxt/ufo'
|
2018-12-22 21:05:13 +00:00
|
|
|
import { r } from './resolve'
|
2017-06-14 16:13:43 +00:00
|
|
|
|
2020-07-26 15:47:56 +00:00
|
|
|
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
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const flatRoutes = function flatRoutes (router, fileName = '', routes = []) {
|
2018-08-06 00:12:44 +00:00
|
|
|
router.forEach((r) => {
|
2018-10-12 22:31:19 +00:00
|
|
|
if ([':', '*'].some(c => r.path.includes(c))) {
|
|
|
|
return
|
|
|
|
}
|
2020-07-26 15:47:56 +00:00
|
|
|
const route = `${fileName}${r.path}/`.replace(/\/+/g, '/')
|
2018-10-12 22:31:19 +00:00
|
|
|
if (r.children) {
|
2020-07-26 15:47:56 +00:00
|
|
|
return flatRoutes(routeChildren(r), route, routes)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2020-06-25 12:42:16 +00:00
|
|
|
|
|
|
|
// if child path is already absolute, do not make any concatenations
|
|
|
|
if (r.path && r.path.startsWith('/')) {
|
|
|
|
routes.push(r.path)
|
2020-07-26 15:47:56 +00:00
|
|
|
} else if (route !== '/' && route[route.length - 1] === '/') {
|
|
|
|
routes.push(route.slice(0, -1))
|
2020-06-25 12:42:16 +00:00
|
|
|
} else {
|
2020-07-26 15:47:56 +00:00
|
|
|
routes.push(route)
|
2020-06-25 12:42:16 +00:00
|
|
|
}
|
2017-06-14 16:13:43 +00:00
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2020-11-14 17:10:53 +00:00
|
|
|
// eslint-disable-next-line default-param-last
|
2020-08-04 09:49:12 +00:00
|
|
|
function cleanChildrenRoutes (routes, isChild = false, routeNameSplitter = '-', trailingSlash, parentRouteName) {
|
2018-12-20 12:42:42 +00:00
|
|
|
const regExpIndex = new RegExp(`${routeNameSplitter}index$`)
|
2020-08-04 09:49:12 +00:00
|
|
|
const regExpParentRouteName = new RegExp(`^${parentRouteName}${routeNameSplitter}`)
|
2018-08-08 10:54:05 +00:00
|
|
|
const routesIndex = []
|
2018-08-06 00:12:44 +00:00
|
|
|
routes.forEach((route) => {
|
2018-12-20 12:42:42 +00:00
|
|
|
if (regExpIndex.test(route.name) || route.name === 'index') {
|
2020-08-04 09:49:12 +00:00
|
|
|
const res = route.name.replace(regExpParentRouteName, '').split(routeNameSplitter)
|
2017-06-14 16:13:43 +00:00
|
|
|
routesIndex.push(res)
|
|
|
|
}
|
|
|
|
})
|
2018-08-06 00:12:44 +00:00
|
|
|
routes.forEach((route) => {
|
2018-01-11 14:11:49 +00:00
|
|
|
route.path = isChild ? route.path.replace('/', '') : route.path
|
2018-10-12 22:31:19 +00:00
|
|
|
if (route.path.includes('?')) {
|
2020-08-04 13:03:00 +00:00
|
|
|
if (route.name.endsWith(`${routeNameSplitter}index`)) {
|
|
|
|
route.path = route.path.replace(/\?$/, '')
|
|
|
|
}
|
2020-08-04 09:49:12 +00:00
|
|
|
const names = route.name.replace(regExpParentRouteName, '').split(routeNameSplitter)
|
2018-08-08 10:54:05 +00:00
|
|
|
const paths = route.path.split('/')
|
2017-06-14 16:13:43 +00:00
|
|
|
if (!isChild) {
|
|
|
|
paths.shift()
|
|
|
|
} // clean first / for parents
|
2018-08-06 00:12:44 +00:00
|
|
|
routesIndex.forEach((r) => {
|
2020-08-04 09:49:12 +00:00
|
|
|
const i = r.indexOf('index')
|
2017-06-14 16:13:43 +00:00
|
|
|
if (i < paths.length) {
|
|
|
|
for (let a = 0; a <= i; a++) {
|
|
|
|
if (a === i) {
|
|
|
|
paths[a] = paths[a].replace('?', '')
|
|
|
|
}
|
|
|
|
if (a < i && names[a] !== r[a]) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
route.path = (isChild ? '' : '/') + paths.join('/')
|
|
|
|
}
|
2018-12-20 12:42:42 +00:00
|
|
|
route.name = route.name.replace(regExpIndex, '')
|
2017-06-14 16:13:43 +00:00
|
|
|
if (route.children) {
|
2020-07-09 13:08:12 +00:00
|
|
|
const indexRoutePath = trailingSlash === false ? '/' : ''
|
|
|
|
const defaultChildRoute = route.children.find(child => child.path === indexRoutePath)
|
2020-08-04 09:49:12 +00:00
|
|
|
const routeName = route.name
|
2020-07-09 13:08:12 +00:00
|
|
|
if (defaultChildRoute) {
|
|
|
|
if (trailingSlash === false) {
|
|
|
|
defaultChildRoute.name = route.name
|
|
|
|
}
|
2020-11-30 22:08:33 +00:00
|
|
|
route.children.forEach((child) => {
|
|
|
|
if (child.path !== indexRoutePath) {
|
|
|
|
const parts = child.path.split('/')
|
|
|
|
parts[1] = parts[1].endsWith('?') ? parts[1].substr(0, parts[1].length - 1) : parts[1]
|
|
|
|
child.path = parts.join('/')
|
|
|
|
}
|
|
|
|
})
|
2017-06-14 16:13:43 +00:00
|
|
|
delete route.name
|
|
|
|
}
|
2020-08-04 09:49:12 +00:00
|
|
|
route.children = cleanChildrenRoutes(route.children, true, routeNameSplitter, trailingSlash, routeName)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2018-12-12 06:31:49 +00:00
|
|
|
const DYNAMIC_ROUTE_REGEX = /^\/([:*])/
|
2018-11-14 16:50:06 +00:00
|
|
|
|
2019-09-01 16:24:21 +00:00
|
|
|
export const sortRoutes = function sortRoutes (routes) {
|
2018-11-14 16:50:06 +00:00
|
|
|
routes.sort((a, b) => {
|
|
|
|
if (!a.path.length) {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
if (!b.path.length) {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
// Order: /static, /index, /:dynamic
|
|
|
|
// Match exact route before index: /login before /index/_slug
|
|
|
|
if (a.path === '/') {
|
|
|
|
return DYNAMIC_ROUTE_REGEX.test(b.path) ? -1 : 1
|
|
|
|
}
|
|
|
|
if (b.path === '/') {
|
|
|
|
return DYNAMIC_ROUTE_REGEX.test(a.path) ? 1 : -1
|
|
|
|
}
|
|
|
|
|
|
|
|
let i
|
|
|
|
let res = 0
|
|
|
|
let y = 0
|
|
|
|
let z = 0
|
|
|
|
const _a = a.path.split('/')
|
|
|
|
const _b = b.path.split('/')
|
|
|
|
for (i = 0; i < _a.length; i++) {
|
|
|
|
if (res !== 0) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
y = _a[i] === '*' ? 2 : _a[i].includes(':') ? 1 : 0
|
|
|
|
z = _b[i] === '*' ? 2 : _b[i].includes(':') ? 1 : 0
|
|
|
|
res = y - z
|
|
|
|
// If a.length >= b.length
|
|
|
|
if (i === _b.length - 1 && res === 0) {
|
|
|
|
// unless * found sort by level, then alphabetically
|
2020-11-14 17:10:53 +00:00
|
|
|
res = _a[i] === '*'
|
|
|
|
? -1
|
|
|
|
: (
|
|
|
|
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
|
|
|
|
)
|
2018-11-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res === 0) {
|
|
|
|
// unless * found sort by level, then alphabetically
|
2020-11-14 17:10:53 +00:00
|
|
|
res = _a[i - 1] === '*' && _b[i]
|
|
|
|
? 1
|
|
|
|
: (
|
|
|
|
_a.length === _b.length ? a.path.localeCompare(b.path) : (_a.length - _b.length)
|
|
|
|
)
|
2018-11-14 16:50:06 +00:00
|
|
|
}
|
|
|
|
return res
|
|
|
|
})
|
|
|
|
|
|
|
|
routes.forEach((route) => {
|
|
|
|
if (route.children) {
|
|
|
|
sortRoutes(route.children)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return routes
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const createRoutes = function createRoutes ({
|
2019-06-04 19:48:01 +00:00
|
|
|
files,
|
|
|
|
srcDir,
|
|
|
|
pagesDir = '',
|
|
|
|
routeNameSplitter = '-',
|
2019-09-01 14:37:25 +00:00
|
|
|
supportedExtensions = ['vue', 'js'],
|
|
|
|
trailingSlash
|
2019-06-04 19:48:01 +00:00
|
|
|
}) {
|
2018-08-08 10:54:05 +00:00
|
|
|
const routes = []
|
2018-08-06 00:12:44 +00:00
|
|
|
files.forEach((file) => {
|
2018-08-08 10:54:05 +00:00
|
|
|
const keys = file
|
2018-12-15 06:55:08 +00:00
|
|
|
.replace(new RegExp(`^${pagesDir}`), '')
|
|
|
|
.replace(new RegExp(`\\.(${supportedExtensions.join('|')})$`), '')
|
2018-01-11 14:11:49 +00:00
|
|
|
.replace(/\/{2,}/g, '/')
|
|
|
|
.split('/')
|
|
|
|
.slice(1)
|
2018-08-08 10:54:05 +00:00
|
|
|
const route = { name: '', path: '', component: r(srcDir, file) }
|
2017-06-14 16:13:43 +00:00
|
|
|
let parent = routes
|
|
|
|
keys.forEach((key, i) => {
|
2018-03-11 13:59:37 +00:00
|
|
|
// remove underscore only, if its the prefix
|
2018-10-12 22:31:19 +00:00
|
|
|
const sanitizedKey = key.startsWith('_') ? key.substr(1) : key
|
|
|
|
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name = route.name
|
2018-12-20 12:42:42 +00:00
|
|
|
? route.name + routeNameSplitter + sanitizedKey
|
2018-07-18 14:39:48 +00:00
|
|
|
: sanitizedKey
|
2018-01-11 14:11:49 +00:00
|
|
|
route.name += key === '_' ? 'all' : ''
|
2018-12-15 06:55:08 +00:00
|
|
|
route.chunkName = file.replace(new RegExp(`\\.(${supportedExtensions.join('|')})$`), '')
|
2018-10-12 22:31:19 +00:00
|
|
|
const child = parent.find(parentRoute => parentRoute.name === route.name)
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
if (child) {
|
2017-08-25 11:54:14 +00:00
|
|
|
child.children = child.children || []
|
2017-06-14 16:13:43 +00:00
|
|
|
parent = child.children
|
|
|
|
route.path = ''
|
2018-08-10 23:24:53 +00:00
|
|
|
} else if (key === 'index' && i + 1 === keys.length) {
|
|
|
|
route.path += i > 0 ? '' : '/'
|
2017-06-14 16:13:43 +00:00
|
|
|
} else {
|
2020-12-09 23:19:31 +00:00
|
|
|
route.path += '/' + normalizeURL(getRoutePathExtension(key))
|
2020-12-06 17:32:39 +00:00
|
|
|
if (key.startsWith('_') && key.length > 1) {
|
2018-08-10 23:24:53 +00:00
|
|
|
route.path += '?'
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-09-01 14:37:25 +00:00
|
|
|
if (trailingSlash !== undefined) {
|
|
|
|
route.pathToRegexpOptions = { ...route.pathToRegexpOptions, strict: true }
|
2020-12-17 11:51:14 +00:00
|
|
|
if (trailingSlash && !route.path.endsWith('*')) {
|
|
|
|
route.path = withTrailingSlash(route.path)
|
|
|
|
} else {
|
|
|
|
route.path = withoutTrailingSlash(route.path) || '/'
|
|
|
|
}
|
2019-09-01 14:37:25 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 16:13:43 +00:00
|
|
|
parent.push(route)
|
|
|
|
})
|
2018-11-14 16:50:06 +00:00
|
|
|
|
|
|
|
sortRoutes(routes)
|
2020-07-09 13:08:12 +00:00
|
|
|
return cleanChildrenRoutes(routes, false, routeNameSplitter, trailingSlash)
|
2017-06-14 16:13:43 +00:00
|
|
|
}
|
2018-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
// Guard dir1 from dir2 which can be indiscriminately removed
|
2019-07-10 10:45:49 +00:00
|
|
|
export const guardDir = function guardDir (options, key1, key2) {
|
2018-10-18 15:43:44 +00:00
|
|
|
const dir1 = get(options, key1, false)
|
|
|
|
const dir2 = get(options, key2, false)
|
2018-09-14 07:06:44 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
dir1 &&
|
|
|
|
dir2 &&
|
|
|
|
(
|
|
|
|
dir1 === dir2 ||
|
|
|
|
(
|
|
|
|
dir1.startsWith(dir2) &&
|
|
|
|
!path.basename(dir1).startsWith(path.basename(dir2))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
const errorMessage = `options.${key2} cannot be a parent of or same as ${key1}`
|
|
|
|
consola.fatal(errorMessage)
|
|
|
|
throw new Error(errorMessage)
|
|
|
|
}
|
|
|
|
}
|
2018-10-09 12:07:23 +00:00
|
|
|
|
2018-10-12 22:31:19 +00:00
|
|
|
const getRoutePathExtension = (key) => {
|
|
|
|
if (key === '_') {
|
|
|
|
return '*'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.startsWith('_')) {
|
|
|
|
return `:${key.substr(1)}`
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2019-07-10 10:45:49 +00:00
|
|
|
export const promisifyRoute = function promisifyRoute (fn, ...args) {
|
2018-12-22 21:05:13 +00:00
|
|
|
// If routes is an array
|
|
|
|
if (Array.isArray(fn)) {
|
|
|
|
return Promise.resolve(fn)
|
2018-10-30 20:42:53 +00:00
|
|
|
}
|
2018-12-22 21:05:13 +00:00
|
|
|
// If routes is a function expecting a callback
|
|
|
|
if (fn.length === arguments.length) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
fn((err, routeParams) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
|
|
|
resolve(routeParams)
|
|
|
|
}, ...args)
|
2018-12-15 08:37:31 +00:00
|
|
|
})
|
2018-12-22 21:05:13 +00:00
|
|
|
}
|
|
|
|
let promise = fn(...args)
|
|
|
|
if (
|
|
|
|
!promise ||
|
|
|
|
(!(promise instanceof Promise) && typeof promise.then !== 'function')
|
|
|
|
) {
|
|
|
|
promise = Promise.resolve(promise)
|
|
|
|
}
|
|
|
|
return promise
|
2018-12-15 08:37:31 +00:00
|
|
|
}
|