1
- import type { ResolvedOptions } from '../options'
1
+ import type { ResolvedOptions , RoutesFolderOption } from '../options'
2
2
import {
3
3
createTreeNodeValue ,
4
4
TreeNodeValueOptions ,
5
5
TreeRouteParam ,
6
6
} from './treeNodeValue'
7
7
import type { TreeNodeValue } from './treeNodeValue'
8
- import { trimExtension } from './utils'
8
+ import { resolveOverridableOption , trimExtension } from './utils'
9
9
import { CustomRouteBlock } from './customBlock'
10
10
import { RouteMeta } from 'vue-router'
11
11
@@ -69,9 +69,16 @@ export class TreeNode {
69
69
* @param filePath - file path, defaults to path for convenience and testing
70
70
*/
71
71
insert ( path : string , filePath : string = path ) : TreeNode {
72
+ // find the `routesFolder` resolved option that matches the filepath
73
+ const folderOptions = findFolderOptions ( this . options . routesFolder , filePath )
74
+
72
75
const { tail, segment, viewName, isComponent } = splitFilePath (
73
76
path ,
74
- this . options
77
+ // use the correct extensions for the folder
78
+ resolveOverridableOption (
79
+ this . options . extensions ,
80
+ folderOptions ?. extensions
81
+ )
75
82
)
76
83
77
84
if ( ! this . children . has ( segment ) ) {
@@ -161,10 +168,14 @@ export class TreeNode {
161
168
* @param path - path segment of the file
162
169
*/
163
170
remove ( path : string ) {
171
+ const folderOptions = findFolderOptions ( this . options . routesFolder , path )
164
172
// TODO: rename remove to removeChild
165
173
const { tail, segment, viewName, isComponent } = splitFilePath (
166
174
path ,
167
- this . options
175
+ resolveOverridableOption (
176
+ this . options . extensions ,
177
+ folderOptions ?. extensions
178
+ )
168
179
)
169
180
170
181
const child = this . children . get ( segment )
@@ -319,15 +330,15 @@ export class PrefixTree extends TreeNode {
319
330
*
320
331
* @param filePath - filePath to split
321
332
*/
322
- function splitFilePath ( filePath : string , options : ResolvedOptions ) {
333
+ function splitFilePath ( filePath : string , extensions : string [ ] ) {
323
334
const slashPos = filePath . indexOf ( '/' )
324
335
let head = slashPos < 0 ? filePath : filePath . slice ( 0 , slashPos )
325
336
const tail = slashPos < 0 ? '' : filePath . slice ( slashPos + 1 )
326
337
327
338
let segment = head
328
339
// only the last segment can be a filename with an extension
329
340
if ( ! tail ) {
330
- segment = trimExtension ( head , options . extensions )
341
+ segment = trimExtension ( head , extensions )
331
342
}
332
343
let viewName = 'default'
333
344
@@ -348,3 +359,17 @@ function splitFilePath(filePath: string, options: ResolvedOptions) {
348
359
isComponent,
349
360
}
350
361
}
362
+
363
+ /**
364
+ * Find the folder options that match the file path.
365
+ *
366
+ * @param folderOptions `options.routesFolder` option
367
+ * @param filePath resolved file path
368
+ * @returns
369
+ */
370
+ function findFolderOptions (
371
+ folderOptions : RoutesFolderOption [ ] ,
372
+ filePath : string
373
+ ) : RoutesFolderOption | undefined {
374
+ return folderOptions . find ( ( folder ) => filePath . includes ( folder . src ) )
375
+ }
0 commit comments