2019-10-23 21:57:40 +00:00
|
|
|
import { NodeTransform } from '../transform'
|
|
|
|
import { findDir } from '../utils'
|
2020-09-02 16:29:07 +00:00
|
|
|
import { ElementNode, ForNode, IfNode, NodeTypes } from '../ast'
|
2019-10-23 21:57:40 +00:00
|
|
|
import { SET_BLOCK_TRACKING } from '../runtimeHelpers'
|
2019-10-09 21:32:58 +00:00
|
|
|
|
2020-09-02 16:29:07 +00:00
|
|
|
const seen = new WeakSet()
|
|
|
|
|
2019-10-23 21:57:40 +00:00
|
|
|
export const transformOnce: NodeTransform = (node, context) => {
|
|
|
|
if (node.type === NodeTypes.ELEMENT && findDir(node, 'once', true)) {
|
2020-09-02 16:29:07 +00:00
|
|
|
if (seen.has(node)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
seen.add(node)
|
2019-10-23 21:57:40 +00:00
|
|
|
context.helper(SET_BLOCK_TRACKING)
|
|
|
|
return () => {
|
2020-09-02 16:29:07 +00:00
|
|
|
const cur = context.currentNode as ElementNode | IfNode | ForNode
|
|
|
|
if (cur.codegenNode) {
|
|
|
|
cur.codegenNode = context.cache(cur.codegenNode, true /* isVNode */)
|
2019-10-23 21:57:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-09 21:32:58 +00:00
|
|
|
}
|
|
|
|
}
|