Skip to content

Commit b913f56

Browse files
committed
feat: allow path as function
1 parent bfe5054 commit b913f56

File tree

5 files changed

+59
-30
lines changed

5 files changed

+59
-30
lines changed

playground/vite.config.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,19 @@ export default defineConfig({
8585
// ignores .vue files
8686
extensions: ['.md'],
8787
},
88-
// {
89-
// src: 'src/features/',
90-
// path: 'features-prefix/',
91-
// },
88+
{
89+
src: 'src/features',
90+
filePatterns: '*/pages/**/*',
91+
path: (file) => {
92+
const prefix = 'src/features'
93+
// +1 for the starting slash
94+
file = file
95+
.slice(file.lastIndexOf(prefix) + prefix.length + 1)
96+
.replace('/pages', '')
97+
console.log('👉 FILE', file)
98+
return file
99+
},
100+
},
92101
],
93102
logs: true,
94103
// getRouteName: getPascalCaseRouteName,

src/core/RoutesFolderWatcher.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { appendExtensionListToPattern, asRoutePath } from './utils'
1212

1313
export class RoutesFolderWatcher {
1414
src: string
15-
path: string
15+
path: string | ((filepath: string) => string)
1616
extensions: string[]
1717
filePatterns: string[]
1818
exclude: string[]

src/core/context.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -79,21 +79,19 @@ export function createRoutesContext(options: ResolvedOptions) {
7979
// TODO: do they return the symbolic link path or the original file?
8080
// followSymbolicLinks: false,
8181
ignore: ignorePattern,
82-
})
83-
.then((files) => files.map((file) => resolve(folder.src, file)))
84-
.then((files) =>
85-
Promise.all(
86-
files
87-
// ensure consistent files in Windows/Unix and absolute paths
88-
.map((file) => resolve(folder.src, file))
89-
.map((file) =>
90-
addPage({
91-
routePath: asRoutePath(folder, file),
92-
filePath: file,
93-
})
94-
)
95-
)
82+
}).then((files) =>
83+
Promise.all(
84+
files
85+
// ensure consistent files in Windows/Unix and absolute paths
86+
.map((file) => resolve(folder.src, file))
87+
.map((file) =>
88+
addPage({
89+
routePath: asRoutePath(folder, file),
90+
filePath: file,
91+
})
92+
)
9693
)
94+
)
9795
})
9896
)
9997

src/core/utils.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,12 @@ export function asRoutePath(
244244
{ src, path = '' }: RoutesFolderOption,
245245
filePath: string
246246
) {
247-
return (
248-
// add the path prefix if any
249-
path +
250-
// remove the absolute path to the pages folder
251-
filePath.slice(src.length + 1)
252-
)
247+
return typeof path === 'string'
248+
? // add the path prefix if any
249+
path +
250+
// remove the absolute path to the pages folder
251+
filePath.slice(src.length + 1)
252+
: path(filePath)
253253
}
254254

255255
/**

src/options.ts

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,37 @@ import { ParseSegmentOptions } from './core/treeNodeValue'
77

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

14-
// TODO: allow a function to customize based on the filepath
1515
/**
16-
* Prefix to add to the route path **as is**. You might need to end with a slash. Defaults to `''`.
16+
* Prefix to add to the route path **as is**. Defaults to `''`. Can also be a function
17+
* to reuse parts of the filepath, in that case you should return a **modified version of the filepath**.
18+
*
19+
* @example
20+
* ```js
21+
* {
22+
* src: 'src/pages',
23+
* // this is equivalent to the default behavior
24+
* path: (file) => file.slice(file.lastIndexOf('src/pages') + 'src/pages'.length
25+
* },
26+
* {
27+
* src: 'src/features',
28+
* // match all files (note the \ is not needed in real code)
29+
* filePatterns: '*‍/pages/**\/',
30+
* path: (file) => {
31+
* const prefix = 'src/features'
32+
* // remove the everything before src/features and removes /pages
33+
* // /src/features/feature1/pages/index.vue -> feature1/index.vue
34+
* return file.slice(file.lastIndexOf(prefix) + prefix.length + 1).replace('/pages', '')
35+
* },
36+
* },
37+
* ```
38+
*
1739
*/
18-
path?: string
40+
path?: string | ((filepath: string) => string)
1941

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

0 commit comments

Comments
 (0)