compiler.js 5.4 KB

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