Skip to content

Commit

Permalink
chore: cache buildSlots result
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 committed Jan 23, 2025
1 parent 6529c3d commit 7327a57
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
2 changes: 2 additions & 0 deletions packages/compiler-core/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ export interface PlainElementNode extends BaseElementNode {

export interface ComponentNode extends BaseElementNode {
tagType: ElementTypes.COMPONENT
slots: SlotsExpression
hasDynamicSlots: boolean
codegenNode:
| VNodeCall
| CacheExpression // when cached by v-once
Expand Down
5 changes: 4 additions & 1 deletion packages/compiler-core/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ export const transformElement: NodeTransform = (node, context) => {
vnodeTag !== KEEP_ALIVE

if (shouldBuildAsSlots) {
const { slots, hasDynamicSlots } = buildSlots(node, context)
const { slots, hasDynamicSlots } = buildSlots(
node as ComponentNode,
context,
)
vnodeChildren = slots
if (hasDynamicSlots) {
patchFlag |= PatchFlags.DYNAMIC_SLOTS
Expand Down
6 changes: 2 additions & 4 deletions packages/compiler-core/src/transforms/vSkip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
NodeTypes,
type SimpleExpressionNode,
type SkipNode,
type SlotsExpression,
type TemplateChildNode,
createConditionalExpression,
createSimpleExpression,
Expand Down Expand Up @@ -79,9 +78,8 @@ export function processSkip(
// if not found, throw an error
if (node.tagType === ElementTypes.COMPONENT) {
const { slots } = buildSlots(node, context)
const genChildren = slots as SlotsExpression
if (genChildren.type === NodeTypes.JS_OBJECT_EXPRESSION) {
const prop = genChildren.properties.find(
if (slots.type === NodeTypes.JS_OBJECT_EXPRESSION) {
const prop = slots.properties.find(
p =>
p.type === NodeTypes.JS_PROPERTY &&
p.key.type === NodeTypes.SIMPLE_EXPRESSION &&
Expand Down
13 changes: 11 additions & 2 deletions packages/compiler-core/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {
type CallExpression,
type ComponentNode,
type ConditionalExpression,
type DirectiveNode,
type ElementNode,
ElementTypes,
type ExpressionNode,
type FunctionExpression,
Expand Down Expand Up @@ -114,13 +114,20 @@ const buildClientSlotFn: SlotFnBuilder = (props, _vForExp, children, loc) =>
// Instead of being a DirectiveTransform, v-slot processing is called during
// transformElement to build the slots object for a component.
export function buildSlots(
node: ElementNode,
node: ComponentNode,
context: TransformContext,
buildSlotFn: SlotFnBuilder = buildClientSlotFn,
): {
slots: SlotsExpression
hasDynamicSlots: boolean
} {
// return early if slots are already built to avoid duplication
if (node.slots) {
return {
slots: node.slots,
hasDynamicSlots: node.hasDynamicSlots,
}
}
context.helper(WITH_CTX)

const { children, loc } = node
Expand Down Expand Up @@ -364,6 +371,8 @@ export function buildSlots(
]) as SlotsExpression
}

node.slots = slots
node.hasDynamicSlots = hasDynamicSlots
return {
slots,
hasDynamicSlots,
Expand Down

0 comments on commit 7327a57

Please sign in to comment.