Skip to content

Commit

Permalink
feat: add nested option (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored Oct 5, 2018
1 parent 923a351 commit a31de7d
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ module.exports = [
- `pages`: Path to the directory that contains your page components.
- `importPrefix`: A string that will be added to importing component path (default `@/pages/`).
- `dynamicImport`: Use dynamic import expression (`import()`) to import component (default `true`).
- `nested`: If `true`, generated route path will be always treated as nested. (e.g. will generate `path: 'foo'` rather than `path: '/foo'`)

## Related Projects

Expand Down
6 changes: 4 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ export interface GenerateConfig {
pages: string
importPrefix?: string
dynamicImport?: boolean
nested?: boolean
}

export function generateRoutes({
pages,
importPrefix = '@/pages/',
dynamicImport = true
dynamicImport = true,
nested = false
}: GenerateConfig): string {
const patterns = ['**/*.vue', '!**/__*__.vue', '!**/__*__/**']

Expand All @@ -22,7 +24,7 @@ export function generateRoutes({
onlyFiles: true
})

const metaList = resolveRoutePaths(pagePaths, importPrefix, file => {
const metaList = resolveRoutePaths(pagePaths, importPrefix, nested, file => {
return fs.readFileSync(path.join(pages, file), 'utf8')
})

Expand Down
26 changes: 20 additions & 6 deletions src/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const routeMetaName = 'route-meta'
export function resolveRoutePaths(
paths: string[],
importPrefix: string,
nested: boolean,
readFile: (path: string) => string
): PageMeta[] {
const map: NestedMap<string[]> = {}
Expand All @@ -25,12 +26,13 @@ export function resolveRoutePaths(
setToMap(map, pathToMapPath(path), path)
})

return pathMapToMeta(map, importPrefix, readFile)
return pathMapToMeta(map, importPrefix, nested, readFile)
}

function pathMapToMeta(
map: NestedMap<string[]>,
importPrefix: string,
nested: boolean,
readFile: (path: string) => string,
parentDepth: number = 0
): PageMeta[] {
Expand All @@ -40,7 +42,7 @@ function pathMapToMeta(
const meta: PageMeta = {
name: pathToName(path),
specifier: pathToSpecifier(path),
path: pathToRoute(path, parentDepth),
path: pathToRoute(path, parentDepth, nested),
pathSegments: toActualPath(path),
component: importPrefix + path.join('/')
}
Expand All @@ -66,6 +68,7 @@ function pathMapToMeta(
meta.children = pathMapChildrenToMeta(
map.children,
importPrefix,
nested,
readFile,
path.length
)
Expand All @@ -75,7 +78,13 @@ function pathMapToMeta(
}

return map.children
? pathMapChildrenToMeta(map.children, importPrefix, readFile, parentDepth)
? pathMapChildrenToMeta(
map.children,
importPrefix,
nested,
readFile,
parentDepth
)
: []
}

Expand All @@ -97,13 +106,14 @@ function routePathComparator(a: string[], b: string[]): number {
function pathMapChildrenToMeta(
children: Map<string, NestedMap<string[]>>,
importPrefix: string,
nested: boolean,
readFile: (path: string) => string,
parentDepth: number
): PageMeta[] {
return Array.from(children.values())
.reduce<PageMeta[]>((acc, value) => {
return acc.concat(
pathMapToMeta(value, importPrefix, readFile, parentDepth)
pathMapToMeta(value, importPrefix, nested, readFile, parentDepth)
)
}, [])
.sort((a, b) => {
Expand Down Expand Up @@ -172,8 +182,12 @@ function pathToSpecifier(segments: string[]): string {
return /^\d/.test(replaced) ? '_' + replaced : replaced
}

function pathToRoute(segments: string[], parentDepth: number): string {
const prefix = parentDepth > 0 ? '' : '/'
function pathToRoute(
segments: string[],
parentDepth: number,
nested: boolean
): string {
const prefix = nested || parentDepth > 0 ? '' : '/'
return (
prefix +
toActualPath(segments)
Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/resolve.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ Array [
]
`;

exports[`Route resolution resolves as nested routes 1`] = `
Array [
Object {
"component": "@/pages/index.vue",
"name": "index",
"path": "",
"pathSegments": Array [],
"specifier": "Index",
},
Object {
"component": "@/pages/foo.vue",
"name": "foo",
"path": "foo",
"pathSegments": Array [
"foo",
],
"specifier": "Foo",
},
]
`;

exports[`Route resolution resolves dynamic nested routes 1`] = `
Array [
Object {
Expand Down
8 changes: 5 additions & 3 deletions test/resolve.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ function mockReadFile(path: string): string {
}

describe('Route resolution', () => {
function test(name: string, paths: string[]): void {
function test(name: string, paths: string[], nested: boolean = false): void {
it(name, () => {
expect(
resolveRoutePaths(paths, '@/pages/', mockReadFile)
resolveRoutePaths(paths, '@/pages/', nested, mockReadFile)
).toMatchSnapshot()
})
}
Expand All @@ -50,9 +50,11 @@ describe('Route resolution', () => {

test('resolve route meta', ['meta.vue'])

test('resolves as nested routes', ['index.vue', 'foo.vue'], true)

it('throws error when failed to parse route-meta', () => {
expect(() => {
resolveRoutePaths(['invalid-meta.vue'], '@/pages/', mockReadFile)
resolveRoutePaths(['invalid-meta.vue'], '@/pages/', false, mockReadFile)
}).toThrow(
/Invalid json format of <route-meta> content in invalid-meta\.vue/
)
Expand Down

0 comments on commit a31de7d

Please sign in to comment.