From d52ffaa20201f9a15861570bc69d3251d89d441b Mon Sep 17 00:00:00 2001 From: Evan You Date: Wed, 26 Feb 2020 15:05:11 -0500 Subject: [PATCH] refactor(compiler-ssr): extract portal processing + emit errors --- .../compiler-ssr/src/ssrCodegenTransform.ts | 5 ++ .../src/transforms/ssrTransformComponent.ts | 72 +++++++++++-------- 2 files changed, 48 insertions(+), 29 deletions(-) diff --git a/packages/compiler-ssr/src/ssrCodegenTransform.ts b/packages/compiler-ssr/src/ssrCodegenTransform.ts index b45f0a95..5afe4b55 100644 --- a/packages/compiler-ssr/src/ssrCodegenTransform.ts +++ b/packages/compiler-ssr/src/ssrCodegenTransform.ts @@ -60,6 +60,11 @@ function createSSRTransformContext( body, helpers, withSlotScopeId, + onError: + options.onError || + (e => { + throw e + }), helper(name: T): T { helpers.add(name) return name diff --git a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts index 87c6988b..e81e13a4 100644 --- a/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts +++ b/packages/compiler-ssr/src/transforms/ssrTransformComponent.ts @@ -42,6 +42,7 @@ import { processChildrenAsStatement } from '../ssrCodegenTransform' import { isSymbol, isObject, isArray } from '@vue/shared' +import { createSSRCompilerError, SSRErrorCodes } from '../errors' // We need to construct the slot functions in the 1st pass to ensure proper // scope tracking, but the children of each slot cannot be processed until @@ -136,35 +137,7 @@ export function ssrProcessComponent( const component = componentTypeMap.get(node)! if (component === PORTAL) { - const targetProp = findProp(node, 'target') - if (!targetProp) return - - let target: JSChildNode - if (targetProp.type === NodeTypes.ATTRIBUTE && targetProp.value) { - target = createSimpleExpression(targetProp.value.content, true) - } else if (targetProp.type === NodeTypes.DIRECTIVE && targetProp.exp) { - target = targetProp.exp - } else { - return - } - - const contentRenderFn = createFunctionExpression( - [`_push`], - undefined, // Body is added later - true, // newline - false, // isSlot - node.loc - ) - contentRenderFn.body = processChildrenAsStatement(node.children, context) - context.pushStatement( - createCallExpression(context.helper(SSR_RENDER_PORTAL), [ - contentRenderFn, - target, - `_parent` - ]) - ) - - return + return ssrProcessPortal(node, context) } const needFragmentWrapper = @@ -194,6 +167,47 @@ export function ssrProcessComponent( } } +function ssrProcessPortal(node: ComponentNode, context: SSRTransformContext) { + const targetProp = findProp(node, 'target') + if (!targetProp) { + context.onError( + createSSRCompilerError(SSRErrorCodes.X_SSR_NO_PORTAL_TARGET, node.loc) + ) + return + } + + let target: JSChildNode + if (targetProp.type === NodeTypes.ATTRIBUTE && targetProp.value) { + target = createSimpleExpression(targetProp.value.content, true) + } else if (targetProp.type === NodeTypes.DIRECTIVE && targetProp.exp) { + target = targetProp.exp + } else { + context.onError( + createSSRCompilerError( + SSRErrorCodes.X_SSR_NO_PORTAL_TARGET, + targetProp.loc + ) + ) + return + } + + const contentRenderFn = createFunctionExpression( + [`_push`], + undefined, // Body is added later + true, // newline + false, // isSlot + node.loc + ) + contentRenderFn.body = processChildrenAsStatement(node.children, context) + context.pushStatement( + createCallExpression(context.helper(SSR_RENDER_PORTAL), [ + contentRenderFn, + target, + `_parent` + ]) + ) +} + export const rawOptionsMap = new WeakMap() const [baseNodeTransforms, baseDirectiveTransforms] = getBaseTransformPreset(