ssrScopeId.spec.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 { withScopeId as _withScopeId } from \\"vue\\"
  12. import { ssrRenderAttrs as _ssrRenderAttrs } from \\"@vue/server-renderer\\"
  13. const _withId = /*#__PURE__*/_withScopeId(\\"data-v-xxxxxxx\\")
  14. export const ssrRender = /*#__PURE__*/_withId(function ssrRender(_ctx, _push, _parent, _attrs) {
  15. _push(\`<div\${_ssrRenderAttrs(_attrs)} data-v-xxxxxxx><span data-v-xxxxxxx>hello</span></div>\`)
  16. })"
  17. `)
  18. })
  19. test('inside slots (only text)', () => {
  20. // should have no branching inside slot
  21. expect(
  22. compile(`<foo>foo</foo>`, {
  23. scopeId,
  24. mode: 'module'
  25. }).code
  26. ).toMatchInlineSnapshot(`
  27. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createTextVNode as _createTextVNode, withScopeId as _withScopeId } from \\"vue\\"
  28. import { ssrRenderComponent as _ssrRenderComponent } from \\"@vue/server-renderer\\"
  29. const _withId = /*#__PURE__*/_withScopeId(\\"data-v-xxxxxxx\\")
  30. export const ssrRender = /*#__PURE__*/_withId(function ssrRender(_ctx, _push, _parent, _attrs) {
  31. const _component_foo = _resolveComponent(\\"foo\\")
  32. _push(_ssrRenderComponent(_component_foo, _attrs, {
  33. default: _withId((_, _push, _parent, _scopeId) => {
  34. if (_push) {
  35. _push(\`foo\`)
  36. } else {
  37. return [
  38. _createTextVNode(\\"foo\\")
  39. ]
  40. }
  41. }),
  42. _: 1
  43. }, _parent))
  44. })"
  45. `)
  46. })
  47. test('inside slots (with elements)', () => {
  48. expect(
  49. compile(`<foo><span>hello</span></foo>`, {
  50. scopeId,
  51. mode: 'module'
  52. }).code
  53. ).toMatchInlineSnapshot(`
  54. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createVNode as _createVNode, withScopeId as _withScopeId } from \\"vue\\"
  55. import { ssrRenderComponent as _ssrRenderComponent } from \\"@vue/server-renderer\\"
  56. const _withId = /*#__PURE__*/_withScopeId(\\"data-v-xxxxxxx\\")
  57. export const ssrRender = /*#__PURE__*/_withId(function ssrRender(_ctx, _push, _parent, _attrs) {
  58. const _component_foo = _resolveComponent(\\"foo\\")
  59. _push(_ssrRenderComponent(_component_foo, _attrs, {
  60. default: _withId((_, _push, _parent, _scopeId) => {
  61. if (_push) {
  62. _push(\`<span data-v-xxxxxxx\${_scopeId}>hello</span>\`)
  63. } else {
  64. return [
  65. _createVNode(\\"span\\", null, \\"hello\\")
  66. ]
  67. }
  68. }),
  69. _: 1
  70. }, _parent))
  71. })"
  72. `)
  73. })
  74. test('nested slots', () => {
  75. expect(
  76. compile(`<foo><span>hello</span><bar><span/></bar></foo>`, {
  77. scopeId,
  78. mode: 'module'
  79. }).code
  80. ).toMatchInlineSnapshot(`
  81. "import { resolveComponent as _resolveComponent, withCtx as _withCtx, createVNode as _createVNode, withScopeId as _withScopeId } from \\"vue\\"
  82. import { ssrRenderComponent as _ssrRenderComponent } from \\"@vue/server-renderer\\"
  83. const _withId = /*#__PURE__*/_withScopeId(\\"data-v-xxxxxxx\\")
  84. export const ssrRender = /*#__PURE__*/_withId(function ssrRender(_ctx, _push, _parent, _attrs) {
  85. const _component_foo = _resolveComponent(\\"foo\\")
  86. const _component_bar = _resolveComponent(\\"bar\\")
  87. _push(_ssrRenderComponent(_component_foo, _attrs, {
  88. default: _withId((_, _push, _parent, _scopeId) => {
  89. if (_push) {
  90. _push(\`<span data-v-xxxxxxx\${_scopeId}>hello</span>\`)
  91. _push(_ssrRenderComponent(_component_bar, null, {
  92. default: _withId((_, _push, _parent, _scopeId) => {
  93. if (_push) {
  94. _push(\`<span data-v-xxxxxxx\${_scopeId}></span>\`)
  95. } else {
  96. return [
  97. _createVNode(\\"span\\")
  98. ]
  99. }
  100. }),
  101. _: 1
  102. }, _parent))
  103. } else {
  104. return [
  105. _createVNode(\\"span\\", null, \\"hello\\"),
  106. _createVNode(_component_bar, null, {
  107. default: _withId(() => [
  108. _createVNode(\\"span\\")
  109. ]),
  110. _: 1
  111. })
  112. ]
  113. }
  114. }),
  115. _: 1
  116. }, _parent))
  117. })"
  118. `)
  119. })
  120. })