2022-08-10 21:31:01 +02:00
|
|
|
import getAppRouteFromEntrypoint from './get-app-route-from-entrypoint'
|
|
|
|
import matchBundle from './match-bundle'
|
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[/\\](.*)$/
|
2022-08-10 21:31:01 +02:00
|
|
|
|
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
|
|
|
export default function getRouteFromEntrypoint(
|
2022-05-03 12:37:23 +02:00
|
|
|
entryFile: string,
|
2022-05-25 11:46:26 +02:00
|
|
|
app?: boolean
|
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
|
|
|
}
|
|
|
|
|
2022-05-25 11:46:26 +02:00
|
|
|
if (app) {
|
2022-08-10 21:31:01 +02:00
|
|
|
pagePath = getAppRouteFromEntrypoint(entryFile)
|
2022-05-03 12:37:23 +02:00
|
|
|
if (pagePath) return pagePath
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|