921c6d3bad8bec7cb2ff97221d53fc285e89e9aebe16abbe5689431ff6ae1bcc7c027227bfe2567f3dc6101d6c6e5951f4231675d29e1fe7536580ca00290c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. import Vue, { 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. // weex specific
  135. appendAsTree?: boolean;
  136. }
  137. export interface ASTExpression {
  138. type: 2;
  139. expression: string;
  140. text: string;
  141. tokens: (string | Record<string, any>)[];
  142. static?: boolean;
  143. // 2.4 ssr optimization
  144. ssrOptimizability?: SSROptimizability;
  145. }
  146. export interface ASTText {
  147. type: 3;
  148. text: string;
  149. static?: boolean;
  150. isComment?: boolean;
  151. // 2.4 ssr optimization
  152. ssrOptimizability?: SSROptimizability;
  153. }
  154. /*
  155. * SFC parser related types
  156. */
  157. interface SFCParserOptions {
  158. pad?: true | 'line' | 'space';
  159. deindent?: boolean
  160. }
  161. export interface SFCBlock {
  162. type: string;
  163. content: string;
  164. attrs: Record<string, string>;
  165. start?: number;
  166. end?: number;
  167. lang?: string;
  168. src?: string;
  169. scoped?: boolean;
  170. module?: string | boolean;
  171. }
  172. export interface SFCDescriptor {
  173. template: SFCBlock | undefined;
  174. script: SFCBlock | undefined;
  175. styles: SFCBlock[];
  176. customBlocks: SFCBlock[];
  177. }
  178. /*
  179. * Exposed functions
  180. */
  181. export function compile(
  182. template: string,
  183. options: CompilerOptionsWithSourceRange
  184. ): CompiledResult<ErrorWithRange>
  185. export function compile(
  186. template: string,
  187. options?: CompilerOptions
  188. ): CompiledResult<string>;
  189. export function compileToFunctions(template: string): CompiledResultFunctions;
  190. export function ssrCompile(
  191. template: string,
  192. options: CompilerOptionsWithSourceRange
  193. ): CompiledResult<ErrorWithRange>;
  194. export function ssrCompile(
  195. template: string,
  196. options?: CompilerOptions
  197. ): CompiledResult<string>;
  198. export function ssrCompileToFunctions(template: string): CompiledResultFunctions;
  199. export function parseComponent(
  200. file: string,
  201. options?: SFCParserOptions
  202. ): SFCDescriptor;
  203. export function generateCodeFrame(
  204. template: string,
  205. start: number,
  206. end: number
  207. ): string;