1 |
|
2 |
|
3 | import { makeMap, isBuiltInTag, cached, no } from 'shared/util'
|
4 |
|
5 | let isStaticKey
|
6 | let isPlatformReservedTag
|
7 |
|
8 | const genStaticKeysCached = cached(genStaticKeys)
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export function optimize (root: ?ASTElement, options: CompilerOptions) {
|
22 | if (!root) return
|
23 | isStaticKey = genStaticKeysCached(options.staticKeys || '')
|
24 | isPlatformReservedTag = options.isReservedTag || no
|
25 |
|
26 | markStatic(root)
|
27 |
|
28 | markStaticRoots(root, false)
|
29 | }
|
30 |
|
31 | function genStaticKeys (keys: string): Function {
|
32 | return makeMap(
|
33 | 'type,tag,attrsList,attrsMap,plain,parent,children,attrs,start,end,rawAttrsMap' +
|
34 | (keys ? ',' + keys : '')
|
35 | )
|
36 | }
|
37 |
|
38 | function markStatic (node: ASTNode) {
|
39 | node.static = isStatic(node)
|
40 | if (node.type === 1) {
|
41 |
|
42 |
|
43 |
|
44 | if (
|
45 | !isPlatformReservedTag(node.tag) &&
|
46 | node.tag !== 'slot' &&
|
47 | node.attrsMap['inline-template'] == null
|
48 | ) {
|
49 | return
|
50 | }
|
51 | for (let i = 0, l = node.children.length; i < l; i++) {
|
52 | const child = node.children[i]
|
53 | markStatic(child)
|
54 | if (!child.static) {
|
55 | node.static = false
|
56 | }
|
57 | }
|
58 | if (node.ifConditions) {
|
59 | for (let i = 1, l = node.ifConditions.length; i < l; i++) {
|
60 | const block = node.ifConditions[i].block
|
61 | markStatic(block)
|
62 | if (!block.static) {
|
63 | node.static = false
|
64 | }
|
65 | }
|
66 | }
|
67 | }
|
68 | }
|
69 |
|
70 | function markStaticRoots (node: ASTNode, isInFor: boolean) {
|
71 | if (node.type === 1) {
|
72 | if (node.static || node.once) {
|
73 | node.staticInFor = isInFor
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 | if (node.static && node.children.length && !(
|
79 | node.children.length === 1 &&
|
80 | node.children[0].type === 3
|
81 | )) {
|
82 | node.staticRoot = true
|
83 | return
|
84 | } else {
|
85 | node.staticRoot = false
|
86 | }
|
87 | if (node.children) {
|
88 | for (let i = 0, l = node.children.length; i < l; i++) {
|
89 | markStaticRoots(node.children[i], isInFor || !!node.for)
|
90 | }
|
91 | }
|
92 | if (node.ifConditions) {
|
93 | for (let i = 1, l = node.ifConditions.length; i < l; i++) {
|
94 | markStaticRoots(node.ifConditions[i].block, isInFor)
|
95 | }
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 | function isStatic (node: ASTNode): boolean {
|
101 | if (node.type === 2) {
|
102 | return false
|
103 | }
|
104 | if (node.type === 3) {
|
105 | return true
|
106 | }
|
107 | return !!(node.pre || (
|
108 | !node.hasBindings &&
|
109 | !node.if && !node.for &&
|
110 | !isBuiltInTag(node.tag) &&
|
111 | isPlatformReservedTag(node.tag) &&
|
112 | !isDirectChildOfTemplateFor(node) &&
|
113 | Object.keys(node).every(isStaticKey)
|
114 | ))
|
115 | }
|
116 |
|
117 | function isDirectChildOfTemplateFor (node: ASTElement): boolean {
|
118 | while (node.parent) {
|
119 | node = node.parent
|
120 | if (node.tag !== 'template') {
|
121 | return false
|
122 | }
|
123 | if (node.for) {
|
124 | return true
|
125 | }
|
126 | }
|
127 | return false
|
128 | }
|