Skip to content

Commit

Permalink
Minor optimizations in getComponentDescriptorForTarget (#6306)
Browse files Browse the repository at this point in the history
**Problem:**
1. Let's early return for intrinsic elements, which are not component
instances, so no reason to search for component descriptors at all.
2. Let's remove the regex replaces which remove file extensions and
replace them with `stripExtension` function calls, which are
significantly faster.
3. 
**Manual Tests:**
I hereby swear that:

- [x] I opened a hydrogen project and it loaded
- [x] I could navigate to various routes in Preview mode
  • Loading branch information
gbalint authored and liady committed Dec 13, 2024
1 parent 2c82990 commit 41e5b4e
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions editor/src/core/property-controls/property-controls-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { getFilePathMappings } from '../model/project-file-utils'
import { valueDependentCache } from '../shared/memoize'
import * as EP from '../shared/element-path'
import { shallowEqual } from '../shared/equality-utils'
import { stripExtension } from '../../components/custom-code/custom-code-utils'

export function propertyControlsForComponentInFile(
componentName: string,
Expand Down Expand Up @@ -170,6 +171,10 @@ function getComponentDescriptorForTargetInner(
underlyingFilePath: string,
) => {
if (isJSXElement(element)) {
if (isIntrinsicElement(element.name)) {
return null
}

const importedFrom = importedFromWhere(
filePathMappings,
underlyingFilePath,
Expand All @@ -180,11 +185,7 @@ function getComponentDescriptorForTargetInner(

let filenameForLookup: string | null = null
if (importedFrom == null) {
if (isIntrinsicElement(element.name)) {
return null
} else {
filenameForLookup = underlyingFilePath.replace(/\.(js|jsx|ts|tsx)$/, '')
}
filenameForLookup = stripExtension(underlyingFilePath)
} else {
filenameForLookup = importedFrom.filePath
}
Expand Down Expand Up @@ -216,7 +217,7 @@ function getComponentDescriptorForTargetInner(
)

const trimmedPath = absolutePath.includes('/')
? absolutePath.replace(/\.(js|jsx|ts|tsx)$/, '')
? stripExtension(absolutePath)
: absolutePath

return propertyControlsInfo[trimmedPath]?.[nameAsString]
Expand Down

0 comments on commit 41e5b4e

Please sign in to comment.