/
vSlot.ts
411 lines (387 loc) · 10.3 KB
/
vSlot.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import {
type BinaryExpression,
binaryExpression,
callExpression,
identifier,
objectExpression,
objectProperty,
stringLiteral,
} from '@babel/types'
import { isString } from '@vue/shared'
import {
type ComponentNode,
type CompoundExpressionNode,
type DirectiveNode,
ElementTypes,
ErrorCodes,
type ExpressionNode,
NodeTypes,
type TemplateChildNode,
type TemplateNode,
createCompilerError,
createCompoundExpression,
createSimpleExpression,
findDir,
findProp,
isStaticExp,
isTemplateNode,
locStub,
} from '@vue/compiler-core'
import { SLOT_DEFAULT_NAME, dynamicSlotName } from '@dcloudio/uni-shared'
import {
createBindDirectiveNode,
isUserComponent,
} from '@dcloudio/uni-cli-shared'
import { WITH_SCOPED_SLOT } from '../runtimeHelpers'
import { parseExpr } from '../ast'
import { genExpr } from '../codegen'
import type { CodegenScope, CodegenVForScope } from '../options'
import {
type NodeTransform,
type TransformContext,
isVForScope,
} from '../transform'
import {
ATTR_VUE_ID,
ATTR_VUE_SLOTS,
SCOPED_SLOT_IDENTIFIER,
rewriteExpression,
rewriteExpressionWithoutProperty,
} from './utils'
import { createVForArrowFunctionExpression } from './vFor'
import { DYNAMIC_SLOT } from '../runtimeHelpers'
export const transformSlot: NodeTransform = (node, context) => {
if (!isUserComponent(node, context as any)) {
return
}
const { tag, children } = node
const slots = new Set<string | ExpressionNode>()
const onComponentSlot = findDir(node, 'slot', true)
const implicitDefaultChildren: TemplateChildNode[] = []
const isMiniProgramComponent = context.isMiniProgramComponent(tag)
for (let i = 0; i < children.length; i++) {
const slotElement = children[i]
let slotDir: DirectiveNode | undefined
if (
!isTemplateNode(slotElement) ||
!(slotDir = findDir(slotElement, 'slot', true))
) {
// not a <template v-slot>, skip.
if (slotElement.type !== NodeTypes.COMMENT) {
implicitDefaultChildren.push(slotElement)
}
continue
}
if (onComponentSlot) {
// already has on-component slot - this is incorrect usage.
context.onError(
createCompilerError(ErrorCodes.X_V_SLOT_MIXED_SLOT_USAGE, slotDir.loc)
)
break
}
if (!slotDir.arg) {
// v-slot => v-slot:default
slotDir.arg = createSimpleExpression('default', true)
}
const slotName = transformTemplateSlotElement(
slotDir,
slotElement,
node,
context
)
// 小程序组件默认插槽,直接移除<template #default>节点
if (isMiniProgramComponent) {
if (slotName === 'default' && slotElement.children.length === 1) {
children.splice(i, 1, slotElement.children[0])
}
continue
}
if (slotName) {
slots.add(slotName)
}
}
if (isMiniProgramComponent) {
return
}
if (implicitDefaultChildren.length) {
slots.add(SLOT_DEFAULT_NAME)
}
if (onComponentSlot) {
// <unicloud-db v-slot:default="{data, loading, error, options}"/>
// => <unicloud-db collection=""><template v-slot:default="{data, loading, error, options}"/></unicloud-db>
slots.add(SLOT_DEFAULT_NAME)
const templateNode = createTemplateNode(
onComponentSlot,
implicitDefaultChildren
)
transformTemplateSlotElement(onComponentSlot, templateNode, node, context)
node.children = [templateNode]
}
// 不支持 $slots, 则自动补充 props
if (slots.size && !context.miniProgram.slot.$slots) {
const slotsArr = [...slots]
const hasDynamic = slotsArr.find((name) => !isString(name))
let value: string | ExpressionNode
if (hasDynamic) {
const children: (string | ExpressionNode)[] = []
const len = slotsArr.length - 1
slotsArr.forEach((name, index) => {
if (isString(name)) {
children.push(`'${dynamicSlotName(name)}'`)
} else {
children.push(name)
}
if (index < len) {
children.push(',')
}
})
value = createCompoundExpression([
context.helperString(DYNAMIC_SLOT) + '([',
...children,
'])',
])
} else {
value = `[${slotsArr
.map((name) => `'${dynamicSlotName(name as string)}'`)
.join(',')}]`
}
node.props.unshift(createBindDirectiveNode(ATTR_VUE_SLOTS, value))
}
}
export function rewriteVSlot(dir: DirectiveNode, context: TransformContext) {
dir.arg = rewriteExpression(
createCompoundExpression([
context.helperString(DYNAMIC_SLOT) + '(',
dir.arg!,
')',
]),
context
)
}
function transformTemplateSlotElement(
slotDir: DirectiveNode,
slotTemplate: TemplateNode,
slotComponent: ComponentNode,
context: TransformContext
) {
const slotName = findSlotName(slotDir)
if (!slotName) {
return
}
const { exp } = slotDir
// non scoped slots
if (!exp) {
return slotName
}
// empty
if (exp.type === NodeTypes.SIMPLE_EXPRESSION && !exp.content.trim()) {
return slotName
}
// 使用vFor来简单处理scoped slot作用域问题
slotTemplate.children = [
createVForTemplate(
slotTemplate,
{ name: slotName, value: genExpr(exp), slotComponent },
context
),
]
if (context.miniProgram.slot.dynamicSlotNames) {
// 已经在 vFor 中补充 slot,故需要移除 slotTemplate 中的
const index = slotTemplate.props.indexOf(slotDir)
if (index > -1) {
slotTemplate.props.splice(index, 1)
}
}
// v-slot="slotProps" => v-slot 避免 transformIdentifier 生成 slotProps 的变量声明
slotDir.exp = undefined
return slotName
}
function createTemplateNode(
slotDir: DirectiveNode,
children: TemplateChildNode[]
): TemplateNode {
return {
type: NodeTypes.ELEMENT,
tag: 'template',
tagType: ElementTypes.TEMPLATE,
loc: locStub,
isSelfClosing: false,
codegenNode: undefined,
ns: 0,
props: [slotDir],
children,
}
}
export function findSlotName(slotDir: DirectiveNode) {
if (!slotDir.arg) {
return SLOT_DEFAULT_NAME
}
if (isStaticExp(slotDir.arg)) {
return slotDir.arg.content
}
return slotDir.arg
}
function findCurrentVForValueAlias(context: TransformContext) {
let scope: CodegenScope | null = context.currentScope
while (scope) {
if (isVForScope(scope)) {
return scope.valueAlias
}
scope = scope.parent
}
return ''
}
function createVForTemplate(
slotElement: TemplateNode,
{
name,
value,
slotComponent,
}: {
name: string | ExpressionNode
value: string
slotComponent: ComponentNode
},
context: TransformContext
) {
const slotName = 's' + context.scopes.vFor
const keyProp: DirectiveNode = createBindDirectiveNode(
'key',
'i' + context.scopes.vFor
)
const source = isString(name) ? `'${name}'` : genExpr(name)
const vForProp: DirectiveNode = {
type: NodeTypes.DIRECTIVE,
name: 'for',
loc: locStub,
modifiers: [],
arg: undefined,
exp: createSimpleExpression(
`(${value}, ${slotName}) in ${SCOPED_SLOT_IDENTIFIER}(${source}, ${
findCurrentVForValueAlias(context) || `''`
})`
),
}
const props = [vForProp, keyProp]
if (context.miniProgram.slot.dynamicSlotNames) {
props.push(createBindDirectiveNode('slot', slotName))
}
return {
loc: slotElement.loc,
ns: 0,
tag: 'template',
type: NodeTypes.ELEMENT,
tagType: ElementTypes.TEMPLATE,
props,
isSelfClosing: false,
codegenNode: undefined,
children: slotElement.children,
slotComponent,
} as TemplateNode
}
const slotNameRE = /\('(.*)',/
/**
* ('default','') => default
* @param source
* @returns
*/
function findCurrentSlotName(source: ExpressionNode) {
return stringLiteral(
dynamicSlotName(
((source as CompoundExpressionNode).children[1] as string).match(
slotNameRE
)![1]
)
)
}
function createPathBinaryExpr(
scope: CodegenVForScope,
computed: boolean = true
) {
return binaryExpression(
'+',
binaryExpression(
'+',
stringLiteral(parseVForPath(scope.sourceAlias) + (computed ? '[' : '.')),
identifier(scope.indexAlias)
),
stringLiteral(computed ? '].' : '.')
)
}
export function findCurrentPath(id: string, scope: CodegenScope) {
let parent = scope.parent
let binaryExpr: BinaryExpression | null = null
while (parent) {
if (isVForScope(parent)) {
// const computed = !isScopedSlotVFor(parent)
if (!binaryExpr) {
binaryExpr = createPathBinaryExpr(parent)
} else {
binaryExpr = binaryExpression(
'+',
createPathBinaryExpr(parent),
binaryExpr
)
}
}
parent = parent.parent
}
return (
(binaryExpr && binaryExpression('+', binaryExpr, stringLiteral(id))) ||
stringLiteral(id)
)
}
function findCurrentVueIdExpr(node: ComponentNode, context: TransformContext) {
if (!node) {
return stringLiteral('')
}
const vueIdProp = findProp(node, ATTR_VUE_ID)!
if (vueIdProp.type === NodeTypes.ATTRIBUTE) {
return stringLiteral(vueIdProp.value!.content)
}
return parseExpr(genExpr(vueIdProp.exp!), context) || stringLiteral('')
}
/**
* 目前无用
* @param vForScope
* @param parentScope
* @param context
*/
export function rewriteScopedSlotVForScope(
vForScope: CodegenVForScope,
parentScope: CodegenScope,
context: TransformContext
) {
// 生成一个新的sourceAlias,用于scopedSlots
const { source, sourceExpr } = vForScope
vForScope.sourceAlias = rewriteExpressionWithoutProperty(
source,
context,
sourceExpr,
parentScope
).content
}
function parseVForPath(id: string) {
return id.includes('.') ? id.split('.')[1] : id
}
export function createVSlotCallExpression(
slotComponent: ComponentNode,
vForScope: CodegenVForScope,
context: TransformContext
) {
const { source, sourceAlias } = vForScope
const id = parseVForPath(sourceAlias)
return callExpression(identifier(context.helperString(WITH_SCOPED_SLOT)), [
createVForArrowFunctionExpression(vForScope),
objectExpression([
// 插槽名称,数据更新 path,vueId
objectProperty(identifier('name'), findCurrentSlotName(source)),
// 暂不生成 path
objectProperty(identifier('path'), findCurrentPath(id, vForScope)),
objectProperty(
identifier('vueId'),
findCurrentVueIdExpr(slotComponent, context)
),
]),
])
}