(children, context, parent)
| 74 | * This optimization is only performed in Node.js. |
| 75 | */ |
| 76 | export const stringifyStatic: HoistTransform = (children, context, parent) => { |
| 77 | // bail stringification for slot content |
| 78 | if (context.scopes.vSlot > 0) { |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | const isParentCached = |
| 83 | parent.type === NodeTypes.ELEMENT && |
| 84 | parent.codegenNode && |
| 85 | parent.codegenNode.type === NodeTypes.VNODE_CALL && |
| 86 | parent.codegenNode.children && |
| 87 | !isArray(parent.codegenNode.children) && |
| 88 | parent.codegenNode.children.type === NodeTypes.JS_CACHE_EXPRESSION |
| 89 | |
| 90 | let nc = 0 // current node count |
| 91 | let ec = 0 // current element with binding count |
| 92 | const currentChunk: StringifiableNode[] = [] |
| 93 | |
| 94 | const stringifyCurrentChunk = (currentIndex: number): number => { |
| 95 | if ( |
| 96 | nc >= StringifyThresholds.NODE_COUNT || |
| 97 | ec >= StringifyThresholds.ELEMENT_WITH_BINDING_COUNT |
| 98 | ) { |
| 99 | // combine all currently eligible nodes into a single static vnode call |
| 100 | const staticCall = createCallExpression(context.helper(CREATE_STATIC), [ |
| 101 | JSON.stringify( |
| 102 | currentChunk.map(node => stringifyNode(node, context)).join(''), |
| 103 | ).replace(expReplaceRE, `" + $1 + "`), |
| 104 | // the 2nd argument indicates the number of DOM nodes this static vnode |
| 105 | // will insert / hydrate |
| 106 | String(currentChunk.length), |
| 107 | ]) |
| 108 | |
| 109 | const deleteCount = currentChunk.length - 1 |
| 110 | |
| 111 | if (isParentCached) { |
| 112 | // if the parent is cached, then `children` is also the value of the |
| 113 | // CacheExpression. Just replace the corresponding range in the cached |
| 114 | // list with staticCall. |
| 115 | children.splice( |
| 116 | currentIndex - currentChunk.length, |
| 117 | currentChunk.length, |
| 118 | // @ts-expect-error |
| 119 | staticCall, |
| 120 | ) |
| 121 | } else { |
| 122 | // replace the first node's hoisted expression with the static vnode call |
| 123 | ;(currentChunk[0].codegenNode as CacheExpression).value = staticCall |
| 124 | if (currentChunk.length > 1) { |
| 125 | // remove merged nodes from children |
| 126 | children.splice(currentIndex - currentChunk.length + 1, deleteCount) |
| 127 | // also adjust index for the remaining cache items |
| 128 | const cacheIndex = context.cached.indexOf( |
| 129 | currentChunk[currentChunk.length - 1] |
| 130 | .codegenNode as CacheExpression, |
| 131 | ) |
| 132 | if (cacheIndex > -1) { |
| 133 | for (let i = cacheIndex; i < context.cached.length; i++) { |
nothing calls this directly
no test coverage detected