feat(ssr): support custom directive getSSRProps in optimized compilation

close #5304
This commit is contained in:
Evan You
2022-02-04 08:58:28 +08:00
parent a51f935b72
commit 60cf175d88
14 changed files with 228 additions and 45 deletions

View File

@@ -10,8 +10,10 @@ import {
vShow,
vModelText,
vModelRadio,
vModelCheckbox
vModelCheckbox,
resolveDirective
} from 'vue'
import { ssrGetDirectiveProps, ssrRenderAttrs } from '../src'
describe('ssr: directives', () => {
describe('template v-show', () => {
@@ -374,7 +376,7 @@ describe('ssr: directives', () => {
})
})
test('custom directive w/ getSSRProps', async () => {
test('custom directive w/ getSSRProps (vdom)', async () => {
expect(
await renderToString(
createApp({
@@ -394,4 +396,35 @@ describe('ssr: directives', () => {
)
).toBe(`<div id="foo"></div>`)
})
test('custom directive w/ getSSRProps (optimized)', async () => {
expect(
await renderToString(
createApp({
data() {
return {
x: 'foo'
}
},
directives: {
xxx: {
getSSRProps({ value, arg, modifiers }) {
return { id: [value, arg, modifiers.ok].join('-') }
}
}
},
ssrRender(_ctx, _push, _parent, _attrs) {
const _directive_xxx = resolveDirective('xxx')!
_push(
`<div${ssrRenderAttrs(
ssrGetDirectiveProps(_ctx, _directive_xxx, _ctx.x, 'arg', {
ok: true
})
)}></div>`
)
}
})
)
).toBe(`<div id="foo-arg-true"></div>`)
})
})

View File

@@ -0,0 +1,26 @@
import { ComponentPublicInstance, Directive } from '@vue/runtime-core'
export function ssrGetDirectiveProps(
instance: ComponentPublicInstance,
dir: Directive,
value?: any,
arg?: string,
modifiers: Record<string, boolean> = {}
): Record<string, any> {
if (typeof dir !== 'function' && dir.getSSRProps) {
return (
dir.getSSRProps(
{
dir,
instance,
value,
oldValue: undefined,
arg,
modifiers
},
null as any
) || {}
)
}
return {}
}

View File

@@ -30,6 +30,7 @@ export {
export { ssrInterpolate } from './helpers/ssrInterpolate'
export { ssrRenderList } from './helpers/ssrRenderList'
export { ssrRenderSuspense } from './helpers/ssrRenderSuspense'
export { ssrGetDirectiveProps } from './helpers/ssrGetDirectiveProps'
export { includeBooleanAttr as ssrIncludeBooleanAttr } from '@vue/shared'
// v-model helpers