index.d.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import { VNode } from 'vue'
  2. /*
  3. * Template compilation options / results
  4. */
  5. interface CompilerOptions {
  6. modules?: ModuleOptions[]
  7. directives?: Record<string, DirectiveFunction>
  8. preserveWhitespace?: boolean
  9. whitespace?: 'preserve' | 'condense'
  10. outputSourceRange?: any
  11. }
  12. interface CompilerOptionsWithSourceRange extends CompilerOptions {
  13. outputSourceRange: true
  14. }
  15. interface ErrorWithRange {
  16. msg: string
  17. start: number
  18. end: number
  19. }
  20. interface CompiledResult<ErrorType> {
  21. ast: ASTElement | undefined
  22. render: string
  23. staticRenderFns: string[]
  24. errors: ErrorType[]
  25. tips: ErrorType[]
  26. }
  27. interface CompiledResultFunctions {
  28. render: () => VNode
  29. staticRenderFns: (() => VNode)[]
  30. }
  31. interface ModuleOptions {
  32. preTransformNode: (el: ASTElement) => ASTElement | undefined
  33. transformNode: (el: ASTElement) => ASTElement | undefined
  34. postTransformNode: (el: ASTElement) => void
  35. genData: (el: ASTElement) => string
  36. transformCode?: (el: ASTElement, code: string) => string
  37. staticKeys?: string[]
  38. }
  39. type DirectiveFunction = (node: ASTElement, directiveMeta: ASTDirective) => void
  40. /*
  41. * AST Types
  42. */
  43. /**
  44. * - 0: FALSE - whole sub tree un-optimizable
  45. * - 1: FULL - whole sub tree optimizable
  46. * - 2: SELF - self optimizable but has some un-optimizable children
  47. * - 3: CHILDREN - self un-optimizable but have fully optimizable children
  48. * - 4: PARTIAL - self un-optimizable with some un-optimizable children
  49. */
  50. export type SSROptimizability = 0 | 1 | 2 | 3 | 4
  51. export interface ASTModifiers {
  52. [key: string]: boolean
  53. }
  54. export interface ASTIfCondition {
  55. exp: string | undefined
  56. block: ASTElement
  57. }
  58. export interface ASTElementHandler {
  59. value: string
  60. params?: any[]
  61. modifiers: ASTModifiers | undefined
  62. }
  63. export interface ASTElementHandlers {
  64. [key: string]: ASTElementHandler | ASTElementHandler[]
  65. }
  66. export interface ASTDirective {
  67. name: string
  68. rawName: string
  69. value: string
  70. arg: string | undefined
  71. modifiers: ASTModifiers | undefined
  72. }
  73. export type ASTNode = ASTElement | ASTText | ASTExpression
  74. export interface ASTElement {
  75. type: 1
  76. tag: string
  77. attrsList: { name: string; value: any }[]
  78. attrsMap: Record<string, any>
  79. parent: ASTElement | undefined
  80. children: ASTNode[]
  81. processed?: true
  82. static?: boolean
  83. staticRoot?: boolean
  84. staticInFor?: boolean
  85. staticProcessed?: boolean
  86. hasBindings?: boolean
  87. text?: string
  88. attrs?: { name: string; value: any }[]
  89. props?: { name: string; value: string }[]
  90. plain?: boolean
  91. pre?: true
  92. ns?: string
  93. component?: string
  94. inlineTemplate?: true
  95. transitionMode?: string | null
  96. slotName?: string
  97. slotTarget?: string
  98. slotScope?: string
  99. scopedSlots?: Record<string, ASTElement>
  100. ref?: string
  101. refInFor?: boolean
  102. if?: string
  103. ifProcessed?: boolean
  104. elseif?: string
  105. else?: true
  106. ifConditions?: ASTIfCondition[]
  107. for?: string
  108. forProcessed?: boolean
  109. key?: string
  110. alias?: string
  111. iterator1?: string
  112. iterator2?: string
  113. staticClass?: string
  114. classBinding?: string
  115. staticStyle?: string
  116. styleBinding?: string
  117. events?: ASTElementHandlers
  118. nativeEvents?: ASTElementHandlers
  119. transition?: string | true
  120. transitionOnAppear?: boolean
  121. model?: {
  122. value: string
  123. callback: string
  124. expression: string
  125. }
  126. directives?: ASTDirective[]
  127. forbidden?: true
  128. once?: true
  129. onceProcessed?: boolean
  130. wrapData?: (code: string) => string
  131. wrapListeners?: (code: string) => string
  132. // 2.4 ssr optimization
  133. ssrOptimizability?: SSROptimizability
  134. }
  135. export interface ASTExpression {
  136. type: 2
  137. expression: string
  138. text: string
  139. tokens: (string | Record<string, any>)[]
  140. static?: boolean
  141. // 2.4 ssr optimization
  142. ssrOptimizability?: SSROptimizability
  143. }
  144. export interface ASTText {
  145. type: 3
  146. text: string
  147. static?: boolean
  148. isComment?: boolean
  149. // 2.4 ssr optimization
  150. ssrOptimizability?: SSROptimizability
  151. }
  152. /*
  153. * SFC parser related types
  154. */
  155. interface SFCParserOptions {
  156. pad?: true | 'line' | 'space'
  157. deindent?: boolean
  158. }
  159. export interface SFCBlock {
  160. type: string
  161. content: string
  162. attrs: Record<string, string>
  163. start?: number
  164. end?: number
  165. lang?: string
  166. src?: string
  167. scoped?: boolean
  168. module?: string | boolean
  169. }
  170. export interface SFCDescriptor {
  171. template: SFCBlock | undefined
  172. script: SFCBlock | undefined
  173. styles: SFCBlock[]
  174. customBlocks: SFCBlock[]
  175. }
  176. /*
  177. * Exposed functions
  178. */
  179. export function compile(
  180. template: string,
  181. options: CompilerOptionsWithSourceRange
  182. ): CompiledResult<ErrorWithRange>
  183. export function compile(
  184. template: string,
  185. options?: CompilerOptions
  186. ): CompiledResult<string>
  187. export function compileToFunctions(template: string): CompiledResultFunctions
  188. export function ssrCompile(
  189. template: string,
  190. options: CompilerOptionsWithSourceRange
  191. ): CompiledResult<ErrorWithRange>
  192. export function ssrCompile(
  193. template: string,
  194. options?: CompilerOptions
  195. ): CompiledResult<string>
  196. export function ssrCompileToFunctions(template: string): CompiledResultFunctions
  197. export function parseComponent(
  198. file: string,
  199. options?: SFCParserOptions
  200. ): SFCDescriptor
  201. export function generateCodeFrame(
  202. template: string,
  203. start: number,
  204. end: number
  205. ): string