index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { parse, ParserOptions } from './parse'
  2. import { transform, TransformOptions } from './transform'
  3. import { generate, CodegenOptions, CodegenResult } from './codegen'
  4. import { RootNode } from './ast'
  5. import { isString } from '@vue/shared'
  6. import { transformIf } from './transforms/vIf'
  7. import { transformFor } from './transforms/vFor'
  8. import { transformExpression } from './transforms/transformExpression'
  9. import { transformSlotOutlet } from './transforms/transformSlotOutlet'
  10. import { transformElement } from './transforms/transformElement'
  11. import { transformOn } from './transforms/vOn'
  12. import { transformBind } from './transforms/vBind'
  13. import { defaultOnError, createCompilerError, ErrorCodes } from './errors'
  14. import { trackSlotScopes, trackVForSlotScopes } from './transforms/vSlot'
  15. import { optimizeText } from './transforms/optimizeText'
  16. export type CompilerOptions = ParserOptions & TransformOptions & CodegenOptions
  17. // we name it `baseCompile` so that higher order compilers like @vue/compiler-dom
  18. // can export `compile` while re-exporting everything else.
  19. export function baseCompile(
  20. template: string | RootNode,
  21. options: CompilerOptions = {}
  22. ): CodegenResult {
  23. /* istanbul ignore if */
  24. if (__BROWSER__) {
  25. const onError = options.onError || defaultOnError
  26. if (options.prefixIdentifiers === true) {
  27. onError(createCompilerError(ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED))
  28. } else if (options.mode === 'module') {
  29. onError(createCompilerError(ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED))
  30. }
  31. }
  32. const ast = isString(template) ? parse(template, options) : template
  33. const prefixIdentifiers =
  34. !__BROWSER__ &&
  35. (options.prefixIdentifiers === true || options.mode === 'module')
  36. transform(ast, {
  37. ...options,
  38. prefixIdentifiers,
  39. nodeTransforms: [
  40. transformIf,
  41. transformFor,
  42. ...(prefixIdentifiers
  43. ? [
  44. // order is important
  45. trackVForSlotScopes,
  46. transformExpression
  47. ]
  48. : []),
  49. trackSlotScopes,
  50. optimizeText,
  51. transformSlotOutlet,
  52. transformElement,
  53. ...(options.nodeTransforms || []) // user transforms
  54. ],
  55. directiveTransforms: {
  56. on: transformOn,
  57. bind: transformBind,
  58. ...(options.directiveTransforms || {}) // user transforms
  59. }
  60. })
  61. return generate(ast, {
  62. ...options,
  63. prefixIdentifiers
  64. })
  65. }
  66. // Also expose lower level APIs & types
  67. export { parse, ParserOptions, TextModes } from './parse'
  68. export {
  69. transform,
  70. createStructuralDirectiveTransform,
  71. TransformOptions,
  72. TransformContext,
  73. NodeTransform,
  74. StructuralDirectiveTransform
  75. } from './transform'
  76. export {
  77. generate,
  78. CodegenOptions,
  79. CodegenContext,
  80. CodegenResult
  81. } from './codegen'
  82. export { ErrorCodes, CompilerError, createCompilerError } from './errors'
  83. export * from './ast'