refactor(compiler): use shorter helpers for text and comment nodes

This commit is contained in:
Evan You
2019-10-24 17:55:00 -04:00
parent eb20730a67
commit 1c0a2c6d41
19 changed files with 126 additions and 108 deletions

View File

@@ -5,9 +5,10 @@ import {
TextNode,
InterpolationNode,
CompoundExpressionNode,
createCallExpression
createCallExpression,
CallExpression
} from '../ast'
import { TEXT, CREATE_VNODE } from '../runtimeHelpers'
import { CREATE_TEXT } from '../runtimeHelpers'
import { PatchFlags, PatchFlagNames } from '@vue/shared'
const isText = (
@@ -54,11 +55,17 @@ export const transformText: NodeTransform = (node, context) => {
if (hasText && children.length > 1) {
// when an element has mixed text/element children, convert text nodes
// into createVNode(Text) calls.
// into createTextVNode(text) calls.
for (let i = 0; i < children.length; i++) {
const child = children[i]
if (isText(child) || child.type === NodeTypes.COMPOUND_EXPRESSION) {
const callArgs = [context.helper(TEXT), `null`, child]
const callArgs: CallExpression['arguments'] = []
// createTextVNode defaults to single whitespace, so if it is a
// single space the code could be an empty call to save bytes.
if (child.type !== NodeTypes.TEXT || child.content !== ' ') {
callArgs.push(child)
}
// mark dynamic text with flag so it gets patched inside a block
if (child.type !== NodeTypes.TEXT) {
callArgs.push(
`${PatchFlags.TEXT} /* ${PatchFlagNames[PatchFlags.TEXT]} */`
@@ -69,7 +76,7 @@ export const transformText: NodeTransform = (node, context) => {
content: child,
loc: child.loc,
codegenNode: createCallExpression(
context.helper(CREATE_VNODE),
context.helper(CREATE_TEXT),
callArgs
)
}

View File

@@ -30,10 +30,10 @@ import { processExpression } from './transformExpression'
import {
OPEN_BLOCK,
CREATE_BLOCK,
COMMENT,
FRAGMENT,
WITH_DIRECTIVES,
CREATE_VNODE
CREATE_VNODE,
CREATE_COMMENT
} from '../runtimeHelpers'
import { injectProp } from '../utils'
@@ -152,9 +152,7 @@ function createCodegenNodeForBranch(
return createConditionalExpression(
branch.condition,
createChildrenCodegenNode(branch, index, context),
createCallExpression(context.helper(CREATE_BLOCK), [
context.helper(COMMENT)
])
createCallExpression(context.helper(CREATE_COMMENT))
) as IfConditionalExpression
} else {
return createChildrenCodegenNode(branch, index, context) as BlockCodegenNode