ssrVFor.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import {
  2. createStructuralDirectiveTransform,
  3. ForNode,
  4. processFor,
  5. createCallExpression,
  6. createFunctionExpression,
  7. createForLoopParams,
  8. NodeTypes
  9. } from '@vue/compiler-dom'
  10. import {
  11. SSRTransformContext,
  12. processChildrenAsStatement
  13. } from '../ssrCodegenTransform'
  14. import { SSR_RENDER_LIST } from '../runtimeHelpers'
  15. // Plugin for the first transform pass, which simply constructs the AST node
  16. export const ssrTransformFor = createStructuralDirectiveTransform(
  17. 'for',
  18. processFor
  19. )
  20. // This is called during the 2nd transform pass to construct the SSR-specific
  21. // codegen nodes.
  22. export function ssrProcessFor(node: ForNode, context: SSRTransformContext) {
  23. const needFragmentWrapper =
  24. node.children.length !== 1 || node.children[0].type !== NodeTypes.ELEMENT
  25. const renderLoop = createFunctionExpression(
  26. createForLoopParams(node.parseResult)
  27. )
  28. renderLoop.body = processChildrenAsStatement(
  29. node.children,
  30. context,
  31. needFragmentWrapper
  32. )
  33. // v-for always renders a fragment
  34. context.pushStringPart(`<!--[-->`)
  35. context.pushStatement(
  36. createCallExpression(context.helper(SSR_RENDER_LIST), [
  37. node.source,
  38. renderLoop
  39. ])
  40. )
  41. context.pushStringPart(`<!--]-->`)
  42. }