Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extendRoutes): allow relative path overrides #519

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/core/context.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ResolvedOptions } from '../options'
import { TreeNode, PrefixTree } from './tree'
import { promises as fs } from 'fs'
import { asRoutePath, ImportsMap, logTree, throttle } from './utils'
import { asRoutePath, ImportsMap, joinPath, logTree, throttle } from './utils'
import { generateRouteNamedMap } from '../codegen/generateRouteMap'
import { MODULE_ROUTES_PATH, MODULE_VUE_ROUTER_AUTO } from './moduleConstants'
import { generateRouteRecord } from '../codegen/generateRouteRecords'
Expand Down Expand Up @@ -114,10 +114,26 @@ export function createRoutesContext(options: ResolvedOptions) {
const routeBlock = getRouteBlock(filePath, content, options)
// TODO: should warn if hasDefinePage and customRouteBlock
// if (routeBlock) logger.log(routeBlock)
node.setCustomRouteBlock(filePath, {

const routeOverrides = {
...routeBlock,
...definedPageNameAndPath,
})
}

if (routeOverrides.path != null && !routeOverrides.path.startsWith('/')) {
let parent = node.parent

while (parent && !parent.value.components.size) {
parent = parent.parent
}

routeOverrides.path = joinPath(
parent?.value.path || '',
routeOverrides.path
)
}

node.setCustomRouteBlock(filePath, routeOverrides)
}

async function addPage(
Expand Down
13 changes: 1 addition & 12 deletions src/core/customBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,7 @@ export function getRouteBlock(
const parsedSFC = parse(content, { pad: 'space' }).descriptor
const blockStr = parsedSFC?.customBlocks.find((b) => b.type === 'route')

if (!blockStr) return

let result = parseCustomBlock(blockStr, path, options)

// validation
if (result) {
if (result.path != null && !result.path.startsWith('/')) {
warn(`Overridden path must start with "/". Found in "${path}".`)
}
}

return result
if (blockStr) return parseCustomBlock(blockStr, path, options)
}

export interface CustomRouteBlock
Expand Down
19 changes: 19 additions & 0 deletions src/core/extendRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,23 @@ describe('EditableTreeNode', () => {
},
])
})

it('can override the path', () => {
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)
const parent = editable.insert('parent', 'file.vue')
const child = parent.insert('child', 'file.vue')
const grandChild = child.insert('grandchild', 'file.vue')

child.path = 'relative'
parent.path = 'relative'
expect(parent.path).toBe('/parent')
expect(child.path).toBe('relative')
expect(child.fullPath).toBe('/parent/relative')
expect(grandChild.fullPath).toBe('/parent/relative/grandchild')
child.path = '/absolute'
expect(child.path).toBe('/absolute')
expect(child.fullPath).toBe('/absolute')
expect(grandChild.fullPath).toBe('/absolute/grandchild')
})
})
7 changes: 5 additions & 2 deletions src/core/extendRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,12 @@ export class EditableTreeNode {
* Override the path of the route. You must ensure `params` match with the existing path.
*/
set path(path: string) {
if (!path.startsWith('/')) {
if (
(!this.node.parent || this.node.parent.isRoot()) &&
!path.startsWith('/')
) {
warn(
`Only absolute paths are supported. Make sure that "${path}" starts with a slash "/".`
`Only absolute paths are supported at the root of the route tree. Make sure that "${path}" starts with a slash "/".`
)
return
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ export class TreeNode {
* Returns the route path of the node including parent paths.
*/
get fullPath() {
return this.value.overrides.path ?? this.value.path
return this.value.path
}

/**
Expand Down
7 changes: 6 additions & 1 deletion src/core/treeNodeValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,14 @@ class _TreeNodeValueBase {
* fullPath of the node based on parent nodes
*/
get path(): string {
const parentPath = this.parent?.path
if (this.overrides.path?.startsWith('/')) {
return this.overrides.path
}

// both the root record and the index record have a path of /
const pathSegment = this.overrides.path ?? this.pathSegment
const parentPath = this.parent?.path

return (!parentPath || parentPath === '/') && pathSegment === ''
? '/'
: joinPath(parentPath || '', pathSegment)
Expand Down