optimizer.spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { parse } from 'compiler/parser/index'
  2. import { optimize } from 'compiler/optimizer'
  3. import { baseOptions } from 'web/compiler/index'
  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) // firts
  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('<h1 id="section1" v-if="show">hello world</h1>', 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><p v-else>foo bar</p></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[1].block.static).toBeUndefined()
  63. })
  64. it('v-pre directive', () => {
  65. const ast = parse('<ul v-pre><li>{{msg}}</li><li>world</li></ul>', baseOptions)
  66. optimize(ast, baseOptions)
  67. expect(ast.static).toBe(true)
  68. expect(ast.staticRoot).toBe(true)
  69. expect(ast.children[0].static).toBe(true)
  70. expect(ast.children[1].static).toBe(true)
  71. expect(ast.children[0].children[0].static).toBe(true)
  72. expect(ast.children[1].children[0].static).toBe(true)
  73. })
  74. it('v-for directive', () => {
  75. const ast = parse('<ul><li v-for="item in items">hello world {{$index}}</li></ul>', baseOptions)
  76. optimize(ast, baseOptions)
  77. // ul
  78. expect(ast.static).toBe(false)
  79. // li with v-for
  80. expect(ast.children[0].static).toBe(false)
  81. expect(ast.children[0].children[0].static).toBe(false)
  82. })
  83. it('v-once directive', () => {
  84. const ast = parse('<p v-once>{{msg}}</p>', baseOptions)
  85. optimize(ast, baseOptions)
  86. expect(ast.static).toBe(false) // p
  87. expect(ast.children[0].static).toBe(false) // text node
  88. })
  89. it('single slot', () => {
  90. const ast = parse('<div><slot>hello</slot></div>', baseOptions)
  91. optimize(ast, baseOptions)
  92. expect(ast.children[0].static).toBe(false) // slot
  93. expect(ast.children[0].children[0].static).toBe(true) // text node
  94. })
  95. it('named slot', () => {
  96. const ast = parse('<div><slot name="one">hello world</slot></div>', baseOptions)
  97. optimize(ast, baseOptions)
  98. expect(ast.children[0].static).toBe(false) // slot
  99. expect(ast.children[0].children[0].static).toBe(true) // text node
  100. })
  101. it('slot target', () => {
  102. const ast = parse('<p slot="one">hello world</p>', baseOptions)
  103. optimize(ast, baseOptions)
  104. expect(ast.static).toBe(false) // slot
  105. expect(ast.children[0].static).toBe(true) // text node
  106. })
  107. it('component', () => {
  108. const ast = parse('<my-component></my-component>', baseOptions)
  109. optimize(ast, baseOptions)
  110. expect(ast.static).toBe(false) // component
  111. })
  112. it('component for inline-template', () => {
  113. const ast = parse('<my-component inline-template><p>hello world</p><p>{{msg}}</p></my-component>', baseOptions)
  114. optimize(ast, baseOptions)
  115. // component
  116. expect(ast.static).toBe(false) // component
  117. // p
  118. expect(ast.children[0].static).toBe(true) // first
  119. expect(ast.children[1].static).toBe(false) // second
  120. // text node inside p
  121. expect(ast.children[0].children[0].static).toBe(true) // first
  122. expect(ast.children[1].children[0].static).toBe(false) // second
  123. })
  124. it('class binding', () => {
  125. const ast = parse('<p :class="class1">hello world</p>', baseOptions)
  126. optimize(ast, baseOptions)
  127. expect(ast.static).toBe(false)
  128. expect(ast.children[0].static).toBe(true)
  129. })
  130. it('style binding', () => {
  131. const ast = parse('<p :style="error">{{msg}}</p>', baseOptions)
  132. optimize(ast, baseOptions)
  133. expect(ast.static).toBe(false)
  134. expect(ast.children[0].static).toBe(false)
  135. })
  136. it('key', () => {
  137. const ast = parse('<p key="foo">hello world</p>', baseOptions)
  138. optimize(ast, baseOptions)
  139. expect(ast.static).toBe(false)
  140. expect(ast.children[0].static).toBe(true)
  141. })
  142. it('ref', () => {
  143. const ast = parse('<p ref="foo">hello world</p>', baseOptions)
  144. optimize(ast, baseOptions)
  145. expect(ast.static).toBe(false)
  146. expect(ast.children[0].static).toBe(true)
  147. })
  148. it('transition', () => {
  149. const ast = parse('<p v-if="show" transition="expand">hello world</p>', baseOptions)
  150. optimize(ast, baseOptions)
  151. expect(ast.static).toBe(false)
  152. expect(ast.children[0].static).toBe(true)
  153. })
  154. it('v-bind directive', () => {
  155. const ast = parse('<input type="text" name="field1" :value="msg">', baseOptions)
  156. optimize(ast, baseOptions)
  157. expect(ast.static).toBe(false)
  158. })
  159. it('v-on directive', () => {
  160. const ast = parse('<input type="text" name="field1" :value="msg" @input="onInput">', baseOptions)
  161. optimize(ast, baseOptions)
  162. expect(ast.static).toBe(false)
  163. })
  164. it('custom directive', () => {
  165. const ast = parse('<form><input type="text" name="field1" :value="msg" v-validate:field1="required"></form>', baseOptions)
  166. optimize(ast, baseOptions)
  167. expect(ast.static).toBe(false)
  168. expect(ast.children[0].static).toBe(false)
  169. })
  170. it('not root ast', () => {
  171. const ast = null
  172. optimize(ast, baseOptions)
  173. expect(ast).toBe(null)
  174. })
  175. it('not specified isReservedTag option', () => {
  176. const ast = parse('<h1 id="section1">hello world</h1>', baseOptions)
  177. optimize(ast, {})
  178. expect(ast.static).toBe(false)
  179. })
  180. it('mark static trees inside v-for', () => {
  181. const ast = parse(`<div><div v-for="i in 10"><p><span>hi</span></p></div></div>`, baseOptions)
  182. optimize(ast, baseOptions)
  183. expect(ast.children[0].children[0].staticRoot).toBe(true)
  184. expect(ast.children[0].children[0].staticInFor).toBe(true)
  185. })
  186. it('mark static trees inside v-for with nested v-else and v-once', () => {
  187. const ast = parse(`
  188. <div v-if="1"></div>
  189. <div v-else-if="2">
  190. <div v-for="i in 10" :key="i">
  191. <div v-if="1">{{ i }}</div>
  192. <div v-else-if="2" v-once>{{ i }}</div>
  193. <div v-else v-once>{{ i }}</div>
  194. </div>
  195. </div>
  196. <div v-else>
  197. <div v-for="i in 10" :key="i">
  198. <div v-if="1">{{ i }}</div>
  199. <div v-else v-once>{{ i }}</div>
  200. </div>
  201. </div>
  202. `, baseOptions)
  203. optimize(ast, baseOptions)
  204. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
  205. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
  206. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticRoot).toBe(false)
  207. expect(ast.ifConditions[1].block.children[0].children[0].ifConditions[2].block.staticInFor).toBe(true)
  208. expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticRoot).toBe(false)
  209. expect(ast.ifConditions[2].block.children[0].children[0].ifConditions[1].block.staticInFor).toBe(true)
  210. })
  211. })