Skip to content

Commit ec921ec

Browse files
committed
chore: tweaks
1 parent 18d2fac commit ec921ec

File tree

8 files changed

+71
-97
lines changed

8 files changed

+71
-97
lines changed

packages/compiler-core/__tests__/transforms/__snapshots__/cacheStatic.spec.ts.snap

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ return function render(_ctx, _cache) {
77
with (_ctx) {
88
const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
99
10-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
10+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
1111
_createElementVNode("div", { key: "foo" }, null, -1 /* CACHED */)
12-
])))
12+
]).slice())))
1313
}
1414
}"
1515
`;
@@ -21,7 +21,7 @@ return function render(_ctx, _cache) {
2121
with (_ctx) {
2222
const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
2323
24-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
24+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
2525
_createElementVNode("p", null, [
2626
_createElementVNode("span"),
2727
_createElementVNode("span")
@@ -30,7 +30,7 @@ return function render(_ctx, _cache) {
3030
_createElementVNode("span"),
3131
_createElementVNode("span")
3232
], -1 /* CACHED */)
33-
])))
33+
]).slice())))
3434
}
3535
}"
3636
`;
@@ -42,11 +42,11 @@ return function render(_ctx, _cache) {
4242
with (_ctx) {
4343
const { createCommentVNode: _createCommentVNode, createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
4444
45-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
45+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
4646
_createElementVNode("div", null, [
4747
_createCommentVNode("comment")
4848
], -1 /* CACHED */)
49-
])))
49+
]).slice())))
5050
}
5151
}"
5252
`;
@@ -58,11 +58,11 @@ return function render(_ctx, _cache) {
5858
with (_ctx) {
5959
const { createElementVNode: _createElementVNode, createTextVNode: _createTextVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
6060
61-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
61+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
6262
_createElementVNode("span", null, null, -1 /* CACHED */),
6363
_createTextVNode("foo", -1 /* CACHED */),
6464
_createElementVNode("div", null, null, -1 /* CACHED */)
65-
])))
65+
]).slice())))
6666
}
6767
}"
6868
`;
@@ -74,9 +74,9 @@ return function render(_ctx, _cache) {
7474
with (_ctx) {
7575
const { createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
7676
77-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
77+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
7878
_createElementVNode("span", { class: "inline" }, "hello", -1 /* CACHED */)
79-
])))
79+
]).slice())))
8080
}
8181
}"
8282
`;
@@ -147,9 +147,9 @@ return function render(_ctx, _cache) {
147147
with (_ctx) {
148148
const { toDisplayString: _toDisplayString, createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
149149
150-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
150+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
151151
_createElementVNode("span", null, "foo " + _toDisplayString(1) + " " + _toDisplayString(true), -1 /* CACHED */)
152-
])))
152+
]).slice())))
153153
}
154154
}"
155155
`;
@@ -161,9 +161,9 @@ return function render(_ctx, _cache) {
161161
with (_ctx) {
162162
const { toDisplayString: _toDisplayString, createElementVNode: _createElementVNode, openBlock: _openBlock, createElementBlock: _createElementBlock } = _Vue
163163
164-
return (_openBlock(), _createElementBlock("div", null, _cache[0] || (_cache[0] = [
164+
return (_openBlock(), _createElementBlock("div", null, (_cache[0] || (_cache[0] = [
165165
_createElementVNode("span", { foo: 0 }, _toDisplayString(1), -1 /* CACHED */)
166-
])))
166+
]).slice())))
167167
}
168168
}"
169169
`;
@@ -177,9 +177,9 @@ return function render(_ctx, _cache) {
177177
178178
return (_openBlock(), _createElementBlock("div", null, [
179179
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(1, (i) => {
180-
return (_openBlock(), _createElementBlock("div", null, [...(_cache[0] || (_cache[0] = [
180+
return (_openBlock(), _createElementBlock("div", null, [...((_cache[0] || (_cache[0] = [
181181
_createElementVNode("span", { class: "hi" }, null, -1 /* CACHED */)
182-
]))]))
182+
]).slice()))]))
183183
}), 256 /* UNKEYED_FRAGMENT */))
184184
]))
185185
}
@@ -215,9 +215,9 @@ return function render(_ctx, _cache) {
215215
const _directive_foo = _resolveDirective("foo")
216216
217217
return (_openBlock(), _createElementBlock("div", null, [
218-
_withDirectives((_openBlock(), _createElementBlock("svg", null, _cache[0] || (_cache[0] = [
218+
_withDirectives((_openBlock(), _createElementBlock("svg", null, (_cache[0] || (_cache[0] = [
219219
_createElementVNode("path", { d: "M2,3H5.5L12" }, null, -1 /* CACHED */)
220-
]))), [
220+
]).slice()))), [
221221
[_directive_foo]
222222
])
223223
]))
@@ -401,9 +401,9 @@ return function render(_ctx, _cache) {
401401
402402
return (_openBlock(), _createElementBlock("div", null, [
403403
ok
404-
? (_openBlock(), _createElementBlock("div", _hoisted_1, _cache[0] || (_cache[0] = [
404+
? (_openBlock(), _createElementBlock("div", _hoisted_1, (_cache[0] || (_cache[0] = [
405405
_createElementVNode("span", null, null, -1 /* CACHED */)
406-
])))
406+
]).slice())))
407407
: _createCommentVNode("v-if", true)
408408
]))
409409
}
@@ -422,15 +422,15 @@ return function render(_ctx, _cache) {
422422
423423
return (_openBlock(), _createElementBlock(_Fragment, null, [
424424
_createCommentVNode("comment"),
425-
_createElementVNode("div", _hoisted_1, _cache[0] || (_cache[0] = [
425+
_createElementVNode("div", _hoisted_1, (_cache[0] || (_cache[0] = [
426426
_createElementVNode("div", { id: "b" }, [
427427
_createElementVNode("div", { id: "c" }, [
428428
_createElementVNode("div", { id: "d" }, [
429429
_createElementVNode("div", { id: "e" }, "hello")
430430
])
431431
])
432432
], -1 /* CACHED */)
433-
]))
433+
]).slice()))
434434
], 2112 /* STABLE_FRAGMENT, DEV_ROOT_FRAGMENT */))
435435
}
436436
}"
@@ -448,9 +448,9 @@ return function render(_ctx, _cache) {
448448
449449
return (_openBlock(), _createElementBlock("div", null, [
450450
(_openBlock(true), _createElementBlock(_Fragment, null, _renderList(list, (i) => {
451-
return (_openBlock(), _createElementBlock("div", _hoisted_1, _cache[0] || (_cache[0] = [
451+
return (_openBlock(), _createElementBlock("div", _hoisted_1, (_cache[0] || (_cache[0] = [
452452
_createElementVNode("span", null, null, -1 /* CACHED */)
453-
])))
453+
]).slice())))
454454
}), 256 /* UNKEYED_FRAGMENT */))
455455
]))
456456
}

packages/compiler-core/src/codegen.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ function genCacheExpression(node: CacheExpression, context: CodegenContext) {
10161016
if (needArraySpread) {
10171017
push(`[...(`)
10181018
}
1019+
if (cachedAsArray) push(`(`)
10191020
push(`_cache[${node.index}] || (`)
10201021
if (needPauseTracking) {
10211022
indent()
@@ -1027,9 +1028,7 @@ function genCacheExpression(node: CacheExpression, context: CodegenContext) {
10271028
}
10281029
push(`_cache[${node.index}] = `)
10291030
genNode(node.value, context)
1030-
if (cachedAsArray) {
1031-
push(`, _cache[${node.index}].patchFlag = -1, _cache[${node.index}]`)
1032-
}
1031+
if (cachedAsArray) push(`).slice()`)
10331032
if (needPauseTracking) {
10341033
push(`).cacheIndex = ${node.index},`)
10351034
newline()

packages/compiler-core/src/transforms/cacheStatic.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,7 @@ function walk(
6767
inFor = false,
6868
) {
6969
const { children } = node
70-
const toCacheMap = new Map<
71-
PlainElementNode | TextCallNode,
72-
undefined | (() => void)
73-
>()
70+
const toCache: (PlainElementNode | TextCallNode)[] = []
7471
for (let i = 0; i < children.length; i++) {
7572
const child = children[i]
7673
// only plain elements & text calls are eligible for caching.
@@ -83,11 +80,8 @@ function walk(
8380
: getConstantType(child, context)
8481
if (constantType > ConstantTypes.NOT_CONSTANT) {
8582
if (constantType >= ConstantTypes.CAN_CACHE) {
86-
toCacheMap.set(
87-
child,
88-
() =>
89-
((child.codegenNode as VNodeCall).patchFlag = PatchFlags.CACHED),
90-
)
83+
;(child.codegenNode as VNodeCall).patchFlag = PatchFlags.CACHED
84+
toCache.push(child)
9185
continue
9286
}
9387
} else {
@@ -118,17 +112,16 @@ function walk(
118112
? ConstantTypes.NOT_CONSTANT
119113
: getConstantType(child, context)
120114
if (constantType >= ConstantTypes.CAN_CACHE) {
121-
toCacheMap.set(child, () => {
122-
if (
123-
child.codegenNode.type === NodeTypes.JS_CALL_EXPRESSION &&
124-
child.codegenNode.arguments.length > 0
125-
) {
126-
child.codegenNode.arguments.push(
127-
PatchFlags.CACHED +
128-
(__DEV__ ? ` /* ${PatchFlagNames[PatchFlags.CACHED]} */` : ``),
129-
)
130-
}
131-
})
115+
if (
116+
child.codegenNode.type === NodeTypes.JS_CALL_EXPRESSION &&
117+
child.codegenNode.arguments.length > 0
118+
) {
119+
child.codegenNode.arguments.push(
120+
PatchFlags.CACHED +
121+
(__DEV__ ? ` /* ${PatchFlagNames[PatchFlags.CACHED]} */` : ``),
122+
)
123+
}
124+
toCache.push(child)
132125
continue
133126
}
134127
}
@@ -161,7 +154,7 @@ function walk(
161154
}
162155

163156
let cachedAsArray = false
164-
if (toCacheMap.size === children.length && node.type === NodeTypes.ELEMENT) {
157+
if (toCache.length === children.length && node.type === NodeTypes.ELEMENT) {
165158
if (
166159
node.tagType === ElementTypes.ELEMENT &&
167160
node.codegenNode &&
@@ -216,8 +209,7 @@ function walk(
216209
}
217210

218211
if (!cachedAsArray) {
219-
for (const [child, setupCache] of toCacheMap) {
220-
if (setupCache) setupCache()
212+
for (const child of toCache) {
221213
child.codegenNode = context.cache(child.codegenNode!)
222214
}
223215
}
@@ -253,7 +245,7 @@ function walk(
253245
}
254246
}
255247

256-
if (toCacheMap.size && context.transformHoist) {
248+
if (toCache.length && context.transformHoist) {
257249
context.transformHoist(children, context, node)
258250
}
259251
}

0 commit comments

Comments
 (0)