2020-07-13 06:04:09 +08:00
|
|
|
import { compile } from '../src'
|
|
|
|
|
|
|
|
describe('ssr: inject <style vars>', () => {
|
|
|
|
test('basic', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<div/>`, {
|
|
|
|
ssrCssVars: `{ color }`
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
|
|
|
"const { mergeProps: _mergeProps } = require(\\"vue\\")
|
2020-11-18 07:54:47 +08:00
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
|
2020-07-13 06:04:09 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-11-18 07:54:47 +08:00
|
|
|
const _cssVars = { style: { color: _ctx.color }}
|
2020-07-13 06:04:09 +08:00
|
|
|
_push(\`<div\${_ssrRenderAttrs(_mergeProps(_attrs, _cssVars))}></div>\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('fragment', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<div/><div/>`, {
|
|
|
|
ssrCssVars: `{ color }`
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
2020-11-18 07:54:47 +08:00
|
|
|
"const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
|
2020-07-13 06:04:09 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-11-18 07:54:47 +08:00
|
|
|
const _cssVars = { style: { color: _ctx.color }}
|
2020-07-13 06:04:09 +08:00
|
|
|
_push(\`<!--[--><div\${
|
|
|
|
_ssrRenderAttrs(_cssVars)
|
|
|
|
}></div><div\${
|
|
|
|
_ssrRenderAttrs(_cssVars)
|
|
|
|
}></div><!--]-->\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('passing on to components', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<div/><foo/>`, {
|
|
|
|
ssrCssVars: `{ color }`
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
|
|
|
"const { resolveComponent: _resolveComponent } = require(\\"vue\\")
|
2020-11-18 07:54:47 +08:00
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs, ssrRenderComponent: _ssrRenderComponent } = require(\\"@vue/server-renderer\\")
|
2020-07-13 06:04:09 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
|
|
|
const _component_foo = _resolveComponent(\\"foo\\")
|
|
|
|
|
2020-11-18 07:54:47 +08:00
|
|
|
const _cssVars = { style: { color: _ctx.color }}
|
2020-07-13 06:04:09 +08:00
|
|
|
_push(\`<!--[--><div\${_ssrRenderAttrs(_cssVars)}></div>\`)
|
|
|
|
_push(_ssrRenderComponent(_component_foo, _cssVars, null, _parent))
|
|
|
|
_push(\`<!--]-->\`)
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('v-if branches', () => {
|
|
|
|
expect(
|
|
|
|
compile(`<div v-if="ok"/><template v-else><div/><div/></template>`, {
|
|
|
|
ssrCssVars: `{ color }`
|
|
|
|
}).code
|
|
|
|
).toMatchInlineSnapshot(`
|
|
|
|
"const { mergeProps: _mergeProps } = require(\\"vue\\")
|
2020-11-18 07:54:47 +08:00
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs } = require(\\"@vue/server-renderer\\")
|
2020-07-13 06:04:09 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-11-18 07:54:47 +08:00
|
|
|
const _cssVars = { style: { color: _ctx.color }}
|
2020-07-13 06:04:09 +08:00
|
|
|
if (_ctx.ok) {
|
|
|
|
_push(\`<div\${_ssrRenderAttrs(_mergeProps(_attrs, _cssVars))}></div>\`)
|
|
|
|
} else {
|
|
|
|
_push(\`<!--[--><div\${
|
|
|
|
_ssrRenderAttrs(_cssVars)
|
|
|
|
}></div><div\${
|
|
|
|
_ssrRenderAttrs(_cssVars)
|
|
|
|
}></div><!--]-->\`)
|
|
|
|
}
|
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
|
2020-11-18 07:54:47 +08:00
|
|
|
test('w/ suspense', () => {
|
2020-07-13 06:04:09 +08:00
|
|
|
expect(
|
2020-11-18 07:54:47 +08:00
|
|
|
compile(
|
|
|
|
`<Suspense>
|
|
|
|
<div>ok</div>
|
|
|
|
<template #fallback>
|
|
|
|
<div>fallback</div>
|
|
|
|
</template>
|
|
|
|
</Suspense>`,
|
|
|
|
{
|
|
|
|
ssrCssVars: `{ color }`
|
|
|
|
}
|
|
|
|
).code
|
2020-07-13 06:04:09 +08:00
|
|
|
).toMatchInlineSnapshot(`
|
2020-11-18 07:54:47 +08:00
|
|
|
"const { withCtx: _withCtx } = require(\\"vue\\")
|
|
|
|
const { ssrRenderAttrs: _ssrRenderAttrs, ssrRenderSuspense: _ssrRenderSuspense } = require(\\"@vue/server-renderer\\")
|
2020-07-13 06:04:09 +08:00
|
|
|
|
|
|
|
return function ssrRender(_ctx, _push, _parent, _attrs) {
|
2020-11-18 07:54:47 +08:00
|
|
|
const _cssVars = { style: { color: _ctx.color }}
|
|
|
|
_ssrRenderSuspense(_push, {
|
|
|
|
fallback: () => {
|
|
|
|
_push(\`<div\${_ssrRenderAttrs(_cssVars)}>fallback</div>\`)
|
|
|
|
},
|
|
|
|
default: () => {
|
|
|
|
_push(\`<div\${_ssrRenderAttrs(_cssVars)}>ok</div>\`)
|
|
|
|
},
|
2020-12-01 04:11:58 +08:00
|
|
|
_: 1 /* STABLE */
|
2020-11-18 07:54:47 +08:00
|
|
|
})
|
2020-07-13 06:04:09 +08:00
|
|
|
}"
|
|
|
|
`)
|
|
|
|
})
|
|
|
|
})
|