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

perf: optimize builtin convention path matching

Co-Authored-By: 's avatarMarcos Hernanz <96699542+marcoshernanz@users.noreply.github.com>
parent a2e8a193
...@@ -4,6 +4,8 @@ export const BUILTIN_PREFIX = '__next_builtin__' ...@@ -4,6 +4,8 @@ export const BUILTIN_PREFIX = '__next_builtin__'
const nextInternalPrefixRegex = const nextInternalPrefixRegex =
/^(.*[\\/])?next[\\/]dist[\\/]client[\\/]components[\\/]builtin[\\/]/ /^(.*[\\/])?next[\\/]dist[\\/]client[\\/]components[\\/]builtin[\\/]/
const normalizedNextInternalPrefix = 'next/dist/client/components/builtin/'
const lineTerminatorRegex = /[\n\r\u2028\u2029]/
function removeFirstOccurrence(value: string, search: string): string { function removeFirstOccurrence(value: string, search: string): string {
const index = value.indexOf(search) const index = value.indexOf(search)
...@@ -81,11 +83,34 @@ export function normalizeConventionFilePath( ...@@ -81,11 +83,34 @@ export function normalizeConventionFilePath(
relativePath = relativePath.slice(4) relativePath = relativePath.slice(4)
} }
// If it's internal file only keep the filename, strip nextjs internal prefix // If it's internal file only keep the filename, strip nextjs internal prefix.
const internalPath = relativePath.replace(nextInternalPrefixRegex, '') // Search from the end to preserve the greedy regex's last-prefix semantics.
if (internalPath !== relativePath) { let firstLineTerminatorIndex: number | undefined
let internalPrefixIndex = relativePath.length
while (
(internalPrefixIndex = relativePath.lastIndexOf(
normalizedNextInternalPrefix,
internalPrefixIndex - 1
)) !== -1
) {
if (
internalPrefixIndex === 0 ||
relativePath.charCodeAt(internalPrefixIndex - 1) === 47
) {
if (firstLineTerminatorIndex === undefined) {
const index = relativePath.search(lineTerminatorRegex)
firstLineTerminatorIndex = index === -1 ? relativePath.length : index
}
if (internalPrefixIndex < firstLineTerminatorIndex) {
// Add a special prefix to let segment explorer know it's a built-in component // Add a special prefix to let segment explorer know it's a built-in component
relativePath = `${BUILTIN_PREFIX}${internalPath}` relativePath =
BUILTIN_PREFIX +
relativePath.slice(
internalPrefixIndex + normalizedNextInternalPrefix.length
)
break
}
}
} }
return relativePath return relativePath
......
...@@ -63,8 +63,40 @@ describe('segment explorer path normalization', () => { ...@@ -63,8 +63,40 @@ describe('segment explorer path normalization', () => {
'[project]/app/next/dist/client/components/builtin/not-found.js', '[project]/app/next/dist/client/components/builtin/not-found.js',
`${BUILTIN_PREFIX}not-found.js`, `${BUILTIN_PREFIX}not-found.js`,
], ],
[
'next/dist/client/components/builtin/first/next/dist/client/components/builtin/last.js',
`${BUILTIN_PREFIX}last.js`,
],
[
'app/xnext/dist/client/components/builtin/not-built-in.js',
'xnext/dist/client/components/builtin/not-built-in.js',
],
[
'app\\next\\dist\\client\\components\\builtin\\error.js',
`${BUILTIN_PREFIX}error.js`,
],
])('normalizes %s', (filePath, expected) => { ])('normalizes %s', (filePath, expected) => {
expect(normalizeConventionFilePath(cwd, filePath)).toBe(expected) expect(normalizeConventionFilePath(cwd, filePath)).toBe(expected)
}) })
it.each(['\n', '\r', '\u2028', '\u2029'])(
'does not match a built-in prefix after line terminator %p',
(lineTerminator) => {
const filePath = `app/foo${lineTerminator}bar/next/dist/client/components/builtin/error.js`
expect(normalizeConventionFilePath(cwd, filePath)).toBe(
filePath.slice(4)
)
}
)
it('uses the last built-in prefix before a line terminator', () => {
const suffix = 'first\nx/next/dist/client/components/builtin/last.js'
expect(
normalizeConventionFilePath(
cwd,
`next/dist/client/components/builtin/${suffix}`
)
).toBe(`${BUILTIN_PREFIX}${suffix}`)
})
}) })
}) })
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