-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
/
ssrTransformTeleport.ts
71 lines (67 loc) · 1.74 KB
/
ssrTransformTeleport.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import {
type ComponentNode,
type ExpressionNode,
NodeTypes,
createCallExpression,
createFunctionExpression,
createSimpleExpression,
findProp,
} from '@vue/compiler-dom'
import {
type SSRTransformContext,
processChildrenAsStatement,
} from '../ssrCodegenTransform'
import { SSRErrorCodes, createSSRCompilerError } from '../errors'
import { SSR_RENDER_TELEPORT } from '../runtimeHelpers'
// Note: this is a 2nd-pass codegen transform.
export function ssrProcessTeleport(
node: ComponentNode,
context: SSRTransformContext,
): void {
const targetProp = findProp(node, 'to')
if (!targetProp) {
context.onError(
createSSRCompilerError(SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET, node.loc),
)
return
}
let target: ExpressionNode | undefined
if (targetProp.type === NodeTypes.ATTRIBUTE) {
target =
targetProp.value && createSimpleExpression(targetProp.value.content, true)
} else {
target = targetProp.exp
}
if (!target) {
context.onError(
createSSRCompilerError(
SSRErrorCodes.X_SSR_NO_TELEPORT_TARGET,
targetProp.loc,
),
)
return
}
const disabledProp = findProp(node, 'disabled', false, true /* allow empty */)
const disabled = disabledProp
? disabledProp.type === NodeTypes.ATTRIBUTE
? `true`
: disabledProp.exp || `false`
: `false`
const contentRenderFn = createFunctionExpression(
[`_push`],
undefined, // Body is added later
true, // newline
false, // isSlot
node.loc,
)
contentRenderFn.body = processChildrenAsStatement(node, context)
context.pushStatement(
createCallExpression(context.helper(SSR_RENDER_TELEPORT), [
`_push`,
contentRenderFn,
target,
disabled,
`_parent`,
]),
)
}