optimizer.spec.js 9.3 KB

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