optimizer.spec.ts 9.7 KB

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