feat(ssr): support getSSRProps for vnode directives

This commit is contained in:
Evan You
2020-03-16 18:36:19 -04:00
parent a46f3b354d
commit c450ede12d
5 changed files with 458 additions and 6 deletions

View File

@@ -12,7 +12,10 @@ import {
Slots,
createApp,
ssrContextKey,
warn
warn,
DirectiveBinding,
VNodeProps,
mergeProps
} from 'vue'
import {
ShapeFlags,
@@ -289,10 +292,12 @@ function renderElementVNode(
parentComponent: ComponentInternalInstance
) {
const tag = vnode.type as string
const { props, children, shapeFlag, scopeId } = vnode
let { props, children, shapeFlag, scopeId, dirs } = vnode
let openTag = `<${tag}`
// TODO directives
if (dirs !== null) {
props = applySSRDirectives(vnode, props, dirs)
}
if (props !== null) {
openTag += ssrRenderAttrs(props, tag)
@@ -338,6 +343,25 @@ function renderElementVNode(
}
}
function applySSRDirectives(
vnode: VNode,
rawProps: VNodeProps | null,
dirs: DirectiveBinding[]
): VNodeProps {
const toMerge: VNodeProps[] = []
for (let i = 0; i < dirs.length; i++) {
const binding = dirs[i]
const {
dir: { getSSRProps }
} = binding
if (getSSRProps) {
const props = getSSRProps(binding, vnode)
if (props) toMerge.push(props)
}
}
return mergeProps(rawProps || {}, ...toMerge)
}
function renderPortalVNode(
vnode: VNode,
parentComponent: ComponentInternalInstance