Skip to content

Commit

Permalink
feat: allow path as function
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Sep 22, 2023
1 parent bfe5054 commit b913f56
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 30 deletions.
17 changes: 13 additions & 4 deletions playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,19 @@ export default defineConfig({
// ignores .vue files
extensions: ['.md'],
},
// {
// src: 'src/features/',
// path: 'features-prefix/',
// },
{
src: 'src/features',
filePatterns: '*/pages/**/*',
path: (file) => {
const prefix = 'src/features'
// +1 for the starting slash
file = file
.slice(file.lastIndexOf(prefix) + prefix.length + 1)
.replace('/pages', '')
console.log('👉 FILE', file)
return file
},
},
],
logs: true,
// getRouteName: getPascalCaseRouteName,
Expand Down
2 changes: 1 addition & 1 deletion src/core/RoutesFolderWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { appendExtensionListToPattern, asRoutePath } from './utils'

export class RoutesFolderWatcher {
src: string
path: string
path: string | ((filepath: string) => string)
extensions: string[]
filePatterns: string[]
exclude: string[]
Expand Down
26 changes: 12 additions & 14 deletions src/core/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,21 +79,19 @@ export function createRoutesContext(options: ResolvedOptions) {
// TODO: do they return the symbolic link path or the original file?
// followSymbolicLinks: false,
ignore: ignorePattern,
})
.then((files) => files.map((file) => resolve(folder.src, file)))
.then((files) =>
Promise.all(
files
// ensure consistent files in Windows/Unix and absolute paths
.map((file) => resolve(folder.src, file))
.map((file) =>
addPage({
routePath: asRoutePath(folder, file),
filePath: file,
})
)
)
}).then((files) =>
Promise.all(
files
// ensure consistent files in Windows/Unix and absolute paths
.map((file) => resolve(folder.src, file))
.map((file) =>
addPage({
routePath: asRoutePath(folder, file),
filePath: file,
})
)
)
)
})
)

Expand Down
12 changes: 6 additions & 6 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,12 @@ export function asRoutePath(
{ src, path = '' }: RoutesFolderOption,
filePath: string
) {
return (
// add the path prefix if any
path +
// remove the absolute path to the pages folder
filePath.slice(src.length + 1)
)
return typeof path === 'string'
? // add the path prefix if any
path +
// remove the absolute path to the pages folder
filePath.slice(src.length + 1)
: path(filePath)
}

/**
Expand Down
32 changes: 27 additions & 5 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,37 @@ import { ParseSegmentOptions } from './core/treeNodeValue'

export interface RoutesFolderOption {
/**
* Path to the folder containing the components that should be used for routes.
* Folder to scan files that should be used for routes. **Cannot be a glob**, use the `path`, `filePatterns`, and
* `exclude` options to filter out files. This section will **be removed** from the resulting path.
*/
src: string

// TODO: allow a function to customize based on the filepath
/**
* Prefix to add to the route path **as is**. You might need to end with a slash. Defaults to `''`.
* Prefix to add to the route path **as is**. Defaults to `''`. Can also be a function
* to reuse parts of the filepath, in that case you should return a **modified version of the filepath**.
*
* @example
* ```js
* {
* src: 'src/pages',
* // this is equivalent to the default behavior
* path: (file) => file.slice(file.lastIndexOf('src/pages') + 'src/pages'.length
* },
* {
* src: 'src/features',
* // match all files (note the \ is not needed in real code)
* filePatterns: '*‍/pages/**\/',
* path: (file) => {
* const prefix = 'src/features'
* // remove the everything before src/features and removes /pages
* // /src/features/feature1/pages/index.vue -> feature1/index.vue
* return file.slice(file.lastIndexOf(prefix) + prefix.length + 1).replace('/pages', '')
* },
* },
* ```
*
*/
path?: string
path?: string | ((filepath: string) => string)

/**
* Allows to override the global `filePattern` option for this folder. It can also extend the global values by passing
Expand All @@ -40,7 +62,7 @@ export interface RoutesFolderOption {
* Normalized options for a routes folder.
*/
export interface RoutesFolderOptionResolved extends RoutesFolderOption {
path: string
path: string | ((filepath: string) => string)
/**
* Final glob pattern to match files in the folder.
*/
Expand Down

0 comments on commit b913f56

Please sign in to comment.