Unverified Commit a2e8a193 authored by vercel[bot]'s avatar vercel[bot] Committed by GitHub

Optimize segment explorer path normalization

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent cf10c50a
......@@ -5,6 +5,19 @@ export const BUILTIN_PREFIX = '__next_builtin__'
const nextInternalPrefixRegex =
/^(.*[\\/])?next[\\/]dist[\\/]client[\\/]components[\\/]builtin[\\/]/
function removeFirstOccurrence(value: string, search: string): string {
const index = value.indexOf(search)
if (index === -1) {
return value
}
return value.slice(0, index) + value.slice(index + search.length)
}
function removeLeadingPathSeparator(value: string): string {
const first = value.charCodeAt(0)
return first === 47 || first === 92 ? value.slice(1) : value
}
/**
* Normalize a file path to be relative to the project directory.
* Handles Turbopack [project] prefix and monorepo setups.
......@@ -17,36 +30,39 @@ export function normalizeFilePath(
// When project root is not the working directory, we can extract the relative project root path.
// This is mostly used for running Next.js inside a monorepo.
const cwd = process.env.NEXT_RUNTIME === 'edge' ? '' : process.cwd()
const relativeProjectRoot = projectDir.replace(cwd, '').replace(/^[\\/]/, '')
let relativePath = (filePath || '')
// remove turbopack [project] prefix
.replace(/^\[project\][\\/]?/, '')
// remove the project root from the path (absolute)
.replace(projectDir, '')
// remove cwd prefix (absolute)
.replace(cwd, '')
// normalize path separators and remove leading slash
.replace(/\\/g, '/')
.replace(/^\//, '')
const relativeProjectRoot = removeLeadingPathSeparator(
removeFirstOccurrence(projectDir, cwd)
)
let relativePath = filePath || ''
if (relativePath.startsWith('[project]')) {
relativePath = removeLeadingPathSeparator(relativePath.slice(9))
}
relativePath = removeFirstOccurrence(relativePath, projectDir)
relativePath = removeFirstOccurrence(relativePath, cwd)
if (relativePath.includes('\\')) {
relativePath = relativePath.replaceAll('\\', '/')
}
relativePath = removeLeadingPathSeparator(relativePath)
// remove relative project path prefix (e.g., "test/e2e/app-dir/actions/")
if (relativeProjectRoot && relativePath.startsWith(relativeProjectRoot)) {
relativePath = relativePath
.slice(relativeProjectRoot.length)
.replace(/^\//, '')
relativePath = removeLeadingPathSeparator(
relativePath.slice(relativeProjectRoot.length)
)
}
// Handle case where filename is relative to a parent of projectDir
// (e.g., in tests where filename is "test/tmp/next-test-XXX/app/page.js"
// but projectDir is the test temp directory)
if (relativePath.includes('/')) {
const projectDirName = projectDir.split(/[\\/]/).pop() || ''
const projectDirNameStart =
Math.max(projectDir.lastIndexOf('/'), projectDir.lastIndexOf('\\')) + 1
const projectDirName = projectDir.slice(projectDirNameStart)
if (projectDirName) {
const projectDirWithSlash = projectDirName + '/'
const idx = relativePath.indexOf(projectDirWithSlash)
const idx = relativePath.indexOf(projectDirName + '/')
if (idx >= 0) {
relativePath = relativePath.slice(idx + projectDirWithSlash.length)
relativePath = relativePath.slice(idx + projectDirName.length + 1)
}
}
}
......@@ -59,14 +75,17 @@ export function normalizeConventionFilePath(
conventionPath: string | undefined
) {
let relativePath = normalizeFilePath(projectDir, conventionPath)
// remove /(src/)?app/ dir prefix
.replace(/^(src\/)?app\//, '')
if (relativePath.startsWith('src/app/')) {
relativePath = relativePath.slice(8)
} else if (relativePath.startsWith('app/')) {
relativePath = relativePath.slice(4)
}
// If it's internal file only keep the filename, strip nextjs internal prefix
if (nextInternalPrefixRegex.test(relativePath)) {
relativePath = relativePath.replace(nextInternalPrefixRegex, '')
const internalPath = relativePath.replace(nextInternalPrefixRegex, '')
if (internalPath !== relativePath) {
// Add a special prefix to let segment explorer know it's a built-in component
relativePath = `${BUILTIN_PREFIX}${relativePath}`
relativePath = `${BUILTIN_PREFIX}${internalPath}`
}
return relativePath
......
import {
BUILTIN_PREFIX,
normalizeConventionFilePath,
normalizeFilePath,
} from '../../packages/next/src/server/app-render/segment-explorer-path'
describe('segment explorer path normalization', () => {
const cwd = process.cwd()
describe('normalizeFilePath', () => {
it.each([
{
name: 'absolute project path',
projectDir: `${cwd}/apps/site`,
filePath: `${cwd}/apps/site/src/app/dashboard/page.tsx`,
expected: 'src/app/dashboard/page.tsx',
},
{
name: 'Turbopack project prefix',
projectDir: cwd,
filePath: '[project]/app/blog/[slug]/page.tsx',
expected: 'app/blog/[slug]/page.tsx',
},
{
name: 'monorepo project prefix',
projectDir: `${cwd}/apps/site`,
filePath: '[project]/apps/site/src/app/page.tsx',
expected: 'src/app/page.tsx',
},
{
name: 'project directory embedded in a parent-relative path',
projectDir: '/tmp/next-test-abc',
filePath: 'test/tmp/next-test-abc/app/page.js',
// Removing the embedded absolute project path preserves its `test` prefix.
expected: 'test/app/page.js',
},
{
name: 'Windows separators',
projectDir: 'C:\\repo\\site',
filePath: 'C:\\repo\\site\\src\\app\\page.tsx',
expected: 'src/app/page.tsx',
},
{
name: 'missing file path',
projectDir: cwd,
filePath: undefined,
expected: '',
},
])('$name', ({ projectDir, filePath, expected }) => {
expect(normalizeFilePath(projectDir, filePath)).toBe(expected)
})
})
describe('normalizeConventionFilePath', () => {
it.each([
['app/page.tsx', 'page.tsx'],
['src/app/dashboard/layout.tsx', 'dashboard/layout.tsx'],
[
`${cwd}/node_modules/next/dist/client/components/builtin/global-error.js`,
`${BUILTIN_PREFIX}global-error.js`,
],
[
'[project]/app/next/dist/client/components/builtin/not-found.js',
`${BUILTIN_PREFIX}not-found.js`,
],
])('normalizes %s', (filePath, expected) => {
expect(normalizeConventionFilePath(cwd, filePath)).toBe(expected)
})
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment