2020-06-23 07:49:48 +02:00
|
|
|
import getRouteFromAssetPath from '../lib/router/utils/get-route-from-asset-path'
|
2020-06-04 19:32:45 +02:00
|
|
|
|
2020-06-20 21:59:47 +02:00
|
|
|
// matches pages/:page*.js
|
2020-06-22 23:12:36 +02:00
|
|
|
const SERVER_ROUTE_NAME_REGEX = /^pages[/\\](.*)$/
|
2020-06-20 21:59:47 +02:00
|
|
|
// matches static/pages/:page*.js
|
|
|
|
const BROWSER_ROUTE_NAME_REGEX = /^static[/\\]pages[/\\](.*)$/
|
2020-06-04 19:32:45 +02:00
|
|
|
|
2020-06-20 21:59:47 +02:00
|
|
|
function matchBundle(regex: RegExp, input: string): string | null {
|
|
|
|
const result = regex.exec(input)
|
2020-06-04 19:32:45 +02:00
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2020-06-23 07:49:48 +02:00
|
|
|
return getRouteFromAssetPath(`/${result[1]}`)
|
2020-06-20 21:59:47 +02:00
|
|
|
}
|
2020-06-04 19:32:45 +02:00
|
|
|
|
2020-06-20 21:59:47 +02:00
|
|
|
export default function getRouteFromEntrypoint(
|
2021-02-19 11:10:19 +01:00
|
|
|
entryFile: string
|
2020-06-20 21:59:47 +02:00
|
|
|
): string | null {
|
2020-06-22 23:12:36 +02:00
|
|
|
let pagePath = matchBundle(SERVER_ROUTE_NAME_REGEX, entryFile)
|
2020-06-20 21:59:47 +02:00
|
|
|
|
|
|
|
if (pagePath) {
|
|
|
|
return pagePath
|
2020-06-04 19:32:45 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 21:59:47 +02:00
|
|
|
// Potentially the passed item is a browser bundle so we try to match that also
|
|
|
|
return matchBundle(BROWSER_ROUTE_NAME_REGEX, entryFile)
|
2020-06-04 19:32:45 +02:00
|
|
|
}
|