optimizer.spec.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { baseOptions } from 'web/compiler/options'
  4. describe('optimizer', () => {
  5. it('simple', () => {
  6. const ast = parse('<h1 id="section1"><span>hello world</span></h1>', baseOptions)
  7. optimize(ast, baseOptions)
  8. expect(ast.static).toBe(true) // h1
  9. expect(ast.staticRoot).toBe(true)
  10. expect(ast.children[0].static).toBe(true) // span
  11. })
  12. it('skip simple nodes', () => {
  13. const ast = parse('<h1 id="section1">hello</h1>', baseOptions)
  14. optimize(ast, baseOptions)
  15. expect(ast.static).toBe(true)
  16. expect(ast.staticRoot).toBe(false) // this is too simple to warrant a static tree
  17. })
  18. it('interpolation', () => {
  19. const ast = parse('<h1>{{msg}}</h1>', baseOptions)
  20. optimize(ast, baseOptions)
  21. expect(ast.static).toBe(false) // h1
  22. expect(ast.children[0].static).toBe(false) // text node with interpolation
  23. })
  24. it('nested elements', () => {
  25. const ast = parse('<ul><li>hello</li><li>world</li></ul>', baseOptions)
  26. optimize(ast, baseOptions)
  27. // ul
  28. expect(ast.static).toBe(true)
  29. expect(ast.staticRoot).toBe(true)
  30. // li
  31. expect(ast.children[0].static).toBe(true) // first
  32. expect(ast.children[1].static).toBe(true) // second
  33. // text node inside li
  34. expect(ast.children[0].children[0].static).toBe(true) // first
  35. expect(ast.children[1].children[0].static).toBe(true) // second
  36. })
  37. it('nested complex elements', () => {
  38. const ast = parse('<ul><li>{{msg1}}</li><li>---</li><li>{{msg2}}</li></ul>', baseOptions)
  39. optimize(ast, baseOptions)
  40. // ul
  41. expect(ast.static).toBe(false) // ul
  42. // li
  43. expect(ast.children[0].static).toBe(false) // first
  44. expect(ast.children[1].static).toBe(true) // second
  45. expect(ast.children[2].static).toBe(false) // third
  46. // text node inside li
  47. expect(ast.children[0].children[0].static).toBe(false) // first
  48. expect(ast.children[1].children[0].static).toBe(true) // second
  49. expect(ast.children[2].children[0].static).toBe(false) // third
  50. })
  51. it('v-if directive', () => {
  52. const ast = parse('<div id="section1" v-if="show"><p><span>hello world</span></p></div>', baseOptions)
  53. optimize(ast, baseOptions)
  54. expect(ast.static).toBe(false)
  55. expect(ast.children[0].static).toBe(true)
  56. })
  57. it('v-else directive', () => {
  58. const ast = parse('<div><p v-if="show">hello world</p><div v-else><p><span>foo bar</span></p></div></div>', baseOptions)
  59. optimize(ast, baseOptions)
  60. expect(ast.static).toBe(false)
  61. expect(ast.children[0].static).toBe(false)
  62. expect(ast.children[0].ifConditions[0].block.static).toBe(false)
  63. expect(ast.children[0].ifConditions[1].block.static).toBe(false)
  64. expect(ast.children[0].ifConditions[0].block.children[0].static).toBe(true)
  65. expect(ast.children[0].ifConditions[1].block.children[0].static).toBe(true)
  66. })
  67. it('v-pre directive', () => {
  68. const ast = parse('<ul v-pre><li>{{msg}}</li><li>world</li></ul>', baseOptions)
  69. optimize(ast, baseOptions)
  70. expect(ast.static).toBe(true)
  71. expect(ast.staticRoot).toBe(true)
  72. expect(ast.children[0].static).toBe(true)
  73. expect(ast.children[1].static).toBe(true)
  74. expect(ast.children[0].children[0].static).toBe(true)
  75. expect(ast.children[1].children[0].static).toBe(true)
  76. })
  77. it('v-for directive', () => {
  78. const ast = parse('<ul><li v-for="item in items">hello world {{$index}}</li></ul>', baseOptions)
  79. optimize(ast, baseOptions)
  80. // ul
  81. expect(ast.static).toBe(false)
  82. // li with v-for
  83. expect(ast.children[0].static).toBe(false)
  84. expect(ast.children[0].children[0].static).toBe(false)
  85. })
  86. it('v-once directive', () => {
  87. const ast = parse('<p v-once>{{msg}}</p>', baseOptions)
  88. optimize(ast, baseOptions)
  89. expect(ast.static).toBe(false) // p
  90. expect(ast.children[0].static).toBe(false) // text node
  91. })
  92. it('single slot', () => {
  93. const ast = parse('<div><slot>hello</slot></div>', baseOptions)
  94. optimize(ast, baseOptions)
  95. expect(ast.children[0].static).toBe(false) // slot
  96. expect(ast.children[0].children[0].static).toBe(true) // text node
  97. })
  98. it('named slot', () => {
  99. const ast = parse('<div><slot name="one">hello world</slot></div>', baseOptions)
  100. optimize(ast, baseOptions)
  101. expect(ast.children[0].static).toBe(false) // slot
  102. expect(ast.children[0].children[0].static).toBe(true) // text node
  103. })
  104. it('slot target', () => {
  105. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  106. optimize(ast, baseOptions)
  107. expect(ast.static).toBe(false) // slot
  108. expect(ast.children[0].static).toBe(true) // text node
  109. })
  110. it('component', () => {
  111. const ast = parse('<my-component></my-component>', baseOptions)
  112. optimize(ast, baseOptions)
  113. expect(ast.static).toBe(false) // component
  114. })
  115. it('component for inline-template', () => {
  116. const ast = parse('<my-component inline-template><p>hello world</p><p>{{msg}}</p></my-component>', baseOptions)
  117. optimize(ast, baseOptions)
  118. // component
  119. expect(ast.static).toBe(false) // component
  120. // p
  121. expect(ast.children[0].static).toBe(true) // first
  122. expect(ast.children[1].static).toBe(false) // second
  123. // text node inside p
  124. expect(ast.children[0].children[0].static).toBe(true) // first
  125. expect(ast.children[1].children[0].static).toBe(false) // second
  126. })
  127. it('class binding', () => {
  128. const ast = parse('<p :class="class1">hello world</p>', baseOptions)
  129. optimize(ast, baseOptions)
  130. expect(ast.static).toBe(false)
  131. expect(ast.children[0].static).toBe(true)
  132. })
  133. it('style binding', () => {
  134. const ast = parse('<p :style="error">{{msg}}</p>', baseOptions)
  135. optimize(ast, baseOptions)
  136. expect(ast.static).toBe(false)
  137. expect(ast.children[0].static).toBe(false)
  138. })
  139. it('key', () => {
  140. const ast = parse('<p key="foo">hello world</p>', baseOptions)
  141. optimize(ast, baseOptions)
  142. expect(ast.static).toBe(false)
  143. expect(ast.children[0].static).toBe(true)
  144. })
  145. it('ref', () => {
  146. const ast = parse('<p ref="foo">hello world</p>', baseOptions)
  147. optimize(ast, baseOptions)
  148. expect(ast.static).toBe(false)
  149. expect(ast.children[0].static).toBe(true)
  150. })
  151. it('transition', () => {
  152. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  153. optimize(ast, baseOptions)
  154. expect(ast.static).toBe(false)
  155. expect(ast.children[0].static).toBe(true)
  156. })
  157. it('v-bind directive', () => {
  158. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  159. optimize(ast, baseOptions)
  160. expect(ast.static).toBe(false)
  161. })
  162. it('v-on directive', () => {
  163. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  164. optimize(ast, baseOptions)
  165. expect(ast.static).toBe(false)
  166. })
  167. it('custom directive', () => {
  168. const ast = parse('<form><input type="text" name="field1" :value="msg" v-validate:field1="required"></form>', baseOptions)
  169. optimize(ast, baseOptions)
  170. expect(ast.static).toBe(false)
  171. expect(ast.children[0].static).toBe(false)
  172. })
  173. it('not root ast', () => {
  174. const ast = null
  175. optimize(ast, baseOptions)
  176. expect(ast).toBe(null)
  177. })
  178. it('not specified isReservedTag option', () => {
  179. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  180. optimize(ast, {})
  181. expect(ast.static).toBe(false)
  182. })
  183. it('mark static trees inside v-for', () => {
  184. const ast = parse(`<div><div v-for="i in 10"><p><span>hi</span></p></div></div>`, baseOptions)
  185. optimize(ast, baseOptions)
  186. expect(ast.children[0].children[0].staticRoot).toBe(true)
  187. expect(ast.children[0].children[0].staticInFor).toBe(true)
  188. })
  189. it('mark static trees inside v-for with nested v-else and v-once', () => {
  190. const ast = parse(`
  191. <div v-if="1"></div>
  192. <div v-else-if="2">
  193. <div v-for="i in 10" :key="i">
  194. <div v-if="1">{{ i }}</div>
  195. <div v-else-if="2" v-once>{{ i }}</div>
  196. <div v-else v-once>{{ i }}</div>
  197. </div>
  198. </div>
  199. <div v-else>
  200. <div v-for="i in 10" :key="i">
  201. <div v-if="1">{{ i }}</div>
  202. <div v-else v-once>{{ i }}</div>
  203. </div>
  204. </div>
  205. `, baseOptions)
  206. optimize(ast, baseOptions)
  207. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
  208. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
  209. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticRoot).toBe(false)
  210. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticInFor).toBe(true)
  211. expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
  212. expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
  213. })
  214. })