ssrScopeId.spec.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { compile } from '../src'
  2. const scopeId = 'data-v-xxxxxxx'
  3. describe('ssr: scopeId', () => {
  4. test('basic', () => {
  5. expect(
  6. compile(`<div><span>hello</span></div>`, {
  7. scopeId,
  8. mode: 'module',
  9. }).code,
  10. ).toMatchInlineSnapshot(`
  11. "import { ssrRenderAttrs as _ssrRenderAttrs } from "vue/server-renderer"
  12. export function ssrRender(_ctx, _push, _parent, _attrs) {
  13. _push(\`<div\${_ssrRenderAttrs(_attrs)} data-v-xxxxxxx><span data-v-xxxxxxx>hello</span></div>\`)
  14. }"
  15. `)
  16. })
  17. test('inside slots (only text)', () => {
  18. // should have no branching inside slot
  19. expect(
  20. compile(`<foo>foo</foo>`, {
  21. scopeId,
  22. mode: 'module',
  23. }).code,
  24. ).toMatchInlineSnapshot(`
  25. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createTextVNode as _createTextVNode } from "vue"
  26. import { ssrRenderComponent as _ssrRenderComponent } from "vue/server-renderer"
  27. export function ssrRender(_ctx, _push, _parent, _attrs) {
  28. const _component_foo = _resolveComponent("foo")
  29. _push(_ssrRenderComponent(_component_foo, _attrs, {
  30. default: _withCtx((_, _push, _parent, _scopeId) => {
  31. if (_push) {
  32. _push(\`foo\`)
  33. } else {
  34. return [
  35. _createTextVNode("foo")
  36. ]
  37. }
  38. }),
  39. _: 1 /* STABLE */
  40. }, _parent))
  41. }"
  42. `)
  43. })
  44. test('inside slots (with elements)', () => {
  45. expect(
  46. compile(`<foo><span>hello</span></foo>`, {
  47. scopeId,
  48. mode: 'module',
  49. }).code,
  50. ).toMatchInlineSnapshot(`
  51. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createVNode as _createVNode } from "vue"
  52. import { ssrRenderComponent as _ssrRenderComponent } from "vue/server-renderer"
  53. export function ssrRender(_ctx, _push, _parent, _attrs) {
  54. const _component_foo = _resolveComponent("foo")
  55. _push(_ssrRenderComponent(_component_foo, _attrs, {
  56. default: _withCtx((_, _push, _parent, _scopeId) => {
  57. if (_push) {
  58. _push(\`<span data-v-xxxxxxx\${_scopeId}>hello</span>\`)
  59. } else {
  60. return [
  61. _createVNode("span", null, "hello")
  62. ]
  63. }
  64. }),
  65. _: 1 /* STABLE */
  66. }, _parent))
  67. }"
  68. `)
  69. })
  70. test('nested slots', () => {
  71. expect(
  72. compile(`<foo><span>hello</span><bar><span/></bar></foo>`, {
  73. scopeId,
  74. mode: 'module',
  75. }).code,
  76. ).toMatchInlineSnapshot(`
  77. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createVNode as _createVNode } from "vue"
  78. import { ssrRenderComponent as _ssrRenderComponent } from "vue/server-renderer"
  79. export function ssrRender(_ctx, _push, _parent, _attrs) {
  80. const _component_foo = _resolveComponent("foo")
  81. const _component_bar = _resolveComponent("bar")
  82. _push(_ssrRenderComponent(_component_foo, _attrs, {
  83. default: _withCtx((_, _push, _parent, _scopeId) => {
  84. if (_push) {
  85. _push(\`<span data-v-xxxxxxx\${_scopeId}>hello</span>\`)
  86. _push(_ssrRenderComponent(_component_bar, null, {
  87. default: _withCtx((_, _push, _parent, _scopeId) => {
  88. if (_push) {
  89. _push(\`<span data-v-xxxxxxx\${_scopeId}></span>\`)
  90. } else {
  91. return [
  92. _createVNode("span")
  93. ]
  94. }
  95. }),
  96. _: 1 /* STABLE */
  97. }, _parent, _scopeId))
  98. } else {
  99. return [
  100. _createVNode("span", null, "hello"),
  101. _createVNode(_component_bar, null, {
  102. default: _withCtx(() => [
  103. _createVNode("span")
  104. ]),
  105. _: 1 /* STABLE */
  106. })
  107. ]
  108. }
  109. }),
  110. _: 1 /* STABLE */
  111. }, _parent))
  112. }"
  113. `)
  114. })
  115. // #7554
  116. test('scopeId is correctly transform to scope attribute of transition-group ', () => {
  117. expect(
  118. compile(
  119. `<transition-group tag="div" class="red"><span>hello</span></transition-group>`,
  120. {
  121. scopeId,
  122. mode: 'module',
  123. },
  124. ).code,
  125. ).toMatchInlineSnapshot(`
  126. "import { mergeProps as _mergeProps } from "vue"
  127. import { ssrRenderAttrs as _ssrRenderAttrs } from "vue/server-renderer"
  128. export function ssrRender(_ctx, _push, _parent, _attrs) {
  129. _push(\`<div\${_ssrRenderAttrs(_mergeProps({ class: "red" }, _attrs))} data-v-xxxxxxx><span data-v-xxxxxxx>hello</span></div>\`)
  130. }"
  131. `)
  132. // with dynamic tag
  133. expect(
  134. compile(
  135. `<transition-group :tag="someTag" class="red"><span>hello</span></transition-group>`,
  136. {
  137. scopeId,
  138. mode: 'module',
  139. },
  140. ).code,
  141. ).toMatchInlineSnapshot(`
  142. "import { mergeProps as _mergeProps } from "vue"
  143. import { ssrRenderAttrs as _ssrRenderAttrs } from "vue/server-renderer"
  144. export function ssrRender(_ctx, _push, _parent, _attrs) {
  145. _push(\`<\${
  146. _ctx.someTag
  147. }\${
  148. _ssrRenderAttrs(_mergeProps({ class: "red" }, _attrs))
  149. } data-v-xxxxxxx><span data-v-xxxxxxx>hello</span></\${
  150. _ctx.someTag
  151. }>\`)
  152. }"
  153. `)
  154. })
  155. })