2020-02-06 12:07:23 +08:00
|
|
|
import { compile } from '../src'
|
|
|
|
|
|
|
|
describe('ssr: components', () => {
|
|
|
|
test('basic', () => {
|
|
|
|
expect(compile(`<foo id="a" :prop="b" />`).code).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-06 12:07:23 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-06 12:07:23 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, {
|
2020-02-06 12:07:23 +08:00
|
|
|
id: \\"a\\",
|
|
|
|
prop: _ctx.b
|
2020-02-07 07:03:14 +08:00
|
|
|
}, null, _parent))
|
2020-02-06 12:07:23 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('dynamic component', () => {
|
|
|
|
expect(compile(`<component is="foo" prop="b" />`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-06 12:07:23 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-06 12:07:23 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, { prop: \\"b\\" }, null, _parent))
|
2020-02-06 12:07:23 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
|
2020-02-10 22:20:07 +08:00
|
|
|
expect(compile(`<component :is="foo" prop="b" />`).code)
|
2020-02-06 12:07:23 +08:00
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-10 22:20:07 +08:00
|
|
|
"const { resolveDynamicComponent: _resolveDynamicComponent } = require(\\"vue\\")
|
2020-02-08 08:04:55 +08:00
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-10 22:20:07 +08:00
|
|
|
|
2020-02-06 12:07:23 +08:00
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-10 22:20:07 +08:00
|
|
|
_push(_ssrRenderComponent(_resolveDynamicComponent(_ctx.foo, _ctx.$), { prop: \\"b\\" }, null, _parent))
|
2020-02-06 12:07:23 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
describe('slots', () => {
|
|
|
|
test('implicit default slot', () => {
|
|
|
|
expect(compile(`<foo>hello<div/></foo>`).code).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, createVNode: _createVNode, createTextVNode: _createTextVNode } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, {
|
2020-02-07 06:45:34 +08:00
|
|
|
default: (_, _push, _parent, _scopeId) => {
|
2020-02-07 14:06:51 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`hello<div\${_scopeId}></div>\`)
|
2020-02-07 06:45:34 +08:00
|
|
|
} else {
|
2020-02-07 14:06:51 +08:00
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(\\"hello\\"),
|
|
|
|
_createVNode(\\"div\\")
|
2020-02-07 14:06:51 +08:00
|
|
|
]
|
2020-02-07 06:45:34 +08:00
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
},
|
2020-02-13 02:45:35 +08:00
|
|
|
_: 1
|
2020-02-07 07:03:14 +08:00
|
|
|
}, _parent))
|
2020-02-07 01:05:53 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('explicit default slot', () => {
|
|
|
|
expect(compile(`<foo v-slot="{ msg }">{{ msg + outer }}</foo>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, createTextVNode: _createTextVNode } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, {
|
2020-02-07 06:45:34 +08:00
|
|
|
default: ({ msg }, _push, _parent, _scopeId) => {
|
2020-02-07 14:06:51 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`\${_ssrInterpolate(msg + _ctx.outer)}\`)
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(_toDisplayString(msg + _ctx.outer))
|
2020-02-07 14:06:51 +08:00
|
|
|
]
|
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
},
|
2020-02-13 02:45:35 +08:00
|
|
|
_: 1
|
2020-02-07 07:03:14 +08:00
|
|
|
}, _parent))
|
2020-02-07 01:05:53 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('named slots', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<foo>
|
|
|
|
<template v-slot>foo</template>
|
|
|
|
<template v-slot:named>bar</template>
|
|
|
|
</foo>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, createTextVNode: _createTextVNode } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, {
|
2020-02-07 06:45:34 +08:00
|
|
|
default: (_, _push, _parent, _scopeId) => {
|
2020-02-07 14:06:51 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`foo\`)
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(\\"foo\\")
|
2020-02-07 14:06:51 +08:00
|
|
|
]
|
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
},
|
2020-02-07 06:45:34 +08:00
|
|
|
named: (_, _push, _parent, _scopeId) => {
|
2020-02-07 14:06:51 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`bar\`)
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(\\"bar\\")
|
2020-02-07 14:06:51 +08:00
|
|
|
]
|
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
},
|
2020-02-13 02:45:35 +08:00
|
|
|
_: 1
|
2020-02-07 07:03:14 +08:00
|
|
|
}, _parent))
|
2020-02-07 01:05:53 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-if slot', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<foo>
|
|
|
|
<template v-slot:named v-if="ok">foo</template>
|
|
|
|
</foo>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, createTextVNode: _createTextVNode, createSlots: _createSlots } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
2020-02-13 02:45:35 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, _createSlots({ _: 1 }, [
|
2020-02-07 01:05:53 +08:00
|
|
|
(_ctx.ok)
|
|
|
|
? {
|
|
|
|
name: \\"named\\",
|
2020-02-07 06:45:34 +08:00
|
|
|
fn: (_, _push, _parent, _scopeId) => {
|
2020-02-07 14:06:51 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`foo\`)
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(\\"foo\\")
|
2020-02-07 14:06:51 +08:00
|
|
|
]
|
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
: undefined
|
2020-02-07 07:03:14 +08:00
|
|
|
]), _parent))
|
2020-02-07 01:05:53 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-for slot', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<foo>
|
|
|
|
<template v-for="key in names" v-slot:[key]="{ msg }">{{ msg + key + bar }}</template>
|
|
|
|
</foo>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, createTextVNode: _createTextVNode, renderList: _renderList, createSlots: _createSlots } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent, ssrInterpolate: _ssrInterpolate } = require(\\"@vue/server-renderer\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 01:05:53 +08:00
|
|
|
|
2020-02-13 02:45:35 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, _createSlots({ _: 1 }, [
|
2020-02-08 08:04:55 +08:00
|
|
|
_renderList(_ctx.names, (key) => {
|
2020-02-07 01:05:53 +08:00
|
|
|
return {
|
|
|
|
name: key,
|
2020-02-07 06:45:34 +08:00
|
|
|
fn: ({ msg }, _push, _parent, _scopeId) => {
|
2020-02-08 02:56:18 +08:00
|
|
|
if (_push) {
|
|
|
|
_push(\`\${_ssrInterpolate(msg + key + _ctx.bar)}\`)
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-08 08:04:55 +08:00
|
|
|
_createTextVNode(_toDisplayString(msg + _ctx.key + _ctx.bar))
|
2020-02-08 02:56:18 +08:00
|
|
|
]
|
|
|
|
}
|
2020-02-07 01:05:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2020-02-07 07:03:14 +08:00
|
|
|
]), _parent))
|
2020-02-07 01:05:53 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
2020-02-07 04:29:02 +08:00
|
|
|
|
2020-02-08 02:56:18 +08:00
|
|
|
test('nested transform scoping in vnode branch', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<foo>
|
|
|
|
<template v-slot:foo="{ list }">
|
|
|
|
<div v-if="ok">
|
|
|
|
<span v-for="i in list"></span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<template v-slot:bar="{ ok }">
|
|
|
|
<div v-if="ok">
|
|
|
|
<span v-for="i in list"></span>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
</foo>`).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-02-12 07:40:21 +08:00
|
|
|
"const { resolveComponent: _resolveComponent, renderList: _renderList, Fragment: _Fragment, openBlock: _openBlock, createBlock: _createBlock, createVNode: _createVNode, createCommentVNode: _createCommentVNode } = require(\\"vue\\")
|
2020-02-08 08:04:55 +08:00
|
|
|
const { ssrRenderComponent: _ssrRenderComponent, ssrRenderList: _ssrRenderList } = require(\\"@vue/server-renderer\\")
|
2020-02-08 02:56:18 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-08 02:56:18 +08:00
|
|
|
|
|
|
|
_push(_ssrRenderComponent(_component_foo, null, {
|
|
|
|
foo: ({ list }, _push, _parent, _scopeId) => {
|
|
|
|
if (_push) {
|
|
|
|
if (_ctx.ok) {
|
|
|
|
_push(\`<div\${_scopeId}><!---->\`)
|
|
|
|
_ssrRenderList(list, (i) => {
|
|
|
|
_push(\`<span\${_scopeId}></span>\`)
|
|
|
|
})
|
|
|
|
_push(\`<!----></div>\`)
|
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-12 07:40:21 +08:00
|
|
|
(_ctx.ok)
|
|
|
|
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, [
|
2020-02-11 07:19:49 +08:00
|
|
|
(_openBlock(true), _createBlock(_Fragment, null, _renderList(list, (i) => {
|
2020-02-08 08:04:55 +08:00
|
|
|
return (_openBlock(), _createBlock(\\"span\\"))
|
2020-02-08 02:56:18 +08:00
|
|
|
}), 256 /* UNKEYED_FRAGMENT */))
|
2020-02-12 07:40:21 +08:00
|
|
|
]))
|
|
|
|
: _createCommentVNode(\\"v-if\\", true)
|
2020-02-08 02:56:18 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
bar: ({ ok }, _push, _parent, _scopeId) => {
|
|
|
|
if (_push) {
|
|
|
|
if (ok) {
|
|
|
|
_push(\`<div\${_scopeId}><!---->\`)
|
|
|
|
_ssrRenderList(_ctx.list, (i) => {
|
|
|
|
_push(\`<span\${_scopeId}></span>\`)
|
|
|
|
})
|
|
|
|
_push(\`<!----></div>\`)
|
|
|
|
} else {
|
|
|
|
_push(\`<!---->\`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return [
|
2020-02-12 07:40:21 +08:00
|
|
|
ok
|
|
|
|
? (_openBlock(), _createBlock(\\"div\\", { key: 0 }, [
|
2020-02-11 07:19:49 +08:00
|
|
|
(_openBlock(true), _createBlock(_Fragment, null, _renderList(_ctx.list, (i) => {
|
2020-02-08 08:04:55 +08:00
|
|
|
return (_openBlock(), _createBlock(\\"span\\"))
|
2020-02-08 02:56:18 +08:00
|
|
|
}), 256 /* UNKEYED_FRAGMENT */))
|
2020-02-12 07:40:21 +08:00
|
|
|
]))
|
|
|
|
: _createCommentVNode(\\"v-if\\", true)
|
2020-02-08 02:56:18 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2020-02-13 02:45:35 +08:00
|
|
|
_: 1
|
2020-02-08 02:56:18 +08:00
|
|
|
}, _parent))
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2020-02-07 04:29:02 +08:00
|
|
|
test('built-in fallthroughs', () => {
|
|
|
|
// no fragment
|
|
|
|
expect(compile(`<transition><div/></transition>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
|
|
|
_push(\`<div></div>\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
|
|
|
|
// wrap with fragment
|
|
|
|
expect(compile(`<transition-group><div/></transition-group>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
|
|
|
_push(\`<!----><div></div><!---->\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
|
|
|
|
// no fragment
|
|
|
|
expect(compile(`<keep-alive><foo/></keep-alive>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
2020-02-08 08:04:55 +08:00
|
|
|
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
|
|
|
const { ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-02-07 04:29:02 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
2020-02-08 08:04:55 +08:00
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
2020-02-07 04:29:02 +08:00
|
|
|
|
2020-02-07 07:03:14 +08:00
|
|
|
_push(_ssrRenderComponent(_component_foo, null, null, _parent))
|
2020-02-07 04:29:02 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
|
|
|
|
// wrap with fragment
|
|
|
|
expect(compile(`<suspense><div/></suspense>`).code)
|
|
|
|
.toMatchInlineSnapshot(`
|
|
|
|
"
|
|
|
|
return function ssrRender(_ctx, _push, _parent) {
|
|
|
|
_push(\`<!----><div></div><!---->\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
2020-02-07 01:05:53 +08:00
|
|
|
})
|
2020-02-06 12:07:23 +08:00
|
|
|
})
|