Skip to content

Commit

Permalink
refact: move pageDir glob parsing to load handler
Browse files Browse the repository at this point in the history
  • Loading branch information
klarkc committed May 18, 2021
1 parent e436353 commit f62dd51
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 28 deletions.
44 changes: 31 additions & 13 deletions src/files.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,32 @@
import fg from 'fast-glob'
import { ResolvedOptions } from './types'
import { PageDirOptions, ResolvedOptions } from './types'
import { extensionsToGlob } from './utils'

function getIgnore(exclude: Array<string>) {
return ['node_modules', '.git', '**/__*__/**', ...exclude]
}

/**
* Resolves the pages that for the given pageDir globs.
*/
export async function getPageDirs(pageDirOptions: PageDirOptions, options: ResolvedOptions): Promise<PageDirOptions[]> {
const { exclude } = options

const dirs = await fg(pageDirOptions.dir, {
ignore: getIgnore(exclude),
onlyDirectories: true,
dot: true,
unique: true,
})

const pageDirs = dirs.map(dir => ({
...pageDirOptions,
dir,
}))

return pageDirs
}

/**
* Resolves the files that are valid pages for the given context.
*/
Expand All @@ -12,19 +37,12 @@ export async function getPageFiles(path: string, options: ResolvedOptions): Prom
} = options

const ext = extensionsToGlob(extensions)
const ignore = ['node_modules', '.git', '**/__*__/**', ...exclude]
const cwds = await fg(path, {
ignore,
onlyDirectories: true,
dot: true,
unique: true,
})

const nestedFiles = await Promise.all(cwds.map(cwd => fg(`**/*.${ext}`, {
ignore,
const files = await fg(`**/*.${ext}`, {
ignore: getIgnore(exclude),
onlyFiles: true,
cwd,
})))
cwd: path,
})

return nestedFiles.flat(1)
return files
}
20 changes: 12 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { resolve } from 'path'
import type { Plugin } from 'vite'
import { Route, ResolvedOptions, UserOptions } from './types'
import { getPageFiles } from './files'
import { getPageFiles, getPageDirs } from './files'
import { generateRoutes, generateClientCode, isRouteBlockChanged } from './generate'
import { debug, getPagesVirtualModule, isTarget, slash, replaceSquareBrackets, isDynamicRoute, isCatchAllRoute } from './utils'
import { parseVueRequest } from './query'
Expand Down Expand Up @@ -69,15 +69,19 @@ function pagesPlugin(userOptions: UserOptions = {}): Plugin {

if (!generatedRoutes) {
generatedRoutes = []
for (const pageDir of options.pagesDirOptions) {
const pageDirPath = slash(resolve(options.root, pageDir.dir))
debug.gen('dir: %O', pageDirPath)

const files = await getPageFiles(pageDirPath, options)
debug.gen('files: %O', files)
for (const pageDirGlob of options.pagesDirOptions) {
const pageDirs = await getPageDirs(pageDirGlob, options)
for (const pageDir of pageDirs) {
const pageDirPath = slash(resolve(options.root, pageDir.dir))
debug.gen('dir: %O', pageDirPath)

const routes = generateRoutes(files, pageDir, options)
generatedRoutes.push(...routes)
const files = await getPageFiles(pageDirPath, options)
debug.gen('files: %O', files)

const routes = generateRoutes(files, pageDir, options)
generatedRoutes.push(...routes)
}
}
generatedRoutes = generatedRoutes.sort(i => isDynamicRoute(i.path) ? 1 : -1)
const allRoute = generatedRoutes.find(i => isCatchAllRoute(i.path))
Expand Down
12 changes: 9 additions & 3 deletions test/__snapshots__/files.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ Array [
]
`;

exports[`Get files Pages files with glob pagesDir: pages files with glob pagesDir 1`] = `
exports[`Get page dirs Page dirs with glob pagesDir: pages dirs with glob pagesDir 1`] = `
Array [
"bar.vue",
"foo.vue",
Object {
"baseRoute": "/",
"dir": "/home/klarkc/Sources/vite-plugin-pages/test/assets/deep-pages/bar/pages",
},
Object {
"baseRoute": "/",
"dir": "/home/klarkc/Sources/vite-plugin-pages/test/assets/deep-pages/foo/pages",
},
]
`;
14 changes: 10 additions & 4 deletions test/files.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { resolve } from 'path'
import { resolveOptions } from '../src/options'
import { getPageFiles } from '../src/files'
import { getPageFiles, getPageDirs } from '../src/files'

const options = resolveOptions({})
const testPagesDir = resolve('test/assets/pages')
Expand All @@ -11,9 +11,15 @@ describe('Get files', () => {
const files = await getPageFiles(testPagesDir, options)
expect(files.sort()).toMatchSnapshot('page files')
})
})

test('Pages files with glob pagesDir', async() => {
const files = await getPageFiles(testGlobPagesDir, options)
expect(files.sort()).toMatchSnapshot('pages files with glob pagesDir')
describe('Get page dirs', () => {
test('Page dirs with glob pagesDir', async() => {
const pageDirOptions = {
dir: testGlobPagesDir,
baseRoute: '/',
}
const dirs = await getPageDirs(pageDirOptions, options)
expect(dirs.sort()).toMatchSnapshot('pages dirs with glob pagesDir')
})
})

0 comments on commit f62dd51

Please sign in to comment.