compiler.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. declare type CompilerOptions = {
  2. warn?: Function; // allow customizing warning in different environments; e.g. node
  3. modules?: Array<ModuleOptions>; // platform specific modules; e.g. style; class
  4. directives?: { [key: string]: Function }; // platform specific directives
  5. staticKeys?: string; // a list of AST properties to be considered static; for optimization
  6. isUnaryTag?: (tag: string) => ?boolean; // check if a tag is unary for the platform
  7. canBeLeftOpenTag?: (tag: string) => ?boolean; // check if a tag can be left opened
  8. isReservedTag?: (tag: string) => ?boolean; // check if a tag is a native for the platform
  9. preserveWhitespace?: boolean; // preserve whitespace between elements? (Deprecated)
  10. whitespace?: 'preserve' | 'condense'; // whitespace handling strategy
  11. optimize?: boolean; // optimize static content?
  12. // web specific
  13. mustUseProp?: (tag: string, type: ?string, name: string) => boolean; // check if an attribute should be bound as a property
  14. isPreTag?: (attr: string) => ?boolean; // check if a tag needs to preserve whitespace
  15. getTagNamespace?: (tag: string) => ?string; // check the namespace for a tag
  16. expectHTML?: boolean; // only false for non-web builds
  17. isFromDOM?: boolean;
  18. shouldDecodeTags?: boolean;
  19. shouldDecodeNewlines?: boolean;
  20. shouldDecodeNewlinesForHref?: boolean;
  21. outputSourceRange?: boolean;
  22. // runtime user-configurable
  23. delimiters?: [string, string]; // template delimiters
  24. comments?: boolean; // preserve comments in template
  25. // for ssr optimization compiler
  26. scopeId?: string;
  27. };
  28. declare type WarningMessage = {
  29. msg: string;
  30. start?: number;
  31. end?: number;
  32. };
  33. declare type CompiledResult = {
  34. ast: ?ASTElement;
  35. render: string;
  36. staticRenderFns: Array<string>;
  37. stringRenderFns?: Array<string>;
  38. errors?: Array<string | WarningMessage>;
  39. tips?: Array<string | WarningMessage>;
  40. };
  41. declare type ModuleOptions = {
  42. // transform an AST node before any attributes are processed
  43. // returning an ASTElement from pre/transforms replaces the element
  44. preTransformNode: (el: ASTElement) => ?ASTElement;
  45. // transform an AST node after built-ins like v-if, v-for are processed
  46. transformNode: (el: ASTElement) => ?ASTElement;
  47. // transform an AST node after its children have been processed
  48. // cannot return replacement in postTransform because tree is already finalized
  49. postTransformNode: (el: ASTElement) => void;
  50. genData: (el: ASTElement) => string; // generate extra data string for an element
  51. transformCode?: (el: ASTElement, code: string) => string; // further transform generated code for an element
  52. staticKeys?: Array<string>; // AST properties to be considered static
  53. };
  54. declare type ASTModifiers = { [key: string]: boolean };
  55. declare type ASTIfCondition = { exp: ?string; block: ASTElement };
  56. declare type ASTIfConditions = Array<ASTIfCondition>;
  57. declare type ASTAttr = {
  58. name: string;
  59. value: any;
  60. dynamic?: boolean;
  61. start?: number;
  62. end?: number
  63. };
  64. declare type ASTElementHandler = {
  65. value: string;
  66. params?: Array<any>;
  67. modifiers: ?ASTModifiers;
  68. dynamic?: boolean;
  69. start?: number;
  70. end?: number;
  71. };
  72. declare type ASTElementHandlers = {
  73. [key: string]: ASTElementHandler | Array<ASTElementHandler>;
  74. };
  75. declare type ASTDirective = {
  76. name: string;
  77. rawName: string;
  78. value: string;
  79. arg: ?string;
  80. isDynamicArg: boolean;
  81. modifiers: ?ASTModifiers;
  82. start?: number;
  83. end?: number;
  84. };
  85. declare type ASTNode = ASTElement | ASTText | ASTExpression;
  86. declare type ASTElement = {
  87. type: 1;
  88. tag: string;
  89. attrsList: Array<ASTAttr>;
  90. attrsMap: { [key: string]: any };
  91. rawAttrsMap: { [key: string]: ASTAttr };
  92. parent: ASTElement | void;
  93. children: Array<ASTNode>;
  94. start?: number;
  95. end?: number;
  96. processed?: true;
  97. static?: boolean;
  98. staticRoot?: boolean;
  99. staticInFor?: boolean;
  100. staticProcessed?: boolean;
  101. hasBindings?: boolean;
  102. text?: string;
  103. attrs?: Array<ASTAttr>;
  104. dynamicAttrs?: Array<ASTAttr>;
  105. props?: Array<ASTAttr>;
  106. plain?: boolean;
  107. pre?: true;
  108. ns?: string;
  109. component?: string;
  110. inlineTemplate?: true;
  111. transitionMode?: string | null;
  112. slotName?: ?string;
  113. slotTarget?: ?string;
  114. slotTargetDynamic?: boolean;
  115. slotScope?: ?string;
  116. scopedSlots?: { [name: string]: ASTElement };
  117. ref?: string;
  118. refInFor?: boolean;
  119. if?: string;
  120. ifProcessed?: boolean;
  121. elseif?: string;
  122. else?: true;
  123. ifConditions?: ASTIfConditions;
  124. for?: string;
  125. forProcessed?: boolean;
  126. key?: string;
  127. alias?: string;
  128. iterator1?: string;
  129. iterator2?: string;
  130. staticClass?: string;
  131. classBinding?: string;
  132. staticStyle?: string;
  133. styleBinding?: string;
  134. events?: ASTElementHandlers;
  135. nativeEvents?: ASTElementHandlers;
  136. transition?: string | true;
  137. transitionOnAppear?: boolean;
  138. model?: {
  139. value: string;
  140. callback: string;
  141. expression: string;
  142. };
  143. directives?: Array<ASTDirective>;
  144. forbidden?: true;
  145. once?: true;
  146. onceProcessed?: boolean;
  147. wrapData?: (code: string) => string;
  148. wrapListeners?: (code: string) => string;
  149. // 2.4 ssr optimization
  150. ssrOptimizability?: number;
  151. // weex specific
  152. appendAsTree?: boolean;
  153. };
  154. declare type ASTExpression = {
  155. type: 2;
  156. expression: string;
  157. text: string;
  158. tokens: Array<string | Object>;
  159. static?: boolean;
  160. // 2.4 ssr optimization
  161. ssrOptimizability?: number;
  162. start?: number;
  163. end?: number;
  164. };
  165. declare type ASTText = {
  166. type: 3;
  167. text: string;
  168. static?: boolean;
  169. isComment?: boolean;
  170. // 2.4 ssr optimization
  171. ssrOptimizability?: number;
  172. start?: number;
  173. end?: number;
  174. };
  175. // SFC-parser related declarations
  176. // an object format describing a single-file component
  177. declare type SFCDescriptor = {
  178. template: ?SFCBlock;
  179. script: ?SFCBlock;
  180. styles: Array<SFCBlock>;
  181. customBlocks: Array<SFCBlock>;
  182. errors: Array<string | WarningMessage>;
  183. }
  184. declare type SFCBlock = {
  185. type: string;
  186. content: string;
  187. attrs: {[attribute:string]: string};
  188. start?: number;
  189. end?: number;
  190. lang?: string;
  191. src?: string;
  192. scoped?: boolean;
  193. module?: string | boolean;
  194. };