From 4547d85a388ebb0f982a8bc48c66c83aca3cae24 Mon Sep 17 00:00:00 2001 From: terencez Date: Tue, 15 Oct 2019 03:11:04 +0800 Subject: [PATCH] feat(compiler-core): support in template (#203) --- .../transforms/transformElement.spec.ts | 49 ++++++++++++++++++- packages/compiler-core/src/ast.ts | 9 +++- packages/compiler-core/src/parse.ts | 1 + .../src/transforms/transformElement.ts | 11 ++++- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts index ae44be68..8f803640 100644 --- a/packages/compiler-core/__tests__/transforms/transformElement.spec.ts +++ b/packages/compiler-core/__tests__/transforms/transformElement.spec.ts @@ -6,7 +6,8 @@ import { RESOLVE_DIRECTIVE, APPLY_DIRECTIVES, TO_HANDLERS, - helperNameMap + helperNameMap, + PORTAL } from '../../src/runtimeHelpers' import { CallExpression, @@ -255,6 +256,52 @@ describe('compiler: element transform', () => { ]) }) + test('should handle element', () => { + const { node } = parseWithElementTransform( + `` + ) + expect(node.callee).toBe(CREATE_VNODE) + expect(node.arguments).toMatchObject([ + PORTAL, + createObjectMatcher({ + target: '#foo' + }), + [ + { + type: NodeTypes.ELEMENT, + tag: 'span', + codegenNode: { + callee: CREATE_VNODE, + arguments: [`"span"`] + } + } + ] + ]) + }) + + test('should handle element', () => { + const { node } = parseWithElementTransform( + `` + ) + expect(node.callee).toBe(CREATE_VNODE) + expect(node.arguments).toMatchObject([ + PORTAL, + createObjectMatcher({ + target: '#foo' + }), + [ + { + type: NodeTypes.ELEMENT, + tag: 'span', + codegenNode: { + callee: CREATE_VNODE, + arguments: [`"span"`] + } + } + ] + ]) + }) + test('error on v-bind with no argument', () => { const onError = jest.fn() parseWithElementTransform(`
`, { onError }) diff --git a/packages/compiler-core/src/ast.ts b/packages/compiler-core/src/ast.ts index b6442202..55916765 100644 --- a/packages/compiler-core/src/ast.ts +++ b/packages/compiler-core/src/ast.ts @@ -49,7 +49,8 @@ export const enum ElementTypes { ELEMENT, COMPONENT, SLOT, - TEMPLATE + TEMPLATE, + PORTAL } export interface Node { @@ -99,6 +100,7 @@ export type ElementNode = | ComponentNode | SlotOutletNode | TemplateNode + | PortalNode export interface BaseElementNode extends Node { type: NodeTypes.ELEMENT @@ -134,6 +136,11 @@ export interface TemplateNode extends BaseElementNode { | undefined } +export interface PortalNode extends BaseElementNode { + tagType: ElementTypes.PORTAL + codegenNode: ElementCodegenNode | undefined +} + export interface TextNode extends Node { type: NodeTypes.TEXT content: string diff --git a/packages/compiler-core/src/parse.ts b/packages/compiler-core/src/parse.ts index 1e8a6d2a..323d46e0 100644 --- a/packages/compiler-core/src/parse.ts +++ b/packages/compiler-core/src/parse.ts @@ -445,6 +445,7 @@ function parseTag( if (tag === 'slot') tagType = ElementTypes.SLOT else if (tag === 'template') tagType = ElementTypes.TEMPLATE + else if (tag === 'portal' || tag === 'Portal') tagType = ElementTypes.PORTAL } return { diff --git a/packages/compiler-core/src/transforms/transformElement.ts b/packages/compiler-core/src/transforms/transformElement.ts index 3df966a8..dd0f2d0e 100644 --- a/packages/compiler-core/src/transforms/transformElement.ts +++ b/packages/compiler-core/src/transforms/transformElement.ts @@ -23,7 +23,8 @@ import { RESOLVE_DIRECTIVE, RESOLVE_COMPONENT, MERGE_PROPS, - TO_HANDLERS + TO_HANDLERS, + PORTAL } from '../runtimeHelpers' import { getInnerRange, isVSlot, toValidAssetId } from '../utils' import { buildSlots } from './vSlot' @@ -38,6 +39,7 @@ export const transformElement: NodeTransform = (node, context) => { if ( node.tagType === ElementTypes.ELEMENT || node.tagType === ElementTypes.COMPONENT || + node.tagType === ElementTypes.PORTAL || //