|
1 | 1 | import { analyzeModule, BaseFileSystemRouter, cleanPath } from "vinxi/fs-router"; |
2 | 2 |
|
| 3 | +function toPathBase(src, config) { |
| 4 | + const routePath = cleanPath(src, config) |
| 5 | + // remove the initial slash |
| 6 | + .slice(1) |
| 7 | + .replace(/index$/, "") |
| 8 | + // replace . with / for flat routes - e.g. foo.bar -> foo/bar |
| 9 | + .replace(/\./g, "/") |
| 10 | + // converts any splat route ... that got replaced back from /// |
| 11 | + // this could be avoided with a lookbehind regex but safar has only supported them since mid 2023 |
| 12 | + .replace(/\/\/\//g, "...") |
| 13 | + .replace(/\[([^\/]+)\]/g, (_, m) => { |
| 14 | + if (m.length > 3 && m.startsWith("...")) { |
| 15 | + return `*${m.slice(3)}`; |
| 16 | + } |
| 17 | + if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) { |
| 18 | + return `:${m.slice(1, -1)}?`; |
| 19 | + } |
| 20 | + return `:${m}`; |
| 21 | + }); |
| 22 | + |
| 23 | + return routePath?.length > 0 ? `/${routePath}` : "/"; |
| 24 | +} |
| 25 | + |
3 | 26 | export class SolidStartClientFileRouter extends BaseFileSystemRouter { |
4 | 27 | toPath(src) { |
5 | | - const routePath = cleanPath(src, this.config) |
6 | | - // remove the initial slash |
7 | | - .slice(1) |
8 | | - .replace(/index$/, "") |
9 | | - .replace(/\[([^\/]+)\]/g, (_, m) => { |
10 | | - if (m.length > 3 && m.startsWith("...")) { |
11 | | - return `*${m.slice(3)}`; |
12 | | - } |
13 | | - if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) { |
14 | | - return `:${m.slice(1, -1)}?`; |
15 | | - } |
16 | | - return `:${m}`; |
17 | | - }); |
18 | | - |
19 | | - return routePath?.length > 0 ? `/${routePath}` : "/"; |
| 28 | + return toPathBase(src, this.config); |
20 | 29 | } |
21 | 30 |
|
22 | 31 | toRoute(src) { |
@@ -72,21 +81,7 @@ function createHTTPHandlers(src, exports) { |
72 | 81 |
|
73 | 82 | export class SolidStartServerFileRouter extends BaseFileSystemRouter { |
74 | 83 | toPath(src) { |
75 | | - const routePath = cleanPath(src, this.config) |
76 | | - // remove the initial slash |
77 | | - .slice(1) |
78 | | - .replace(/index$/, "") |
79 | | - .replace(/\[([^\/]+)\]/g, (_, m) => { |
80 | | - if (m.length > 3 && m.startsWith("...")) { |
81 | | - return `*${m.slice(3)}`; |
82 | | - } |
83 | | - if (m.length > 2 && m.startsWith("[") && m.endsWith("]")) { |
84 | | - return `:${m.slice(1, -1)}?`; |
85 | | - } |
86 | | - return `:${m}`; |
87 | | - }); |
88 | | - |
89 | | - return routePath?.length > 0 ? `/${routePath}` : "/"; |
| 84 | + return toPathBase(src, this.config); |
90 | 85 | } |
91 | 86 |
|
92 | 87 | toRoute(src) { |
|
0 commit comments