-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: normalize pages to it's own adex sub-plugin (#20)
* refactor: move pages to it's own plugin * fix: null scenario
- Loading branch information
1 parent
432cce9
commit 7f590ba
Showing
5 changed files
with
132 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { pathToRegex } from 'adex/ssr' | ||
|
||
const pages = import.meta.glob('#{__PLUGIN_PAGES_ROOT}') | ||
|
||
export const routes = normalizeRouteImports(pages, [ | ||
new RegExp('#{__PLUGIN_PAGES_ROOT_REGEX}'), | ||
'#{__PLUGIN_PAGES_ROOT_REGEX_REPLACER}', | ||
]) | ||
|
||
// taken from | ||
// https://github.com/cyco130/smf/blob/c4b601f48cd3b3b71bea6d76b52b9a85800813e4/smf/shared.ts#L22 | ||
// as it's decently tested and aligns to what we want for our routing | ||
function compareRoutePatterns(a, b) { | ||
// Non-catch-all routes first: /foo before /$$rest | ||
const catchAll = Number(a.match(/\$\$(\w+)$/)) - Number(b.match(/\$\$(\w+)$/)) | ||
if (catchAll) return catchAll | ||
|
||
// Split into segments | ||
const aSegments = a.split('/') | ||
const bSegments = b.split('/') | ||
|
||
// Routes with fewer dynamic segments first: /foo/bar before /foo/$bar | ||
const dynamicSegments = | ||
aSegments.filter(segment => segment.includes('$')).length - | ||
bSegments.filter(segment => segment.includes('$')).length | ||
if (dynamicSegments) return dynamicSegments | ||
|
||
// Routes with fewer segments first: /foo/bar before /foo/bar | ||
const segments = aSegments.length - bSegments.length | ||
if (segments) return segments | ||
|
||
// Routes with earlier dynamic segments first: /foo/$bar before /$foo/bar | ||
for (let i = 0; i < aSegments.length; i++) { | ||
const aSegment = aSegments[i] | ||
const bSegment = bSegments[i] | ||
const dynamic = | ||
Number(aSegment.includes('$')) - Number(bSegment.includes('$')) | ||
if (dynamic) return dynamic | ||
|
||
// Routes with more dynamic subsegments at this position first: /foo/$a-$b before /foo/$a | ||
const subsegments = aSegment.split('$').length - bSegment.split('$').length | ||
if (subsegments) return subsegments | ||
} | ||
|
||
// Equal as far as we can tell | ||
return 0 | ||
} | ||
|
||
function normalizeRouteImports(imports, baseKeyMatcher) { | ||
return Object.keys(imports) | ||
.sort(compareRoutePatterns) | ||
.map(route => { | ||
const routePath = simplifyPath(route).replace( | ||
baseKeyMatcher[0], | ||
baseKeyMatcher[1] | ||
) | ||
|
||
const regex = pathToRegex(routePath) | ||
|
||
return { | ||
route, | ||
regex, | ||
routePath, | ||
module: imports[route], | ||
} | ||
}) | ||
} | ||
|
||
function simplifyPath(path) { | ||
return path | ||
.replace(/(\.(js|ts)x?)/, '') | ||
.replace(/index/, '/') | ||
.replace('//', '/') | ||
.replace(/\$\$/, '*') | ||
.replace(/\$/, ':') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,3 @@ | ||
export { toStatic } from 'hoofd/preact' | ||
export { renderToString } from 'preact-render-to-string' | ||
|
||
export function normalizeRouteImports<Routes>( | ||
obj: Routes, | ||
matcher: [RegExp, string] | ||
): { | ||
route: string | ||
regex: { | ||
pattern: RegExp | ||
keys: string[] | ||
} | ||
routePath: string | ||
module: () => Promise<{ | ||
default: () => any | ||
}> | ||
}[] | ||
export { parse as pathToRegex } from 'regexparam' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,5 @@ | ||
export { renderToString } from 'preact-render-to-string' | ||
export { default as sirv } from 'sirv' | ||
export { default as mri } from 'mri' | ||
import { parse } from 'regexparam' | ||
export { parse as pathToRegex } from 'regexparam' | ||
export { toStatic } from 'hoofd/preact' | ||
|
||
// taken from | ||
// https://github.com/cyco130/smf/blob/c4b601f48cd3b3b71bea6d76b52b9a85800813e4/smf/shared.ts#L22 | ||
// as it's decently tested and aligns to what we want for our routing | ||
export function compareRoutePatterns(a, b) { | ||
// Non-catch-all routes first: /foo before /$$rest | ||
const catchAll = Number(a.match(/\$\$(\w+)$/)) - Number(b.match(/\$\$(\w+)$/)) | ||
if (catchAll) return catchAll | ||
|
||
// Split into segments | ||
const aSegments = a.split('/') | ||
const bSegments = b.split('/') | ||
|
||
// Routes with fewer dynamic segments first: /foo/bar before /foo/$bar | ||
const dynamicSegments = | ||
aSegments.filter(segment => segment.includes('$')).length - | ||
bSegments.filter(segment => segment.includes('$')).length | ||
if (dynamicSegments) return dynamicSegments | ||
|
||
// Routes with fewer segments first: /foo/bar before /foo/bar | ||
const segments = aSegments.length - bSegments.length | ||
if (segments) return segments | ||
|
||
// Routes with earlier dynamic segments first: /foo/$bar before /$foo/bar | ||
for (let i = 0; i < aSegments.length; i++) { | ||
const aSegment = aSegments[i] | ||
const bSegment = bSegments[i] | ||
const dynamic = | ||
Number(aSegment.includes('$')) - Number(bSegment.includes('$')) | ||
if (dynamic) return dynamic | ||
|
||
// Routes with more dynamic subsegments at this position first: /foo/$a-$b before /foo/$a | ||
const subsegments = aSegment.split('$').length - bSegment.split('$').length | ||
if (subsegments) return subsegments | ||
} | ||
|
||
// Equal as far as we can tell | ||
return 0 | ||
} | ||
|
||
export function normalizeRouteImports(imports, baseKeyMatcher) { | ||
return Object.keys(imports) | ||
.sort(compareRoutePatterns) | ||
.map(route => { | ||
const routePath = simplifyPath(route).replace( | ||
baseKeyMatcher[0], | ||
baseKeyMatcher[1] | ||
) | ||
const regex = pathToRegex(routePath) | ||
|
||
return { | ||
route, | ||
regex, | ||
routePath, | ||
module: imports[route], | ||
} | ||
}) | ||
} | ||
|
||
function simplifyPath(path) { | ||
return path | ||
.replace(/(\.(js|ts)x?)/, '') | ||
.replace(/index/, '/') | ||
.replace('//', '/') | ||
.replace(/\$\$/, '*') | ||
.replace(/\$/, ':') | ||
} | ||
|
||
function pathToRegex(path) { | ||
return parse(path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters