| import type { LoaderTree } from '../lib/app-dir-module' |
|
|
| export const BUILTIN_PREFIX = '__next_builtin__' |
|
|
| const nextInternalPrefixRegex = |
| /^(.*[\\/])?next[\\/]dist[\\/]client[\\/]components[\\/]builtin[\\/]/ |
|
|
| export function normalizeConventionFilePath( |
| projectDir: string, |
| conventionPath: string | undefined |
| ) { |
| const cwd = process.env.NEXT_RUNTIME === 'edge' ? '' : process.cwd() |
|
|
| let relativePath = (conventionPath || '') |
| |
| .replace(/^\[project\][\\/]/, '') |
| |
| .replace(projectDir, '') |
| |
| .replace(cwd, '') |
| |
| .replace(/^([\\/])*(src[\\/])?app[\\/]/, '') |
|
|
| |
| if (nextInternalPrefixRegex.test(relativePath)) { |
| relativePath = relativePath.replace(nextInternalPrefixRegex, '') |
| |
| relativePath = `${BUILTIN_PREFIX}${relativePath}` |
| } |
|
|
| return relativePath |
| } |
|
|
| |
| |
| |
| export const isNextjsBuiltinFilePath = (filePath: string) => { |
| return nextInternalPrefixRegex.test(filePath) |
| } |
|
|
| export const BOUNDARY_SUFFIX = '@boundary' |
| export function normalizeBoundaryFilename(filename: string) { |
| return filename |
| .replace(new RegExp(`^${BUILTIN_PREFIX}`), '') |
| .replace(new RegExp(`${BOUNDARY_SUFFIX}$`), '') |
| } |
|
|
| export const BOUNDARY_PREFIX = 'boundary:' |
| export function isBoundaryFile(fileType: string) { |
| return fileType.startsWith(BOUNDARY_PREFIX) |
| } |
|
|
| |
| |
| |
| export function isBuiltinBoundaryFile(fileType: string) { |
| return fileType.startsWith(BUILTIN_PREFIX) |
| } |
|
|
| export function getBoundaryOriginFileType(fileType: string) { |
| return fileType.replace(BOUNDARY_PREFIX, '') |
| } |
|
|
| export function getConventionPathByType( |
| tree: LoaderTree, |
| dir: string, |
| conventionType: |
| | 'layout' |
| | 'template' |
| | 'page' |
| | 'not-found' |
| | 'error' |
| | 'loading' |
| | 'forbidden' |
| | 'unauthorized' |
| | 'defaultPage' |
| | 'global-error' |
| ) { |
| const modules = tree[2] |
| const conventionPath = modules[conventionType] |
| ? modules[conventionType][1] |
| : undefined |
| if (conventionPath) { |
| return normalizeConventionFilePath(dir, conventionPath) |
| } |
| return undefined |
| } |
|
|