Skip to content

feat(vapor): scopeId #13422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 8 commits into
base: vapor
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,97 @@ export function render(_ctx) {
}"
`;

exports[`compiler: transform slot > forwarded slots > <slot w/ nested component> 1`] = `
"import { forwardedSlotCreator as _forwardedSlotCreator, resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _createForwardedSlot = _forwardedSlotCreator()
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n1 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createForwardedSlot("default", null)
return n0
}
})
return n1
}
}, true)
return n2
}"
`;

exports[`compiler: transform slot > forwarded slots > <slot> tag only 1`] = `
"import { forwardedSlotCreator as _forwardedSlotCreator, resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _createForwardedSlot = _forwardedSlotCreator()
const _component_Comp = _resolveComponent("Comp")
const n1 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createForwardedSlot("default", null)
return n0
}
}, true)
return n1
}"
`;

exports[`compiler: transform slot > forwarded slots > <slot> tag w/ template 1`] = `
"import { forwardedSlotCreator as _forwardedSlotCreator, resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _createForwardedSlot = _forwardedSlotCreator()
const _component_Comp = _resolveComponent("Comp")
const n2 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createForwardedSlot("default", null)
return n0
}
}, true)
return n2
}"
`;

exports[`compiler: transform slot > forwarded slots > <slot> tag w/ v-for 1`] = `
"import { forwardedSlotCreator as _forwardedSlotCreator, resolveComponent as _resolveComponent, createFor as _createFor, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _createForwardedSlot = _forwardedSlotCreator()
const _component_Comp = _resolveComponent("Comp")
const n3 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createFor(() => (_ctx.b), (_for_item0) => {
const n2 = _createForwardedSlot("default", null)
return n2
})
return n0
}
}, true)
return n3
}"
`;

exports[`compiler: transform slot > forwarded slots > <slot> tag w/ v-if 1`] = `
"import { forwardedSlotCreator as _forwardedSlotCreator, resolveComponent as _resolveComponent, createIf as _createIf, createComponentWithFallback as _createComponentWithFallback } from 'vue';

export function render(_ctx) {
const _createForwardedSlot = _forwardedSlotCreator()
const _component_Comp = _resolveComponent("Comp")
const n3 = _createComponentWithFallback(_component_Comp, null, {
"default": () => {
const n0 = _createIf(() => (_ctx.ok), () => {
const n2 = _createForwardedSlot("default", null)
return n2
})
return n0
}
}, true)
return n3
}"
`;

exports[`compiler: transform slot > implicit default slot 1`] = `
"import { resolveComponent as _resolveComponent, createComponentWithFallback as _createComponentWithFallback, template as _template } from 'vue';
const t0 = _template("<div></div>")
Expand Down
29 changes: 29 additions & 0 deletions packages/compiler-vapor/__tests__/transforms/vSlot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,35 @@ describe('compiler: transform slot', () => {
})
})

describe('forwarded slots', () => {
test('<slot> tag only', () => {
const { code } = compileWithSlots(`<Comp><slot/></Comp>`)
expect(code).toMatchSnapshot()
})

test('<slot> tag w/ v-if', () => {
const { code } = compileWithSlots(`<Comp><slot v-if="ok"/></Comp>`)
expect(code).toMatchSnapshot()
})

test('<slot> tag w/ v-for', () => {
const { code } = compileWithSlots(`<Comp><slot v-for="a in b"/></Comp>`)
expect(code).toMatchSnapshot()
})

test('<slot> tag w/ template', () => {
const { code } = compileWithSlots(
`<Comp><template #default><slot/></template></Comp>`,
)
expect(code).toMatchSnapshot()
})

test('<slot w/ nested component>', () => {
const { code } = compileWithSlots(`<Comp><Comp><slot/></Comp></Comp>`)
expect(code).toMatchSnapshot()
})
})

describe('errors', () => {
test('error on extraneous children w/ named default slot', () => {
const onError = vi.fn()
Expand Down
7 changes: 7 additions & 0 deletions packages/compiler-vapor/src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
genCall,
} from './generators/utils'
import { setTemplateRefIdent } from './generators/templateRef'
import { createForwardedSlotIdent } from './generators/slotOutlet'

export type CodegenOptions = Omit<BaseCodegenOptions, 'optimizeImports'>

Expand Down Expand Up @@ -129,6 +130,12 @@ export function generate(
`const ${setTemplateRefIdent} = ${context.helper('createTemplateRefSetter')}()`,
)
}
if (ir.hasForwardedSlot) {
push(
NEWLINE,
`const ${createForwardedSlotIdent} = ${context.helper('forwardedSlotCreator')}()`,
)
}
push(...genBlockContent(ir.block, context, true))
push(INDENT_END, NEWLINE)

Expand Down
3 changes: 2 additions & 1 deletion packages/compiler-vapor/src/generators/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export function genCreateComponent(
const { helper } = context

const tag = genTag()
const { root, props, slots, once } = operation
const { root, props, slots, once, scopeId } = operation
const rawSlots = genRawSlots(slots, context)
const [ids, handlers] = processInlineHandlers(props, context)
const rawProps = context.withId(() => genRawProps(props, context), ids)
Expand Down Expand Up @@ -75,6 +75,7 @@ export function genCreateComponent(
rawSlots,
root ? 'true' : false,
once && 'true',
scopeId && JSON.stringify(scopeId),
),
...genDirectivesForElement(operation.id, context),
]
Expand Down
6 changes: 4 additions & 2 deletions packages/compiler-vapor/src/generators/slotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { genExpression } from './expression'
import { type CodeFragment, NEWLINE, buildCodeFragment, genCall } from './utils'
import { genRawProps } from './component'

export const createForwardedSlotIdent = `_createForwardedSlot`

export function genSlotOutlet(
oper: SlotOutletIRNode,
context: CodegenContext,
): CodeFragment[] {
const { helper } = context
const { id, name, fallback } = oper
const { id, name, fallback, forwarded } = oper
const [frag, push] = buildCodeFragment()

const nameExpr = name.isStatic
Expand All @@ -26,7 +28,7 @@ export function genSlotOutlet(
NEWLINE,
`const n${id} = `,
...genCall(
helper('createSlot'),
forwarded ? createForwardedSlotIdent : helper('createSlot'),
nameExpr,
genRawProps(oper.props, context) || 'null',
fallbackArg,
Expand Down
3 changes: 3 additions & 0 deletions packages/compiler-vapor/src/ir/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface RootIRNode {
directive: Set<string>
block: BlockIRNode
hasTemplateRef: boolean
hasForwardedSlot: boolean
}

export interface IfIRNode extends BaseIRNode {
Expand Down Expand Up @@ -196,6 +197,7 @@ export interface CreateComponentIRNode extends BaseIRNode {
dynamic?: SimpleExpressionNode
parent?: number
anchor?: number
scopeId?: string | null
}

export interface DeclareOldRefIRNode extends BaseIRNode {
Expand All @@ -209,6 +211,7 @@ export interface SlotOutletIRNode extends BaseIRNode {
name: SimpleExpressionNode
props: IRProps[]
fallback?: BlockIRNode
forwarded?: boolean
parent?: number
anchor?: number
}
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-vapor/src/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export class TransformContext<T extends AllNode = AllNode> {

inVOnce: boolean = false
inVFor: number = 0
inSlot: boolean = false

comment: CommentNode[] = []
component: Set<string> = this.ir.component
Expand Down Expand Up @@ -230,6 +231,7 @@ export function transform(
directive: new Set(),
block: newBlock(node),
hasTemplateRef: false,
hasForwardedSlot: false,
}

const context = new TransformContext(ir, node, options)
Expand Down
1 change: 1 addition & 0 deletions packages/compiler-vapor/src/transforms/transformElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function transformComponentElement(
root: singleRoot,
slots: [...context.slots],
once: context.inVOnce,
scopeId: context.inSlot ? context.options.scopeId : undefined,
dynamic: dynamicComponent,
}
context.slots = []
Expand Down
2 changes: 2 additions & 0 deletions packages/compiler-vapor/src/transforms/transformSlotOutlet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,15 @@ export const transformSlotOutlet: NodeTransform = (node, context) => {
}

return () => {
if (context.inSlot) context.ir.hasForwardedSlot = true
exitBlock && exitBlock()
context.dynamic.operation = {
type: IRNodeTypes.SLOT_OUTLET_NODE,
id,
name: slotName,
props: irProps,
fallback,
forwarded: context.inSlot,
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion packages/compiler-vapor/src/transforms/vSlot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,14 @@ function createSlotBlock(
const block: SlotBlockIRNode = newBlock(slotNode)
block.props = dir && dir.exp
const exitBlock = context.enterBlock(block)
return [block, exitBlock]
context.inSlot = true
return [
block,
() => {
context.inSlot = false
exitBlock()
},
]
}

function isNonWhitespaceContent(node: TemplateChildNode): boolean {
Expand Down
7 changes: 6 additions & 1 deletion packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,12 @@ export interface VaporInteropInterface {
move(vnode: VNode, container: any, anchor: any): void
slot(n1: VNode | null, n2: VNode, container: any, anchor: any): void

vdomMount: (component: ConcreteComponent, props?: any, slots?: any) => any
vdomMount: (
component: ConcreteComponent,
props?: any,
slots?: any,
scopeId?: string,
) => any
vdomUnmount: UnmountComponentFn
vdomSlot: (
slots: any,
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,11 @@ export { type VaporInteropInterface } from './apiCreateApp'
/**
* @internal
*/
export { type RendererInternals, MoveType } from './renderer'
export {
type RendererInternals,
MoveType,
getInheritedScopeIds,
} from './renderer'
/**
* @internal
*/
Expand Down
78 changes: 54 additions & 24 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,30 +764,9 @@ function baseCreateRenderer(
hostSetScopeId(el, slotScopeIds[i])
}
}
let subTree = parentComponent && parentComponent.subTree
if (subTree) {
if (
__DEV__ &&
subTree.patchFlag > 0 &&
subTree.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
) {
subTree =
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
}
if (
vnode === subTree ||
(isSuspense(subTree.type) &&
(subTree.ssContent === vnode || subTree.ssFallback === vnode))
) {
const parentVNode = parentComponent!.vnode!
setScopeId(
el,
parentVNode,
parentVNode.scopeId,
parentVNode.slotScopeIds,
parentComponent!.parent,
)
}
const inheritedScopeIds = getInheritedScopeIds(vnode, parentComponent)
for (let i = 0; i < inheritedScopeIds.length; i++) {
hostSetScopeId(el, inheritedScopeIds[i])
}
}

Expand Down Expand Up @@ -2656,3 +2635,54 @@ function getVaporInterface(
}
return res!
}

/**
* shared between vdom and vapor
*/
export function getInheritedScopeIds(
vnode: VNode,
parentComponent: GenericComponentInstance | null,
): string[] {
const inheritedScopeIds: string[] = []

let currentParent = parentComponent
let currentVNode = vnode

while (currentParent) {
let subTree = currentParent.subTree
if (!subTree) break

if (
__DEV__ &&
subTree.patchFlag > 0 &&
subTree.patchFlag & PatchFlags.DEV_ROOT_FRAGMENT
) {
subTree =
filterSingleRoot(subTree.children as VNodeArrayChildren) || subTree
}

if (
currentVNode === subTree ||
(isSuspense(subTree.type) &&
(subTree.ssContent === currentVNode ||
subTree.ssFallback === currentVNode))
) {
const parentVNode = currentParent.vnode!

if (parentVNode.scopeId) {
inheritedScopeIds.push(parentVNode.scopeId)
}

if (parentVNode.slotScopeIds) {
inheritedScopeIds.push(...parentVNode.slotScopeIds)
}

currentVNode = parentVNode
currentParent = currentParent.parent
} else {
break
}
}

return inheritedScopeIds
}
Loading