Skip to content

Commit 456a7e2

Browse files
committed
Optimize
1 parent 7de2d95 commit 456a7e2

File tree

8 files changed

+288
-215
lines changed

8 files changed

+288
-215
lines changed

src/code-impl.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
resetMainComponentCache,
55
} from './codegen/Codegen'
66
import { resetGetPropsCache } from './codegen/props'
7+
import { resetChildAnimationCache } from './codegen/props/reaction'
78
import { resetSelectorPropsCache } from './codegen/props/selector'
89
import { ResponsiveCodegen } from './codegen/responsive/ResponsiveCodegen'
910
import { nodeProxyTracker } from './codegen/utils/node-proxy'
@@ -139,6 +140,7 @@ export function registerCodegen(ctx: typeof figma) {
139140
perfReset()
140141
resetGetPropsCache()
141142
resetSelectorPropsCache()
143+
resetChildAnimationCache()
142144
resetVariableCache()
143145
resetTextStyleCache()
144146
resetMainComponentCache()

src/codegen/Codegen.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,20 +100,23 @@ function getBooleanConditionName(
100100
}
101101

102102
/**
103-
* Clone a NodeTree — shallow-clone props at every level so mutations
104-
* to one clone's props don't affect the cached original or other clones.
103+
* Shallow-clone a NodeTree — only the root-level props object is cloned
104+
* (downstream code mutates tree.props via Object.assign).
105+
* Children and textChildren are shared by reference because they are
106+
* never mutated (verified: no push/pop/splice/sort on children arrays,
107+
* no child.props mutations in ResponsiveCodegen).
105108
*/
106109
function cloneTree(tree: NodeTree): NodeTree {
107110
return {
108111
component: tree.component,
109112
props: { ...tree.props },
110-
children: tree.children.map(cloneTree),
113+
children: tree.children,
111114
nodeType: tree.nodeType,
112115
nodeName: tree.nodeName,
113116
isComponent: tree.isComponent,
114117
isSlot: tree.isSlot,
115118
condition: tree.condition,
116-
textChildren: tree.textChildren ? [...tree.textChildren] : undefined,
119+
textChildren: tree.textChildren,
117120
}
118121
}
119122

src/codegen/props/background.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BLEND_MODE_MAP } from '../utils/blend-mode-map'
2-
import { paintToCSS } from '../utils/paint-to-css'
2+
import { paintToCSS, paintToCSSSyncIfPossible } from '../utils/paint-to-css'
33

44
export async function getBackgroundProps(
55
node: SceneNode,
@@ -21,7 +21,9 @@ export async function getBackgroundProps(
2121
for (let i = 0; i < node.fills.length; i++) {
2222
const fill = node.fills[node.fills.length - 1 - i]
2323
if (fill.opacity === 0 || !fill.visible) continue
24-
const cssFill = await paintToCSS(fill, node, i === node.fills.length - 1)
24+
const cssFill =
25+
paintToCSSSyncIfPossible(fill, node, i === node.fills.length - 1) ??
26+
(await paintToCSS(fill, node, i === node.fills.length - 1))
2527
if (
2628
fill.type === 'SOLID' &&
2729
fill.blendMode &&

src/codegen/props/border.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { addPx } from '../utils/add-px'
22
import { fourValueShortcut } from '../utils/four-value-shortcut'
3-
import { paintToCSS } from '../utils/paint-to-css'
3+
import { paintToCSS, paintToCSSSyncIfPossible } from '../utils/paint-to-css'
44

55
export function getBorderRadiusProps(
66
node: SceneNode,
@@ -45,7 +45,8 @@ export async function getBorderProps(
4545
const paint = node.strokes[node.strokes.length - 1 - i]
4646
if (paint.visible && paint.opacity !== 0) {
4747
paintCssList.push(
48-
await paintToCSS(paint, node, i === node.strokes.length - 1),
48+
paintToCSSSyncIfPossible(paint, node, i === node.strokes.length - 1) ??
49+
(await paintToCSS(paint, node, i === node.strokes.length - 1)),
4950
)
5051
}
5152
}

0 commit comments

Comments
 (0)