index.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import {
  2. type CodegenResult,
  3. type CompilerOptions,
  4. type RootNode,
  5. baseParse,
  6. generate,
  7. noopDirectiveTransform,
  8. parserOptions,
  9. trackSlotScopes,
  10. trackVForSlotScopes,
  11. transform,
  12. transformBind,
  13. transformExpression,
  14. transformOn,
  15. transformStyle,
  16. transformVBindShorthand,
  17. } from '@vue/compiler-dom'
  18. import { ssrCodegenTransform } from './ssrCodegenTransform'
  19. import { ssrTransformElement } from './transforms/ssrTransformElement'
  20. import {
  21. rawOptionsMap,
  22. ssrTransformComponent,
  23. } from './transforms/ssrTransformComponent'
  24. import { ssrTransformSlotOutlet } from './transforms/ssrTransformSlotOutlet'
  25. import { ssrTransformIf } from './transforms/ssrVIf'
  26. import { ssrTransformFor } from './transforms/ssrVFor'
  27. import { ssrTransformModel } from './transforms/ssrVModel'
  28. import { ssrTransformShow } from './transforms/ssrVShow'
  29. import { ssrInjectFallthroughAttrs } from './transforms/ssrInjectFallthroughAttrs'
  30. import { ssrInjectCssVars } from './transforms/ssrInjectCssVars'
  31. export function compile(
  32. source: string | RootNode,
  33. options: CompilerOptions = {},
  34. ): CodegenResult {
  35. options = {
  36. ...options,
  37. ...parserOptions,
  38. ssr: true,
  39. inSSR: true,
  40. scopeId: options.mode === 'function' ? null : options.scopeId,
  41. // always prefix since compiler-ssr doesn't have size concern
  42. prefixIdentifiers: true,
  43. // disable optimizations that are unnecessary for ssr
  44. cacheHandlers: false,
  45. hoistStatic: false,
  46. }
  47. const ast = typeof source === 'string' ? baseParse(source, options) : source
  48. // Save raw options for AST. This is needed when performing sub-transforms
  49. // on slot vnode branches.
  50. rawOptionsMap.set(ast, options)
  51. transform(ast, {
  52. ...options,
  53. hoistStatic: false,
  54. nodeTransforms: [
  55. transformVBindShorthand,
  56. ssrTransformIf,
  57. ssrTransformFor,
  58. trackVForSlotScopes,
  59. transformExpression,
  60. ssrTransformSlotOutlet,
  61. ssrInjectFallthroughAttrs,
  62. ssrInjectCssVars,
  63. ssrTransformElement,
  64. ssrTransformComponent,
  65. trackSlotScopes,
  66. transformStyle,
  67. ...(options.nodeTransforms || []), // user transforms
  68. ],
  69. directiveTransforms: {
  70. // reusing core v-bind
  71. bind: transformBind,
  72. on: transformOn,
  73. // model and show have dedicated SSR handling
  74. model: ssrTransformModel,
  75. show: ssrTransformShow,
  76. // the following are ignored during SSR
  77. // on: noopDirectiveTransform,
  78. cloak: noopDirectiveTransform,
  79. once: noopDirectiveTransform,
  80. memo: noopDirectiveTransform,
  81. ...(options.directiveTransforms || {}), // user transforms
  82. },
  83. })
  84. // traverse the template AST and convert into SSR codegen AST
  85. // by replacing ast.codegenNode.
  86. ssrCodegenTransform(ast, options)
  87. return generate(ast, options)
  88. }