return function render() {
with (this) {
- const { createVNode: _createVNode, toString: _toString, openBlock: _openBlock, createBlock: _createBlock, Empty: _Empty, Fragment: _Fragment, renderList: _renderList } = _Vue
+ const { toString: _toString, openBlock: _openBlock, createVNode: _createVNode, createBlock: _createBlock, Empty: _Empty, Fragment: _Fragment, renderList: _renderList } = _Vue
return _createVNode(\\"div\\", {
id: \\"foo\\",
])),
_createVNode(_Fragment, null, _renderList(list, (value, index) => {
return (_openBlock(), _createBlock(\\"div\\", null, [
- _createVNode(\\"span\\", null, _toString(value + index))
+ _createVNode(\\"span\\", null, _toString(value + index), 1 /* TEXT */)
]))
}), 128 /* UNKEYED_FRAGMENT */)
], 2 /* CLASS */)
`;
exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 1`] = `
-"const { createVNode, toString, openBlock, createBlock, Empty, Fragment, renderList } = Vue
+"const { toString, openBlock, createVNode, createBlock, Empty, Fragment, renderList } = Vue
return function render() {
const _ctx = this
])),
createVNode(Fragment, null, renderList(_ctx.list, (value, index) => {
return (openBlock(), createBlock(\\"div\\", null, [
- createVNode(\\"span\\", null, toString(value + index))
+ createVNode(\\"span\\", null, toString(value + index), 1 /* TEXT */)
]))
}), 128 /* UNKEYED_FRAGMENT */)
], 2 /* CLASS */)
`;
exports[`compiler: integration tests module mode 1`] = `
-"import { createVNode, toString, openBlock, createBlock, Empty, Fragment, renderList } from \\"vue\\"
+"import { toString, openBlock, createVNode, createBlock, Empty, Fragment, renderList } from \\"vue\\"
export default function render() {
const _ctx = this
])),
createVNode(Fragment, null, renderList(_ctx.list, (value, index) => {
return (openBlock(), createBlock(\\"div\\", null, [
- createVNode(\\"span\\", null, _toString(value + index))
+ createVNode(\\"span\\", null, _toString(value + index), 1 /* TEXT */)
]))
}), 128 /* UNKEYED_FRAGMENT */)
], 2 /* CLASS */)
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`compiler: transform component slots dynamically named slots 1`] = `
-"const { resolveComponent, createVNode, toString } = Vue
+"const { toString, resolveComponent, createVNode } = Vue
return function render() {
const _ctx = this
`;
exports[`compiler: transform component slots explicit default slot 1`] = `
-"const { resolveComponent, createVNode, toString } = Vue
+"const { toString, resolveComponent, createVNode } = Vue
return function render() {
const _ctx = this
`;
exports[`compiler: transform component slots implicit default slot 1`] = `
-"const { resolveComponent, createVNode } = Vue
+"const { createVNode, resolveComponent } = Vue
return function render() {
const _ctx = this
`;
exports[`compiler: transform component slots named slots 1`] = `
-"const { resolveComponent, createVNode, toString } = Vue
+"const { toString, resolveComponent, createVNode } = Vue
return function render() {
const _ctx = this
`;
exports[`compiler: transform component slots nested slots scoping 1`] = `
-"const { resolveComponent, createVNode, toString } = Vue
+"const { toString, resolveComponent, createVNode } = Vue
return function render() {
const _ctx = this
- const _component_Comp = resolveComponent(\\"Comp\\")
const _component_Inner = resolveComponent(\\"Inner\\")
+ const _component_Comp = resolveComponent(\\"Comp\\")
return createVNode(_component_Comp, null, {
default: ({ foo }) => [
import { transformBind } from '../../src/transforms/vBind'
import { PatchFlags } from '@vue/shared'
import { createObjectMatcher } from '../testUtils'
+import { optimizeText } from '../../src/transforms/optimizeText'
function parseWithElementTransform(
template: string,
} {
const ast = parse(template, options)
transform(ast, {
- nodeTransforms: [transformElement],
+ nodeTransforms: [optimizeText, transformElement],
...options
})
const codegenNode = (ast.children[0] as ElementNode)
})
}
+ test('TEXT', () => {
+ const { node } = parseWithBind(`<div>foo</div>`)
+ expect(node.arguments.length).toBe(3)
+
+ const { node: node2 } = parseWithBind(`<div>{{ foo }}</div>`)
+ expect(node2.arguments.length).toBe(4)
+ expect(node2.arguments[3]).toBe(`${PatchFlags.TEXT} /* TEXT */`)
+
+ // multiple nodes, merged with optimze text
+ const { node: node3 } = parseWithBind(`<div>foo {{ bar }} baz</div>`)
+ expect(node3.arguments.length).toBe(4)
+ expect(node3.arguments[3]).toBe(`${PatchFlags.TEXT} /* TEXT */`)
+ })
+
test('CLASS', () => {
const { node } = parseWithBind(`<div :class="foo" />`)
expect(node.arguments.length).toBe(4)
one: {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `{ foo }`,
- isStatic: false
+ type: NodeTypes.COMPOUND_EXPRESSION,
+ children: [`{ `, { content: `foo` }, ` }`]
},
returns: [
{
two: {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `{ bar }`,
- isStatic: false
+ type: NodeTypes.COMPOUND_EXPRESSION,
+ children: [`{ `, { content: `bar` }, ` }`]
},
returns: [
{
'[_ctx.one]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `{ foo }`,
- isStatic: false
+ type: NodeTypes.COMPOUND_EXPRESSION,
+ children: [`{ `, { content: `foo` }, ` }`]
},
returns: [
{
'[_ctx.two]': {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `{ bar }`,
- isStatic: false
+ type: NodeTypes.COMPOUND_EXPRESSION,
+ children: [`{ `, { content: `bar` }, ` }`]
},
returns: [
{
default: {
type: NodeTypes.JS_FUNCTION_EXPRESSION,
params: {
- type: NodeTypes.SIMPLE_EXPRESSION,
- content: `{ foo }`,
- isStatic: false
+ type: NodeTypes.COMPOUND_EXPRESSION,
+ children: [`{ `, { content: `foo` }, ` }`]
},
returns: [
{
node.tagType === ElementTypes.ELEMENT ||
node.tagType === ElementTypes.COMPONENT
) {
- const isComponent = node.tagType === ElementTypes.COMPONENT
- let hasProps = node.props.length > 0
- const hasChildren = node.children.length > 0
- let patchFlag: number = 0
- let runtimeDirectives: DirectiveNode[] | undefined
- let dynamicPropNames: string[] | undefined
- let componentIdentifier: string | undefined
+ // perform the work on exit, after all child expressions have been
+ // processed and merged.
+ return () => {
+ const isComponent = node.tagType === ElementTypes.COMPONENT
+ let hasProps = node.props.length > 0
+ const hasChildren = node.children.length > 0
+ let patchFlag: number = 0
+ let runtimeDirectives: DirectiveNode[] | undefined
+ let dynamicPropNames: string[] | undefined
+ let componentIdentifier: string | undefined
- if (isComponent) {
- componentIdentifier = `_component_${toValidId(node.tag)}`
- context.statements.add(
- `const ${componentIdentifier} = ${context.helper(
- RESOLVE_COMPONENT
- )}(${JSON.stringify(node.tag)})`
- )
- }
-
- const args: CallExpression['arguments'] = [
- isComponent ? componentIdentifier! : `"${node.tag}"`
- ]
- // props
- if (hasProps) {
- const propsBuildResult = buildProps(
- node.props,
- node.loc,
- context,
- isComponent
- )
- patchFlag = propsBuildResult.patchFlag
- dynamicPropNames = propsBuildResult.dynamicPropNames
- runtimeDirectives = propsBuildResult.directives
- if (!propsBuildResult.props) {
- hasProps = false
- } else {
- args.push(propsBuildResult.props)
+ if (isComponent) {
+ componentIdentifier = `_component_${toValidId(node.tag)}`
+ context.statements.add(
+ `const ${componentIdentifier} = ${context.helper(
+ RESOLVE_COMPONENT
+ )}(${JSON.stringify(node.tag)})`
+ )
}
- }
- // children
- if (hasChildren) {
- if (!hasProps) {
- args.push(`null`)
+
+ const args: CallExpression['arguments'] = [
+ isComponent ? componentIdentifier! : `"${node.tag}"`
+ ]
+ // props
+ if (hasProps) {
+ const propsBuildResult = buildProps(
+ node.props,
+ node.loc,
+ context,
+ isComponent
+ )
+ patchFlag = propsBuildResult.patchFlag
+ dynamicPropNames = propsBuildResult.dynamicPropNames
+ runtimeDirectives = propsBuildResult.directives
+ if (!propsBuildResult.props) {
+ hasProps = false
+ } else {
+ args.push(propsBuildResult.props)
+ }
}
- if (isComponent) {
- const { slots, hasDynamicSlotName } = buildSlots(node, context)
- args.push(slots)
- if (hasDynamicSlotName) {
- patchFlag |= PatchFlags.DYNAMIC_SLOTS
+ // children
+ if (hasChildren) {
+ if (!hasProps) {
+ args.push(`null`)
}
- } else {
- if (node.children.length === 1) {
+ if (isComponent) {
+ const { slots, hasDynamicSlotName } = buildSlots(node, context)
+ args.push(slots)
+ if (hasDynamicSlotName) {
+ patchFlag |= PatchFlags.DYNAMIC_SLOTS
+ }
+ } else if (node.children.length === 1) {
const child = node.children[0]
const type = child.type
+ const hasDynamicTextChild =
+ type === NodeTypes.INTERPOLATION ||
+ type === NodeTypes.COMPOUND_EXPRESSION
+ if (hasDynamicTextChild) {
+ patchFlag |= PatchFlags.TEXT
+ }
// pass directly if the only child is one of:
// - text (plain / interpolation / expression)
// - <slot> outlet (already an array)
if (
type === NodeTypes.TEXT ||
- type === NodeTypes.INTERPOLATION ||
- type === NodeTypes.COMPOUND_EXPRESSION ||
+ hasDynamicTextChild ||
(type === NodeTypes.ELEMENT &&
(child as ElementNode).tagType === ElementTypes.SLOT)
) {
args.push(node.children)
}
}
- }
- // patchFlag & dynamicPropNames
- if (patchFlag !== 0) {
- if (!hasChildren) {
- if (!hasProps) {
+ // patchFlag & dynamicPropNames
+ if (patchFlag !== 0) {
+ if (!hasChildren) {
+ if (!hasProps) {
+ args.push(`null`)
+ }
args.push(`null`)
}
- args.push(`null`)
- }
- if (__DEV__) {
- const flagNames = Object.keys(PatchFlagNames)
- .filter(n => patchFlag & Number(n))
- .map(n => PatchFlagNames[n as any])
- .join(`, `)
- args.push(patchFlag + ` /* ${flagNames} */`)
- } else {
- args.push(patchFlag + '')
- }
- if (dynamicPropNames && dynamicPropNames.length) {
- args.push(
- `[${dynamicPropNames.map(n => JSON.stringify(n)).join(`, `)}]`
- )
+ if (__DEV__) {
+ const flagNames = Object.keys(PatchFlagNames)
+ .filter(n => patchFlag & Number(n))
+ .map(n => PatchFlagNames[n as any])
+ .join(`, `)
+ args.push(patchFlag + ` /* ${flagNames} */`)
+ } else {
+ args.push(patchFlag + '')
+ }
+ if (dynamicPropNames && dynamicPropNames.length) {
+ args.push(
+ `[${dynamicPropNames.map(n => JSON.stringify(n)).join(`, `)}]`
+ )
+ }
}
- }
- const { loc } = node
- const vnode = createCallExpression(
- context.helper(CREATE_VNODE),
- args,
- loc
- )
-
- if (runtimeDirectives && runtimeDirectives.length) {
- node.codegenNode = createCallExpression(
- context.helper(APPLY_DIRECTIVES),
- [
- vnode,
- createArrayExpression(
- runtimeDirectives.map(dir => {
- return createDirectiveArgs(dir, context)
- }),
- loc
- )
- ],
+ const { loc } = node
+ const vnode = createCallExpression(
+ context.helper(CREATE_VNODE),
+ args,
loc
)
- } else {
- node.codegenNode = vnode
+
+ if (runtimeDirectives && runtimeDirectives.length) {
+ node.codegenNode = createCallExpression(
+ context.helper(APPLY_DIRECTIVES),
+ [
+ vnode,
+ createArrayExpression(
+ runtimeDirectives.map(dir => {
+ return createDirectiveArgs(dir, context)
+ }),
+ loc
+ )
+ ],
+ loc
+ )
+ } else {
+ node.codegenNode = vnode
+ }
}
}
}