compile.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import { CompilerOptions } from './options'
  2. import { baseParse } from './parse'
  3. import { transform, NodeTransform, DirectiveTransform } from './transform'
  4. import { generate, CodegenResult } from './codegen'
  5. import { RootNode } from './ast'
  6. import { isString, extend } from '@vue/shared'
  7. import { transformIf } from './transforms/vIf'
  8. import { transformFor } from './transforms/vFor'
  9. import { transformExpression } from './transforms/transformExpression'
  10. import { transformSlotOutlet } from './transforms/transformSlotOutlet'
  11. import { transformElement } from './transforms/transformElement'
  12. import { transformOn } from './transforms/vOn'
  13. import { transformBind } from './transforms/vBind'
  14. import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
  15. import { transformText } from './transforms/transformText'
  16. import { transformOnce } from './transforms/vOnce'
  17. import { transformModel } from './transforms/vModel'
  18. import { transformFilter } from './compat/transformFilter'
  19. import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
  20. import { transformMemo } from './transforms/vMemo'
  21. export type TransformPreset = [
  22. NodeTransform[],
  23. Record<string, DirectiveTransform>
  24. ]
  25. export function getBaseTransformPreset(
  26. prefixIdentifiers?: boolean
  27. ): TransformPreset {
  28. return [
  29. [
  30. transformOnce,
  31. transformIf,
  32. transformMemo,
  33. transformFor,
  34. ...(__COMPAT__ ? [transformFilter] : []),
  35. ...(!__BROWSER__ && prefixIdentifiers
  36. ? [
  37. // order is important
  38. trackVForSlotScopes,
  39. transformExpression
  40. ]
  41. : __BROWSER__ && __DEV__
  42. ? [transformExpression]
  43. : []),
  44. transformSlotOutlet,
  45. transformElement,
  46. trackSlotScopes,
  47. transformText
  48. ],
  49. {
  50. on: transformOn,
  51. bind: transformBind,
  52. model: transformModel
  53. }
  54. ]
  55. }
  56. // we name it `baseCompile` so that higher order compilers like
  57. // @vue/compiler-dom can export `compile` while re-exporting everything else.
  58. export function baseCompile(
  59. template: string | RootNode,
  60. options: CompilerOptions = {}
  61. ): CodegenResult {
  62. const onError = options.onError || defaultOnError
  63. const isModuleMode = options.mode === 'module'
  64. /* istanbul ignore if */
  65. if (__BROWSER__) {
  66. if (options.prefixIdentifiers === true) {
  67. onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
  68. } else if (isModuleMode) {
  69. onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
  70. }
  71. }
  72. const prefixIdentifiers =
  73. !__BROWSER__ && (options.prefixIdentifiers === true || isModuleMode)
  74. if (!prefixIdentifiers && options.cacheHandlers) {
  75. onError(createCompilerError(ErrorCodes.X_CACHE_HANDLER_NOT_SUPPORTED))
  76. }
  77. if (options.scopeId && !isModuleMode) {
  78. onError(createCompilerError(ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED))
  79. }
  80. const ast = isString(template) ? baseParse(template, options) : template
  81. const [nodeTransforms, directiveTransforms] =
  82. getBaseTransformPreset(prefixIdentifiers)
  83. if (!__BROWSER__ && options.isTS) {
  84. const { expressionPlugins } = options
  85. if (!expressionPlugins || !expressionPlugins.includes('typescript')) {
  86. options.expressionPlugins = [...(expressionPlugins || []), 'typescript']
  87. }
  88. }
  89. transform(
  90. ast,
  91. extend({}, options, {
  92. prefixIdentifiers,
  93. nodeTransforms: [
  94. ...nodeTransforms,
  95. ...(options.nodeTransforms || []) // user transforms
  96. ],
  97. directiveTransforms: extend(
  98. {},
  99. directiveTransforms,
  100. options.directiveTransforms || {} // user transforms
  101. )
  102. })
  103. )
  104. return generate(
  105. ast,
  106. extend({}, options, {
  107. prefixIdentifiers
  108. })
  109. )
  110. }