|
1 | 1 | export { renderToString } from 'preact-render-to-string'
|
2 | 2 | export { default as sirv } from 'sirv'
|
3 | 3 | export { default as mri } from 'mri'
|
4 |
| -import { parse } from 'regexparam' |
| 4 | +export { parse as pathToRegex } from 'regexparam' |
5 | 5 | export { toStatic } from 'hoofd/preact'
|
6 |
| - |
7 |
| -// taken from |
8 |
| -// https://github.com/cyco130/smf/blob/c4b601f48cd3b3b71bea6d76b52b9a85800813e4/smf/shared.ts#L22 |
9 |
| -// as it's decently tested and aligns to what we want for our routing |
10 |
| -export function compareRoutePatterns(a, b) { |
11 |
| - // Non-catch-all routes first: /foo before /$$rest |
12 |
| - const catchAll = Number(a.match(/\$\$(\w+)$/)) - Number(b.match(/\$\$(\w+)$/)) |
13 |
| - if (catchAll) return catchAll |
14 |
| - |
15 |
| - // Split into segments |
16 |
| - const aSegments = a.split('/') |
17 |
| - const bSegments = b.split('/') |
18 |
| - |
19 |
| - // Routes with fewer dynamic segments first: /foo/bar before /foo/$bar |
20 |
| - const dynamicSegments = |
21 |
| - aSegments.filter(segment => segment.includes('$')).length - |
22 |
| - bSegments.filter(segment => segment.includes('$')).length |
23 |
| - if (dynamicSegments) return dynamicSegments |
24 |
| - |
25 |
| - // Routes with fewer segments first: /foo/bar before /foo/bar |
26 |
| - const segments = aSegments.length - bSegments.length |
27 |
| - if (segments) return segments |
28 |
| - |
29 |
| - // Routes with earlier dynamic segments first: /foo/$bar before /$foo/bar |
30 |
| - for (let i = 0; i < aSegments.length; i++) { |
31 |
| - const aSegment = aSegments[i] |
32 |
| - const bSegment = bSegments[i] |
33 |
| - const dynamic = |
34 |
| - Number(aSegment.includes('$')) - Number(bSegment.includes('$')) |
35 |
| - if (dynamic) return dynamic |
36 |
| - |
37 |
| - // Routes with more dynamic subsegments at this position first: /foo/$a-$b before /foo/$a |
38 |
| - const subsegments = aSegment.split('$').length - bSegment.split('$').length |
39 |
| - if (subsegments) return subsegments |
40 |
| - } |
41 |
| - |
42 |
| - // Equal as far as we can tell |
43 |
| - return 0 |
44 |
| -} |
45 |
| - |
46 |
| -export function normalizeRouteImports(imports, baseKeyMatcher) { |
47 |
| - return Object.keys(imports) |
48 |
| - .sort(compareRoutePatterns) |
49 |
| - .map(route => { |
50 |
| - const routePath = simplifyPath(route).replace( |
51 |
| - baseKeyMatcher[0], |
52 |
| - baseKeyMatcher[1] |
53 |
| - ) |
54 |
| - const regex = pathToRegex(routePath) |
55 |
| - |
56 |
| - return { |
57 |
| - route, |
58 |
| - regex, |
59 |
| - routePath, |
60 |
| - module: imports[route], |
61 |
| - } |
62 |
| - }) |
63 |
| -} |
64 |
| - |
65 |
| -function simplifyPath(path) { |
66 |
| - return path |
67 |
| - .replace(/(\.(js|ts)x?)/, '') |
68 |
| - .replace(/index/, '/') |
69 |
| - .replace('//', '/') |
70 |
| - .replace(/\$\$/, '*') |
71 |
| - .replace(/\$/, ':') |
72 |
| -} |
73 |
| - |
74 |
| -function pathToRegex(path) { |
75 |
| - return parse(path) |
76 |
| -} |
0 commit comments