瀏覽代碼

chore: tweaks

daiwei 5 月之前
父節點
當前提交
8cacccba96

+ 1 - 5
packages/compiler-vapor/src/generators/component.ts

@@ -462,11 +462,7 @@ function genSlotBlockWithProps(oper: SlotBlockIRNode, context: CodegenContext) {
 
   if (
     node.type === NodeTypes.ELEMENT &&
-    // // Not a real component
-    // !isTeleportTag(node.tag) &&
-    // // Needs to determine whether to activate/deactivate based on instance.parent being KeepAlive
-    // !isKeepAliveTag(node.tag) &&
-    // // Slot updates need to trigger TransitionGroup's onBeforeUpdate/onUpdated hook
+    // Slot updates need to trigger TransitionGroup's onBeforeUpdate/onUpdated hook
     !isTransitionGroupTag(node.tag)
   ) {
     // wrap with withVaporCtx to ensure correct currentInstance inside slot

+ 5 - 11
packages/runtime-vapor/src/block.ts

@@ -252,22 +252,16 @@ export function setScopeId(block: Block, scopeIds: string[]): void {
 }
 
 export function setComponentScopeId(instance: VaporComponentInstance): void {
-  const parent = instance.parent
-  const slotScopeOwner = instance.slotScopeOwner
-  if (!parent && !slotScopeOwner) return
+  const { parent, slotOwner } = instance
+  if (!parent && !slotOwner) return
   // prevent setting scopeId on multi-root fragments
   if (isArray(instance.block) && instance.block.length > 1) return
 
   const scopeIds: string[] = []
 
-  const slotScopeId = slotScopeOwner && slotScopeOwner.type.__scopeId
-  const parentScopeId = parent && parent.type.__scopeId
-
-  if (slotScopeId) {
-    scopeIds.push(slotScopeId)
-  } else if (parentScopeId) {
-    scopeIds.push(parentScopeId)
-  }
+  const scopeOwner = slotOwner || parent
+  const scopeId = scopeOwner && scopeOwner.type.__scopeId
+  if (scopeId) scopeIds.push(scopeId)
 
   // inherit scopeId from vdom parent
   if (

+ 8 - 11
packages/runtime-vapor/src/component.ts

@@ -72,7 +72,7 @@ import {
   dynamicSlotsProxyHandlers,
   getSlot,
   getSlotConsumer,
-  getSlotScopeOwner,
+  getSlotOwner,
 } from './componentSlots'
 import { hmrReload, hmrRerender } from './hmr'
 import {
@@ -180,10 +180,8 @@ export function createComponent(
   rawSlots?: LooseRawSlots | null,
   isSingleRoot?: boolean,
   once?: boolean,
-  appContext: GenericAppContext = (((getSlotScopeOwner() as VaporComponentInstance | null) ||
-    (currentInstance as VaporComponentInstance | null)) &&
-    ((getSlotScopeOwner() as VaporComponentInstance | null) ||
-      (currentInstance as VaporComponentInstance | null))!.appContext) ||
+  appContext: GenericAppContext = (currentInstance &&
+    currentInstance.appContext) ||
     emptyContext,
 ): VaporComponentInstance {
   const _insertionParent = insertionParent
@@ -196,8 +194,7 @@ export function createComponent(
   }
 
   const parentInstance =
-    (getSlotConsumer() as VaporComponentInstance | null) ||
-    (currentInstance as VaporComponentInstance | null)
+    getSlotConsumer() || (currentInstance as VaporComponentInstance | null)
 
   if (
     isSingleRoot &&
@@ -480,8 +477,8 @@ export class VaporComponentInstance implements GenericComponentInstance {
 
   slots: StaticSlots
 
-  // slot template owner for scope inheritance
-  slotScopeOwner: VaporComponentInstance | null
+  // slot owner for scopeId inheritance
+  slotOwner?: VaporComponentInstance | null
 
   // to hold vnode props / slots in vdom interop mode
   rawPropsRef?: ShallowRef<any>
@@ -609,7 +606,7 @@ export class VaporComponentInstance implements GenericComponentInstance {
         : rawSlots
       : EMPTY_OBJ
 
-    this.slotScopeOwner = getSlotScopeOwner() as VaporComponentInstance | null
+    this.slotOwner = getSlotOwner()
 
     // apply custom element special handling
     if (comp.ce) {
@@ -683,7 +680,7 @@ export function createPlainElement(
   ;(el as any).$root = isSingleRoot
 
   if (!isHydrating) {
-    const scopeOwner = getSlotScopeOwner() || currentInstance
+    const scopeOwner = getSlotOwner() || currentInstance
     const scopeId = scopeOwner && scopeOwner.type.__scopeId
     if (scopeId) setScopeId(el, [scopeId])
   }

+ 15 - 27
packages/runtime-vapor/src/componentSlots.ts

@@ -125,30 +125,26 @@ export function getSlot(
 // Tracks slot execution context: the owner that defined the slot, and the
 // consumer that is currently rendering it.
 const slotOwnerStack: (VaporComponentInstance | null)[] = []
-const slotConsumerStack: (GenericComponentInstance | null)[] = []
+const slotConsumerStack: (VaporComponentInstance | null)[] = []
 
-export function getSlotScopeOwner(): GenericComponentInstance | null {
+export function getSlotOwner(): VaporComponentInstance | null {
   return slotOwnerStack.length > 0
     ? slotOwnerStack[slotOwnerStack.length - 1]
     : null
 }
 
-export function getSlotConsumer(): GenericComponentInstance | null {
+export function getSlotConsumer(): VaporComponentInstance | null {
   return slotConsumerStack.length > 0
     ? slotConsumerStack[slotConsumerStack.length - 1]
     : null
 }
 
 export function withVaporCtx(fn: Function): BlockFn {
-  const owner =
-    (currentInstance as VaporComponentInstance | null) ||
-    (getSlotScopeOwner() as VaporComponentInstance | null)
-
-  const ownerInstance = owner!
+  const ownerInstance = currentInstance as VaporComponentInstance | null
   return (...args: any[]) => {
     const prev = setCurrentInstance(ownerInstance)
     slotOwnerStack.push(ownerInstance)
-    slotConsumerStack.push(prev[0])
+    slotConsumerStack.push(prev[0] as VaporComponentInstance | null)
     try {
       return fn(...args)
     } finally {
@@ -170,25 +166,21 @@ export function createSlot(
   const _isLastInsertion = isLastInsertion
   if (!isHydrating) resetInsertionState()
 
-  const slotContext =
-    (currentInstance as VaporComponentInstance | null) ||
-    (getSlotScopeOwner() as VaporComponentInstance | null)
-  const owner =
-    (getSlotScopeOwner() as VaporComponentInstance | null) || slotContext!
-  const rawSlots = slotContext!.rawSlots
+  const consumer = currentInstance as VaporComponentInstance
+  const owner = getSlotOwner() || consumer
+  const rawSlots = consumer!.rawSlots
   const slotProps = rawProps
     ? new Proxy(rawProps, rawPropsProxyHandlers)
     : EMPTY_OBJ
 
   let fragment: DynamicFragment
-  const contextInstance = owner
   if (isRef(rawSlots._)) {
     if (isHydrating) locateHydrationNode()
-    fragment = contextInstance.appContext.vapor!.vdomSlot(
+    fragment = owner.appContext.vapor!.vdomSlot(
       rawSlots._,
       name,
       slotProps,
-      contextInstance,
+      owner,
       fallback,
     )
   } else {
@@ -198,10 +190,9 @@ export function createSlot(
         : new DynamicFragment()
     const isDynamicName = isFunction(name)
 
-    // Calculate slotScopeIds once (for vdom interop)
     const slotScopeIds: string[] = []
     if (!noSlotted) {
-      const scopeId = contextInstance.type.__scopeId
+      const scopeId = owner.type.__scopeId
       if (scopeId) {
         slotScopeIds.push(`${scopeId}-s`)
       }
@@ -210,14 +201,11 @@ export function createSlot(
     const renderSlot = () => {
       const slotName = isFunction(name) ? name() : name
 
-      // in custom element mode, render <slot/> as actual slot outlets
-      // because in shadowRoot: false mode the slot element gets
-      // replaced by injected content
       if (
-        (slotContext as GenericComponentInstance).ce ||
-        (slotContext!.parent &&
-          isAsyncWrapper(slotContext!.parent) &&
-          slotContext!.parent.ce)
+        (consumer as GenericComponentInstance).ce ||
+        (consumer.parent &&
+          isAsyncWrapper(consumer.parent) &&
+          consumer.parent.ce)
       ) {
         const el = createElement('slot')
         renderEffect(() => {

+ 3 - 4
packages/runtime-vapor/src/vdomInterop.ts

@@ -59,7 +59,7 @@ import {
 } from '@vue/shared'
 import { type RawProps, rawPropsProxyHandlers } from './componentProps'
 import type { RawSlots, VaporSlot } from './componentSlots'
-import { currentSlotScopeIds, getSlotScopeOwner } from './componentSlots'
+import { currentSlotScopeIds, getSlotOwner } from './componentSlots'
 import { renderEffect } from './renderEffect'
 import { _next, createTextNode } from './dom/node'
 import { optimizePropertyLookup } from './dom/prop'
@@ -335,10 +335,9 @@ function createVDOMComponent(
     frag.nodes = vnode.el as any
   }
 
-  const slotScopeOwner = getSlotScopeOwner()
   const scopeOwner =
-    (isVaporComponent(slotScopeOwner) ? slotScopeOwner : null) ||
-    (parentInstance && parentInstance.slotScopeOwner) ||
+    getSlotOwner() ||
+    (parentInstance && parentInstance.slotOwner) ||
     parentInstance
   vnode.scopeId = scopeOwner && scopeOwner.type.__scopeId!
   vnode.slotScopeIds = currentSlotScopeIds