2021-09-27 12:49:36 +00:00
|
|
|
import { basename, extname, relative, resolve } from 'pathe'
|
2021-01-18 12:22:38 +00:00
|
|
|
import { encodePath } from 'ufo'
|
2021-05-20 11:42:41 +00:00
|
|
|
import { Nuxt, resolveFiles } from '@nuxt/kit'
|
2021-06-30 16:32:22 +00:00
|
|
|
import { kebabCase } from 'scule'
|
2020-08-18 18:34:08 +00:00
|
|
|
|
|
|
|
export interface NuxtRoute {
|
2021-03-17 09:11:37 +00:00
|
|
|
name?: string
|
2020-08-18 18:34:08 +00:00
|
|
|
path: string
|
|
|
|
file: string
|
2021-01-18 12:22:38 +00:00
|
|
|
children: NuxtRoute[]
|
2020-08-18 18:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-18 14:26:41 +00:00
|
|
|
enum SegmentParserState {
|
|
|
|
initial,
|
|
|
|
static,
|
|
|
|
dynamic,
|
2021-06-21 12:09:08 +00:00
|
|
|
catchall,
|
2021-03-18 14:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enum SegmentTokenType {
|
|
|
|
static,
|
|
|
|
dynamic,
|
2021-06-21 12:09:08 +00:00
|
|
|
catchall,
|
2021-03-18 14:26:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface SegmentToken {
|
|
|
|
type: SegmentTokenType
|
|
|
|
value: string
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
export async function resolvePagesRoutes (nuxt: Nuxt) {
|
|
|
|
const pagesDir = resolve(nuxt.options.srcDir, nuxt.options.dir.pages)
|
|
|
|
const files = await resolveFiles(pagesDir, `**/*{${nuxt.options.extensions.join(',')}}`)
|
2020-08-18 18:34:08 +00:00
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
// Sort to make sure parent are listed first
|
2021-05-20 11:42:41 +00:00
|
|
|
files.sort()
|
|
|
|
|
|
|
|
return generateRoutesFromFiles(files, pagesDir)
|
2021-01-18 12:22:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
export function generateRoutesFromFiles (files: string[], pagesDir: string): NuxtRoute[] {
|
2020-08-18 18:34:08 +00:00
|
|
|
const routes: NuxtRoute[] = []
|
|
|
|
|
|
|
|
for (const file of files) {
|
2021-01-18 12:22:38 +00:00
|
|
|
const segments = relative(pagesDir, file)
|
2020-08-18 18:34:08 +00:00
|
|
|
.replace(new RegExp(`${extname(file)}$`), '')
|
|
|
|
.split('/')
|
|
|
|
|
|
|
|
const route: NuxtRoute = {
|
|
|
|
name: '',
|
|
|
|
path: '',
|
2021-01-18 12:22:38 +00:00
|
|
|
file,
|
|
|
|
children: []
|
2020-08-18 18:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:42:41 +00:00
|
|
|
// Array where routes should be added, useful when adding child routes
|
2020-08-18 18:34:08 +00:00
|
|
|
let parent = routes
|
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
for (let i = 0; i < segments.length; i++) {
|
|
|
|
const segment = segments[i]
|
2020-08-18 18:34:08 +00:00
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
const tokens = parseSegment(segment)
|
|
|
|
const segmentName = tokens.map(({ value }) => value).join('')
|
|
|
|
const isSingleSegment = segments.length === 1
|
|
|
|
const isLastSegment = i === segments.length - 1
|
2020-08-18 18:34:08 +00:00
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
// ex: parent/[slug].vue -> parent-slug
|
|
|
|
route.name += (route.name && '-') + segmentName
|
|
|
|
|
|
|
|
// ex: parent.vue + parent/child.vue
|
|
|
|
const child = parent.find(parentRoute => parentRoute.name === route.name)
|
2020-08-18 18:34:08 +00:00
|
|
|
if (child) {
|
|
|
|
parent = child.children
|
|
|
|
route.path = ''
|
2021-01-18 12:22:38 +00:00
|
|
|
} else if (segmentName === '404' && isSingleSegment) {
|
|
|
|
route.path += '/:catchAll(.*)*'
|
|
|
|
} else if (segmentName === 'index' && !route.path) {
|
2020-08-18 18:34:08 +00:00
|
|
|
route.path += '/'
|
2021-01-18 12:22:38 +00:00
|
|
|
} else if (segmentName !== 'index') {
|
|
|
|
route.path += getRoutePath(tokens)
|
|
|
|
if (isLastSegment && tokens.length === 1 && tokens[0].type === SegmentTokenType.dynamic) {
|
|
|
|
route.path += '?'
|
2020-08-18 18:34:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
parent.push(route)
|
|
|
|
}
|
|
|
|
|
|
|
|
return prepareRoutes(routes)
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
function getRoutePath (tokens: SegmentToken[]): string {
|
|
|
|
return tokens.reduce((path, token) => {
|
|
|
|
return (
|
|
|
|
path +
|
|
|
|
(token.type === SegmentTokenType.dynamic
|
|
|
|
? `:${token.value}`
|
2021-06-21 12:09:08 +00:00
|
|
|
: token.type === SegmentTokenType.catchall
|
|
|
|
? `:${token.value}(.*)*`
|
|
|
|
: encodePath(token.value))
|
2021-01-18 12:22:38 +00:00
|
|
|
)
|
|
|
|
}, '/')
|
|
|
|
}
|
|
|
|
|
2021-06-21 12:09:08 +00:00
|
|
|
const PARAM_CHAR_RE = /[\w\d_.]/
|
2021-01-18 12:22:38 +00:00
|
|
|
|
|
|
|
function parseSegment (segment: string) {
|
2021-06-21 12:09:08 +00:00
|
|
|
let state: SegmentParserState = SegmentParserState.initial
|
2021-01-18 12:22:38 +00:00
|
|
|
let i = 0
|
|
|
|
|
|
|
|
let buffer = ''
|
|
|
|
const tokens: SegmentToken[] = []
|
|
|
|
|
|
|
|
function consumeBuffer () {
|
|
|
|
if (!buffer) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (state === SegmentParserState.initial) {
|
|
|
|
throw new Error('wrong state')
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
type:
|
|
|
|
state === SegmentParserState.static
|
|
|
|
? SegmentTokenType.static
|
2021-06-21 12:09:08 +00:00
|
|
|
: state === SegmentParserState.dynamic
|
|
|
|
? SegmentTokenType.dynamic
|
|
|
|
: SegmentTokenType.catchall,
|
2021-01-18 12:22:38 +00:00
|
|
|
value: buffer
|
|
|
|
})
|
|
|
|
|
|
|
|
buffer = ''
|
|
|
|
}
|
|
|
|
|
|
|
|
while (i < segment.length) {
|
|
|
|
const c = segment[i]
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case SegmentParserState.initial:
|
|
|
|
buffer = ''
|
|
|
|
if (c === '[') {
|
|
|
|
state = SegmentParserState.dynamic
|
|
|
|
} else {
|
|
|
|
i--
|
|
|
|
state = SegmentParserState.static
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case SegmentParserState.static:
|
|
|
|
if (c === '[') {
|
|
|
|
consumeBuffer()
|
|
|
|
state = SegmentParserState.dynamic
|
|
|
|
} else {
|
|
|
|
buffer += c
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2021-06-21 12:09:08 +00:00
|
|
|
case SegmentParserState.catchall:
|
2021-01-18 12:22:38 +00:00
|
|
|
case SegmentParserState.dynamic:
|
2021-06-21 12:09:08 +00:00
|
|
|
if (buffer === '...') {
|
|
|
|
buffer = ''
|
|
|
|
state = SegmentParserState.catchall
|
|
|
|
}
|
2021-01-18 12:22:38 +00:00
|
|
|
if (c === ']') {
|
2021-10-20 18:12:55 +00:00
|
|
|
if (!buffer) {
|
|
|
|
throw new Error('Empty param')
|
|
|
|
} else {
|
|
|
|
consumeBuffer()
|
|
|
|
}
|
2021-01-18 12:22:38 +00:00
|
|
|
state = SegmentParserState.initial
|
|
|
|
} else if (PARAM_CHAR_RE.test(c)) {
|
|
|
|
buffer += c
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line no-console
|
2021-10-20 18:12:55 +00:00
|
|
|
// console.debug(`[pages]Ignored character "${c}" while building param "${buffer}" from "segment"`)
|
2021-01-18 12:22:38 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state === SegmentParserState.dynamic) {
|
|
|
|
throw new Error(`Unfinished param "${buffer}"`)
|
|
|
|
}
|
|
|
|
|
|
|
|
consumeBuffer()
|
|
|
|
|
|
|
|
return tokens
|
|
|
|
}
|
|
|
|
|
|
|
|
function prepareRoutes (routes: NuxtRoute[], parent?: NuxtRoute) {
|
2020-08-18 18:34:08 +00:00
|
|
|
for (const route of routes) {
|
2021-01-18 12:22:38 +00:00
|
|
|
// Remove -index
|
2020-08-18 18:34:08 +00:00
|
|
|
if (route.name) {
|
|
|
|
route.name = route.name.replace(/-index$/, '')
|
|
|
|
}
|
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
if (route.path === '/') {
|
|
|
|
// Remove ? suffix when index page at same level
|
|
|
|
routes.forEach((siblingRoute) => {
|
|
|
|
if (siblingRoute.path.endsWith('?')) {
|
|
|
|
siblingRoute.path = siblingRoute.path.slice(0, -1)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Remove leading / if children route
|
|
|
|
if (parent && route.path.startsWith('/')) {
|
|
|
|
route.path = route.path.slice(1)
|
2020-08-18 18:34:08 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 12:22:38 +00:00
|
|
|
if (route.children.length) {
|
|
|
|
route.children = prepareRoutes(route.children, route)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (route.children.find(childRoute => childRoute.path === '')) {
|
2020-08-18 18:34:08 +00:00
|
|
|
delete route.name
|
|
|
|
}
|
|
|
|
}
|
2021-01-18 12:22:38 +00:00
|
|
|
|
2020-08-18 18:34:08 +00:00
|
|
|
return routes
|
|
|
|
}
|
2021-06-30 16:32:22 +00:00
|
|
|
|
|
|
|
export async function resolveLayouts (nuxt: Nuxt) {
|
|
|
|
const layoutDir = resolve(nuxt.options.srcDir, nuxt.options.dir.layouts)
|
|
|
|
const files = await resolveFiles(layoutDir, `*{${nuxt.options.extensions.join(',')}}`)
|
|
|
|
|
|
|
|
return files.map((file) => {
|
|
|
|
const name = kebabCase(basename(file).replace(extname(file), '')).replace(/["']/g, '')
|
|
|
|
return { name, file }
|
|
|
|
})
|
|
|
|
}
|
2021-10-20 18:49:15 +00:00
|
|
|
|
|
|
|
export function addComponentToRoutes (routes: NuxtRoute[]) {
|
|
|
|
return routes.map(route => ({
|
|
|
|
...route,
|
|
|
|
children: addComponentToRoutes(route.children),
|
|
|
|
component: `{() => import('${route.file}')}`
|
|
|
|
}))
|
|
|
|
}
|