Skip to content

Commit

Permalink
wip(ssr): renderer support for optimized and manual slots
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 29, 2020
1 parent a7b0954 commit 6b1ce00
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/runtime-core/__tests__/rendererPortal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
TestElement,
TestNode
} from '@vue/runtime-test'
import { VNodeChildren } from '../src/vnode'
import { VNodeArrayChildren } from '../src/vnode'

describe('renderer: portal', () => {
test('should work', () => {
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('renderer: portal', () => {
test('should update children', async () => {
const target = nodeOps.createElement('div')
const root = nodeOps.createElement('div')
const children = ref<VNodeChildren<TestNode, TestElement>>([
const children = ref<VNodeArrayChildren<TestNode, TestElement>>([
h('div', 'teleported')
])

Expand Down
9 changes: 7 additions & 2 deletions packages/runtime-core/src/componentSlots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { ComponentInternalInstance, currentInstance } from './component'
import { VNode, NormalizedChildren, normalizeVNode, VNodeChild } from './vnode'
import {
VNode,
VNodeNormalizedChildren,
normalizeVNode,
VNodeChild
} from './vnode'
import { isArray, isFunction, EMPTY_OBJ } from '@vue/shared'
import { ShapeFlags } from './shapeFlags'
import { warn } from './warning'
Expand Down Expand Up @@ -41,7 +46,7 @@ const normalizeSlot = (key: string, rawSlot: Function): Slot => (

export function resolveSlots(
instance: ComponentInternalInstance,
children: NormalizedChildren
children: VNodeNormalizedChildren
) {
let slots: InternalSlots | void
if (instance.vnode.shapeFlag & ShapeFlags.SLOTS_CHILDREN) {
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/components/BaseTransition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
Comment,
isSameVNodeType,
VNode,
VNodeChildren
VNodeArrayChildren
} from '../vnode'
import { warn } from '../warning'
import { isKeepAlive } from './KeepAlive'
Expand Down Expand Up @@ -370,7 +370,7 @@ function emptyPlaceholder(vnode: VNode): VNode | undefined {
function getKeepAliveChild(vnode: VNode): VNode | undefined {
return isKeepAlive(vnode)
? vnode.children
? ((vnode.children as VNodeChildren)[0] as VNode)
? ((vnode.children as VNodeArrayChildren)[0] as VNode)
: undefined
: vnode
}
Expand Down
8 changes: 4 additions & 4 deletions packages/runtime-core/src/h.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
VNode,
VNodeProps,
createVNode,
VNodeChildren,
VNodeArrayChildren,
Fragment,
Portal,
isVNode
Expand Down Expand Up @@ -62,7 +62,7 @@ type RawChildren =
| number
| boolean
| VNode
| VNodeChildren
| VNodeArrayChildren
| (() => any)

// fake constructor type returned from `defineComponent`
Expand All @@ -85,11 +85,11 @@ export function h(
): VNode

// fragment
export function h(type: typeof Fragment, children?: VNodeChildren): VNode
export function h(type: typeof Fragment, children?: VNodeArrayChildren): VNode
export function h(
type: typeof Fragment,
props?: RawProps | null,
children?: VNodeChildren
children?: VNodeArrayChildren
): VNode

// portal (target prop is required)
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/helpers/renderSlot.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Data } from '../component'
import { Slot } from '../componentSlots'
import {
VNodeChildren,
VNodeArrayChildren,
openBlock,
createBlock,
Fragment,
Expand All @@ -15,7 +15,7 @@ export function renderSlot(
props: Data = {},
// this is not a user-facing function, so the fallback is always generated by
// the compiler and guaranteed to be an array
fallback?: VNodeChildren
fallback?: VNodeArrayChildren
): VNode {
const slot = slots[name]
return (
Expand Down
8 changes: 7 additions & 1 deletion packages/runtime-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ export {
Plugin,
CreateAppFunction
} from './apiCreateApp'
export { VNode, VNodeTypes, VNodeProps, VNodeChildren } from './vnode'
export {
VNode,
VNodeTypes,
VNodeProps,
VNodeArrayChildren,
VNodeNormalizedChildren
} from './vnode'
export {
Component,
FunctionalComponent,
Expand Down
4 changes: 2 additions & 2 deletions packages/runtime-core/src/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
cloneIfMounted,
normalizeVNode,
VNode,
VNodeChildren,
VNodeArrayChildren,
createVNode,
isSameVNodeType
} from './vnode'
Expand Down Expand Up @@ -177,7 +177,7 @@ export function createRenderer<
createApp: CreateAppFunction<HostElement>
} {
type HostVNode = VNode<HostNode, HostElement>
type HostVNodeChildren = VNodeChildren<HostNode, HostElement>
type HostVNodeChildren = VNodeArrayChildren<HostNode, HostElement>
type HostSuspenseBoundary = SuspenseBoundary<HostNode, HostElement>

const {
Expand Down
14 changes: 7 additions & 7 deletions packages/runtime-core/src/vnode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ type VNodeChildAtom<HostNode, HostElement> =
| null
| void

export interface VNodeChildren<HostNode = any, HostElement = any>
export interface VNodeArrayChildren<HostNode = any, HostElement = any>
extends Array<
| VNodeChildren<HostNode, HostElement>
| VNodeArrayChildren<HostNode, HostElement>
| VNodeChildAtom<HostNode, HostElement>
> {}

export type VNodeChild<HostNode = any, HostElement = any> =
| VNodeChildAtom<HostNode, HostElement>
| VNodeChildren<HostNode, HostElement>
| VNodeArrayChildren<HostNode, HostElement>

export type NormalizedChildren<HostNode = any, HostElement = any> =
export type VNodeNormalizedChildren<HostNode = any, HostElement = any> =
| string
| VNodeChildren<HostNode, HostElement>
| VNodeArrayChildren<HostNode, HostElement>
| RawSlots
| null

Expand All @@ -94,7 +94,7 @@ export interface VNode<HostNode = any, HostElement = any> {
key: string | number | null
ref: string | Ref | ((ref: object | null) => void) | null
scopeId: string | null // SFC only
children: NormalizedChildren<HostNode, HostElement>
children: VNodeNormalizedChildren<HostNode, HostElement>
component: ComponentInternalInstance | null
suspense: SuspenseBoundary<HostNode, HostElement> | null
dirs: DirectiveBinding[] | null
Expand Down Expand Up @@ -376,7 +376,7 @@ export function normalizeChildren(vnode: VNode, children: unknown) {
children = String(children)
type = ShapeFlags.TEXT_CHILDREN
}
vnode.children = children as NormalizedChildren
vnode.children = children as VNodeNormalizedChildren
vnode.shapeFlag |= type
}

Expand Down
68 changes: 53 additions & 15 deletions packages/server-renderer/src/renderToString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ import {
Component,
ComponentInternalInstance,
VNode,
VNodeChildren,
VNodeArrayChildren,
VNodeNormalizedChildren,
createVNode,
Text,
Comment,
Fragment,
Portal,
ShapeFlags,
ssrUtils
ssrUtils,
Slot
} from 'vue'
import {
isString,
isPromise,
isArray,
isFunction,
isVoidTag
isVoidTag,
EMPTY_OBJ
} from '@vue/shared'
import { renderProps } from './renderProps'
import { escape } from './escape'
Expand All @@ -39,6 +42,7 @@ type SSRBuffer = SSRBufferItem[]
type SSRBufferItem = string | ResolvedSSRBuffer | Promise<ResolvedSSRBuffer>
type ResolvedSSRBuffer = (string | ResolvedSSRBuffer)[]
type PushFn = (item: SSRBufferItem) => void
type Props = Record<string, unknown>

function createBuffer() {
let appendable = false
Expand Down Expand Up @@ -79,26 +83,36 @@ function unrollBuffer(buffer: ResolvedSSRBuffer): string {
}

export async function renderToString(app: App): Promise<string> {
const resolvedBuffer = await renderComponent(
createVNode(app._component, app._props)
)
const resolvedBuffer = await renderComponent(app._component, app._props, null)
return unrollBuffer(resolvedBuffer)
}

export function renderComponent(
comp: Component,
props: Props | null,
children: VNodeNormalizedChildren | null,
parentComponent: ComponentInternalInstance | null = null
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
return renderComponentVNode(
createVNode(comp, props, children),
parentComponent
)
}

function renderComponentVNode(
vnode: VNode,
parentComponent: ComponentInternalInstance | null = null
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
const instance = createComponentInstance(vnode, parentComponent)
const res = setupComponent(instance, null)
if (isPromise(res)) {
return res.then(() => innerRenderComponent(instance))
return res.then(() => renderComponentSubTree(instance))
} else {
return innerRenderComponent(instance)
return renderComponentSubTree(instance)
}
}

function innerRenderComponent(
function renderComponentSubTree(
instance: ComponentInternalInstance
): ResolvedSSRBuffer | Promise<ResolvedSSRBuffer> {
const comp = instance.type as Component
Expand Down Expand Up @@ -141,7 +155,7 @@ export function renderVNode(
break
case Fragment:
push(`<!---->`)
renderVNodeChildren(push, children as VNodeChildren, parentComponent)
renderVNodeChildren(push, children as VNodeArrayChildren, parentComponent)
push(`<!---->`)
break
case Portal:
Expand All @@ -151,7 +165,7 @@ export function renderVNode(
if (shapeFlag & ShapeFlags.ELEMENT) {
renderElement(push, vnode, parentComponent)
} else if (shapeFlag & ShapeFlags.COMPONENT) {
push(renderComponent(vnode, parentComponent))
push(renderComponentVNode(vnode, parentComponent))
} else if (shapeFlag & ShapeFlags.SUSPENSE) {
// TODO
} else {
Expand All @@ -166,7 +180,7 @@ export function renderVNode(

function renderVNodeChildren(
push: PushFn,
children: VNodeChildren,
children: VNodeArrayChildren,
parentComponent: ComponentInternalInstance | null = null
) {
for (let i = 0; i < children.length; i++) {
Expand Down Expand Up @@ -218,13 +232,37 @@ function renderElement(
if (shapeFlag & ShapeFlags.TEXT_CHILDREN) {
push(escape(children as string))
} else if (shapeFlag & ShapeFlags.ARRAY_CHILDREN) {
renderVNodeChildren(push, children as VNodeChildren, parentComponent)
renderVNodeChildren(
push,
children as VNodeArrayChildren,
parentComponent
)
}
}
push(`</${tag}>`)
}
}

export function renderSlot() {
// TODO
type OptimizedSlotFn = (
props: Props,
push: PushFn,
parentComponent: ComponentInternalInstance | null
) => void

export function renderSlot(
slotFn: Slot | OptimizedSlotFn,
slotProps: Props,
push: PushFn,
parentComponent: ComponentInternalInstance | null = null
) {
// template-compiled slots are always rendered as fragments
push(`<!---->`)
if (slotFn.length > 2) {
// only ssr-optimized slot fns accept 3 arguments
slotFn(slotProps, push, parentComponent)
} else {
// normal slot
renderVNodeChildren(push, (slotFn as Slot)(slotProps), parentComponent)
}
push(`<!---->`)
}

0 comments on commit 6b1ce00

Please sign in to comment.