Skip to content

Commit

Permalink
fix: watch all extensions not just root
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed May 22, 2024
1 parent 9a74ed1 commit 74ac22b
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 116 deletions.
1 change: 0 additions & 1 deletion playground/typed-router.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ declare module 'vue-router/auto-routes' {
'/n-[[n]]/': RouteRecordInfo<'/n-[[n]]/', '/n-:n?', { n?: ParamValueZeroOrOne<true> }, { n?: ParamValueZeroOrOne<false> }>,
'/n-[[n]]/[[more]]+/': RouteRecordInfo<'/n-[[n]]/[[more]]+/', '/n-:n?/:more*', { n?: ParamValueZeroOrOne<true>, more?: ParamValueZeroOrMore<true> }, { n?: ParamValueZeroOrOne<false>, more?: ParamValueZeroOrMore<false> }>,
'/n-[[n]]/[[more]]+/[final]': RouteRecordInfo<'/n-[[n]]/[[more]]+/[final]', '/n-:n?/:more*/:final', { n?: ParamValueZeroOrOne<true>, more?: ParamValueZeroOrMore<true>, final: ParamValue<true> }, { n?: ParamValueZeroOrOne<false>, more?: ParamValueZeroOrMore<false>, final: ParamValue<false> }>,
'/not-used': RouteRecordInfo<'/not-used', '/not-used', Record<never, never>, Record<never, never>>,
'/partial-[name]': RouteRecordInfo<'/partial-[name]', '/partial-:name', { name: ParamValue<true> }, { name: ParamValue<false> }>,
'/custom-path': RouteRecordInfo<'/custom-path', '/surprise-:id(\d+)', Record<never, never>, Record<never, never>>,
'/todos/': RouteRecordInfo<'/todos/', '/todos', Record<never, never>, Record<never, never>>,
Expand Down
2 changes: 1 addition & 1 deletion playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default defineConfig({

plugins: [
VueRouter({
extensions: ['.page.vue', '.vue', '.md'],
extensions: ['.page.vue', '.vue'],
importMode: 'async',
extendRoute(route) {
// console.log('extending route', route.meta)
Expand Down
31 changes: 16 additions & 15 deletions src/core/extendRoutes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { expect, describe, it } from 'vitest'
import { PrefixTree } from './tree'
import { DEFAULT_OPTIONS } from '../options'
import { DEFAULT_OPTIONS, resolveOptions } from '../options'
import { EditableTreeNode } from './extendRoutes'

describe('EditableTreeNode', () => {
const RESOLVED_OPTIONS = resolveOptions(DEFAULT_OPTIONS)
it('creates an editable tree node', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

expect(editable.children).toEqual([])
})

it('reflects changes made on the tree', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

tree.insert('foo', 'file.vue')
Expand All @@ -21,7 +22,7 @@ describe('EditableTreeNode', () => {
})

it('reflects changes made on the editable tree', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert('foo', 'file.vue')
Expand All @@ -30,7 +31,7 @@ describe('EditableTreeNode', () => {
})

it('keeps nested routes flat', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert('foo/bar', 'file.vue')
Expand All @@ -41,7 +42,7 @@ describe('EditableTreeNode', () => {
})

it('can nest routes', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

const node = editable.insert('foo', 'file.vue')
Expand All @@ -55,7 +56,7 @@ describe('EditableTreeNode', () => {
})

it('adds params', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id', 'file.vue')
Expand All @@ -75,7 +76,7 @@ describe('EditableTreeNode', () => {
})

it('adds params with modifiers', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id+', 'file.vue')
Expand All @@ -95,7 +96,7 @@ describe('EditableTreeNode', () => {
})

it('can have multiple params', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':foo/:bar', 'file.vue')
Expand All @@ -122,7 +123,7 @@ describe('EditableTreeNode', () => {
})

it('can have multiple params with modifiers', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':foo/:bar+_:o(\\d+)', 'file.vue')
Expand Down Expand Up @@ -156,7 +157,7 @@ describe('EditableTreeNode', () => {
})

it('adds params with custom regex', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id(\\d+)', 'file.vue')
Expand All @@ -175,7 +176,7 @@ describe('EditableTreeNode', () => {
})

it('adds a param with empty regex', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id()', 'file.vue')
Expand All @@ -194,7 +195,7 @@ describe('EditableTreeNode', () => {
})

it('adds a param with a modifier and custom regex', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id(\\d+)+', 'file.vue')
Expand All @@ -213,7 +214,7 @@ describe('EditableTreeNode', () => {
})

it('adds a param with a modifier and empty regex', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert(':id()+', 'file.vue')
Expand All @@ -232,7 +233,7 @@ describe('EditableTreeNode', () => {
})

it('detects a splat', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const editable = new EditableTreeNode(tree)

editable.insert('/:path(.*)', 'file.vue')
Expand Down
55 changes: 28 additions & 27 deletions src/core/tree.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { describe, expect, it } from 'vitest'
import { DEFAULT_OPTIONS } from '../options'
import { DEFAULT_OPTIONS, resolveOptions } from '../options'
import { PrefixTree } from './tree'
import { TreeNodeType } from './treeNodeValue'

describe('Tree', () => {
const RESOLVED_OPTIONS = resolveOptions(DEFAULT_OPTIONS)
it('creates an empty tree', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
expect(tree.children.size).toBe(0)
})

it('creates a tree with a single static path', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('foo.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('foo')!
Expand All @@ -24,7 +25,7 @@ describe('Tree', () => {
})

it('creates a tree with a single param', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[id].vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('[id]')!
Expand All @@ -39,7 +40,7 @@ describe('Tree', () => {
})

it('separate param names from static segments', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[id]_a')
tree.insert('[a]e[b]f')
expect(tree.children.get('[id]_a')!.value).toMatchObject({
Expand All @@ -58,7 +59,7 @@ describe('Tree', () => {
})

it('creates params in nested files', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const nestedId = tree.insert('nested/[id].vue')

expect(nestedId.value.isParam()).toBe(true)
Expand Down Expand Up @@ -86,7 +87,7 @@ describe('Tree', () => {
})

it('creates params in nested folders', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)

let node = tree.insert('nested/[id]/index.vue')
const id = tree.children.get('nested')!.children.get('[id]')!
Expand Down Expand Up @@ -138,7 +139,7 @@ describe('Tree', () => {
})

it('handles repeatable params one or more', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[id]+.vue')
expect(tree.children.get('[id]+')!.value).toMatchObject({
rawSegment: '[id]+',
Expand All @@ -156,7 +157,7 @@ describe('Tree', () => {
})

it('handles repeatable params zero or more', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[[id]]+.vue')
expect(tree.children.get('[[id]]+')!.value).toMatchObject({
rawSegment: '[[id]]+',
Expand All @@ -174,7 +175,7 @@ describe('Tree', () => {
})

it('handles optional params', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[[id]].vue')
expect(tree.children.get('[[id]]')!.value).toMatchObject({
rawSegment: '[[id]]',
Expand All @@ -192,7 +193,7 @@ describe('Tree', () => {
})

it('handles named views', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('index.vue')
tree.insert('index@a.vue')
tree.insert('index@b.vue')
Expand Down Expand Up @@ -233,15 +234,15 @@ describe('Tree', () => {
})

it('handles single named views that are not default', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('index@a.vue')
expect([...tree.children.get('index')!.value.components.keys()]).toEqual([
'a',
])
})

it('removes the node after all named views', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('index.vue')
tree.insert('index@a.vue')
expect(tree.children.get('index')).toBeDefined()
Expand All @@ -252,7 +253,7 @@ describe('Tree', () => {
})

it('can remove itself from the tree', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('index.vue').insert('nested.vue')
tree.insert('a.vue').insert('nested.vue')
tree.insert('b.vue')
Expand All @@ -264,7 +265,7 @@ describe('Tree', () => {
})

it('handles multiple params', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[a]-[b].vue')
tree.insert('o[a]-[b]c.vue')
tree.insert('o[a][b]c.vue')
Expand All @@ -276,7 +277,7 @@ describe('Tree', () => {
})

it('creates a tree of nested routes', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('index.vue')
tree.insert('a/index.vue')
tree.insert('a/b/index.vue')
Expand Down Expand Up @@ -313,7 +314,7 @@ describe('Tree', () => {
})

it('handles a modifier for single params', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('[id]+.vue')
expect(tree.children.size).toBe(1)
const child = tree.children.get('[id]+')!
Expand All @@ -329,7 +330,7 @@ describe('Tree', () => {
})

it('removes nodes', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('foo.vue')
tree.insert('[id].vue')
tree.remove('foo.vue')
Expand All @@ -346,15 +347,15 @@ describe('Tree', () => {
})

it('removes empty folders', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('a/b/c/d.vue')
expect(tree.children.size).toBe(1)
tree.remove('a/b/c/d.vue')
expect(tree.children.size).toBe(0)
})

it('insert returns the node', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
const a = tree.insert('a.vue')
expect(tree.children.get('a')).toBe(a)
const bC = tree.insert('b/c.vue')
Expand All @@ -366,7 +367,7 @@ describe('Tree', () => {
})

it('keeps parent with file but no children', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('a/b/c/d.vue')
tree.insert('a/b.vue')
expect(tree.children.size).toBe(1)
Expand All @@ -381,7 +382,7 @@ describe('Tree', () => {
})

it('allows a custom name', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
let node = tree.insert('[a]-[b].vue')
node.value.setOverride('', {
name: 'custom',
Expand All @@ -396,7 +397,7 @@ describe('Tree', () => {
})

it('allows a custom path', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
let node = tree.insert('[a]-[b].vue')
node.value.setOverride('', {
path: '/custom',
Expand All @@ -413,7 +414,7 @@ describe('Tree', () => {
})

it('removes trailing slash from path but not from name', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('a/index.vue')
tree.insert('a/a.vue')
let child = tree.children.get('a')!
Expand All @@ -435,7 +436,7 @@ describe('Tree', () => {

it('handles long extensions', () => {
const tree = new PrefixTree({
...DEFAULT_OPTIONS,
...RESOLVED_OPTIONS,
extensions: ['.page.vue'],
})
tree.insert('a.page.vue')
Expand Down Expand Up @@ -467,7 +468,7 @@ describe('Tree', () => {

describe('dot nesting', () => {
it('transforms dots into nested routes by default', () => {
const tree = new PrefixTree(DEFAULT_OPTIONS)
const tree = new PrefixTree(RESOLVED_OPTIONS)
tree.insert('users.new.vue')
expect(tree.children.size).toBe(1)
const users = tree.children.get('users.new')!
Expand All @@ -481,7 +482,7 @@ describe('Tree', () => {

it('can ignore dot nesting', () => {
const tree = new PrefixTree({
...DEFAULT_OPTIONS,
...RESOLVED_OPTIONS,
pathParser: {
dotNesting: false,
},
Expand Down
Loading

0 comments on commit 74ac22b

Please sign in to comment.