@dcloudio/uni-template-compiler 2.0.1-34920220609002 → 2.0.1-35320220729002
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/app/parser/base-parser.js +8 -0
- package/lib/app/service.js +2 -0
- package/lib/app/util.js +2 -0
- package/lib/app/view.js +2 -0
- package/lib/constants.js +2 -0
- package/lib/module.js +6 -5
- package/lib/script/traverse/data/class.js +32 -28
- package/lib/script/traverse/data/style.js +26 -9
- package/lib/script/traverse/data/util.js +8 -8
- package/lib/script/traverse/render-slot.js +1 -1
- package/lib/script/traverse/resolve-scoped-slots.js +7 -2
- package/lib/script/traverse/visitor.js +6 -2
- package/lib/template/generate.js +23 -3
- package/lib/template/traverse.js +50 -34
- package/lib/util.js +9 -2
- package/package.json +2 -2
|
@@ -2,6 +2,7 @@ const {
|
|
|
2
2
|
ID,
|
|
3
3
|
C_IS,
|
|
4
4
|
C_REF,
|
|
5
|
+
C_NAME,
|
|
5
6
|
V_IF,
|
|
6
7
|
V_FOR,
|
|
7
8
|
V_ELSE_IF,
|
|
@@ -16,6 +17,12 @@ function parseRef (el, genVar) {
|
|
|
16
17
|
}
|
|
17
18
|
}
|
|
18
19
|
|
|
20
|
+
function parseSlotName (el, genVar) {
|
|
21
|
+
if (el.slotName && isVar(el.slotName)) {
|
|
22
|
+
el.slotName = genVar(C_NAME, el.slotName)
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
19
26
|
function parseIs (el, genVar) {
|
|
20
27
|
if (!el.component) {
|
|
21
28
|
return
|
|
@@ -106,6 +113,7 @@ function parseText (el, parent, state) {
|
|
|
106
113
|
module.exports = {
|
|
107
114
|
parseIs,
|
|
108
115
|
parseRef,
|
|
116
|
+
parseSlotName,
|
|
109
117
|
parseIf,
|
|
110
118
|
parseFor,
|
|
111
119
|
parseText,
|
package/lib/app/service.js
CHANGED
|
@@ -18,6 +18,7 @@ const {
|
|
|
18
18
|
const {
|
|
19
19
|
parseIs,
|
|
20
20
|
parseRef,
|
|
21
|
+
parseSlotName,
|
|
21
22
|
parseIf,
|
|
22
23
|
parseFor,
|
|
23
24
|
parseText,
|
|
@@ -137,6 +138,7 @@ function transformNode (el, parent, state, isScopedSlot) {
|
|
|
137
138
|
|
|
138
139
|
parseIs(el, genVar)
|
|
139
140
|
parseRef(el, genVar)
|
|
141
|
+
parseSlotName(el, genVar)
|
|
140
142
|
parseFor(el, createGenVar, isScopedSlot, checkAutoFill(el))
|
|
141
143
|
parseKey(el, isScopedSlot)
|
|
142
144
|
|
package/lib/app/util.js
CHANGED
|
@@ -15,6 +15,7 @@ const GET_CHANGE_DATA = '_$gc' // wxs
|
|
|
15
15
|
const C_IS = 'is'
|
|
16
16
|
const C_SLOT_TARGET = 'st'
|
|
17
17
|
const C_REF = 'ref'
|
|
18
|
+
const C_NAME = 'name'
|
|
18
19
|
|
|
19
20
|
const V_FOR = 'f'
|
|
20
21
|
const V_IF = 'i'
|
|
@@ -268,6 +269,7 @@ function addHandler (el, name, value, important) {
|
|
|
268
269
|
module.exports = {
|
|
269
270
|
C_IS,
|
|
270
271
|
C_REF,
|
|
272
|
+
C_NAME,
|
|
271
273
|
V_FOR,
|
|
272
274
|
V_IF,
|
|
273
275
|
V_ELSE_IF,
|
package/lib/app/view.js
CHANGED
|
@@ -13,6 +13,7 @@ const {
|
|
|
13
13
|
const {
|
|
14
14
|
parseIs,
|
|
15
15
|
parseRef,
|
|
16
|
+
parseSlotName,
|
|
16
17
|
parseIf,
|
|
17
18
|
parseFor,
|
|
18
19
|
parseText,
|
|
@@ -130,6 +131,7 @@ function transformNode (el, parent, state, isScopedSlot) {
|
|
|
130
131
|
|
|
131
132
|
parseIs(el, genVar)
|
|
132
133
|
parseRef(el, genVar)
|
|
134
|
+
parseSlotName(el, genVar)
|
|
133
135
|
if (parseFor(el, createGenVar, isScopedSlot)) {
|
|
134
136
|
if (el.alias[0] === '{') { // <div><li v-for=" { a, b } in items"></li></div>
|
|
135
137
|
el.alias = '$item'
|
package/lib/constants.js
CHANGED
|
@@ -66,6 +66,8 @@ module.exports = {
|
|
|
66
66
|
ATTR_DATA_EVENT_PARAMS: 'data-event-params',
|
|
67
67
|
ATTR_DATA_EVENT_LIST: 'data-event-list',
|
|
68
68
|
ATTE_DATA_CUSTOM_HIDDEN: 'data-custom-hidden',
|
|
69
|
+
VIRTUAL_HOST_STYLE: 'virtualHostStyle',
|
|
70
|
+
VIRTUAL_HOST_CLASS: 'virtualHostClass',
|
|
69
71
|
INTERNAL_GET_ORIG,
|
|
70
72
|
INTERNAL_GET_CLASS,
|
|
71
73
|
INTERNAL_GET_STYLE,
|
package/lib/module.js
CHANGED
|
@@ -15,12 +15,13 @@ module.exports = {
|
|
|
15
15
|
preTransformNode (el, {
|
|
16
16
|
warn
|
|
17
17
|
}) {
|
|
18
|
-
|
|
18
|
+
const attrsMap = el.attrsMap
|
|
19
|
+
if (el.tag === 'slot' && !(attrsMap.name || attrsMap[':name'])) {
|
|
19
20
|
el.attrsList.push({
|
|
20
21
|
name: 'SLOT_DEFAULT',
|
|
21
22
|
value: true
|
|
22
23
|
})
|
|
23
|
-
|
|
24
|
+
attrsMap.SLOT_DEFAULT = true
|
|
24
25
|
}
|
|
25
26
|
// 处理 attr
|
|
26
27
|
el.attrsList.forEach(attr => {
|
|
@@ -31,11 +32,11 @@ module.exports = {
|
|
|
31
32
|
const origName = attr.name
|
|
32
33
|
const newName = origName.replace('.lazy', '')
|
|
33
34
|
attr.name = newName
|
|
34
|
-
|
|
35
|
-
delete
|
|
35
|
+
attrsMap[newName] = attr.value
|
|
36
|
+
delete attrsMap[origName]
|
|
36
37
|
} else if (onRE.test(attr.name) && !attr.value.trim()) { // 事件为空
|
|
37
38
|
attr.value = '__HOLDER__'
|
|
38
|
-
|
|
39
|
+
attrsMap[attr.name] = attr.value
|
|
39
40
|
}
|
|
40
41
|
})
|
|
41
42
|
// 暂不支持的指令
|
|
@@ -2,7 +2,12 @@ const t = require('@babel/types')
|
|
|
2
2
|
const uniI18n = require('@dcloudio/uni-cli-i18n')
|
|
3
3
|
|
|
4
4
|
const {
|
|
5
|
-
|
|
5
|
+
VIRTUAL_HOST_CLASS
|
|
6
|
+
} = require('../../../constants')
|
|
7
|
+
|
|
8
|
+
const {
|
|
9
|
+
getCode,
|
|
10
|
+
isRootElement
|
|
6
11
|
} = require('../../../util')
|
|
7
12
|
|
|
8
13
|
function processClassArrayExpressionElements (classArrayExpression) {
|
|
@@ -76,45 +81,29 @@ function processClassArrayExpression (classValuePath) {
|
|
|
76
81
|
module.exports = function processClass (paths, path, state) {
|
|
77
82
|
const classPath = paths.class
|
|
78
83
|
const staticClassPath = paths.staticClass
|
|
84
|
+
const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
|
|
85
|
+
let classArrayExpression
|
|
79
86
|
if (classPath) {
|
|
80
87
|
const classValuePath = classPath.get('value')
|
|
81
88
|
if (classValuePath.isObjectExpression()) { // object
|
|
82
|
-
classValuePath
|
|
83
|
-
processStaticClass(
|
|
84
|
-
processClassObjectExpression(classValuePath),
|
|
85
|
-
staticClassPath,
|
|
86
|
-
state
|
|
87
|
-
)
|
|
88
|
-
)
|
|
89
|
+
classArrayExpression = processClassObjectExpression(classValuePath)
|
|
89
90
|
} else if (classValuePath.isArrayExpression()) { // array
|
|
90
|
-
classValuePath
|
|
91
|
-
processStaticClass(
|
|
92
|
-
processClassArrayExpression(classValuePath),
|
|
93
|
-
staticClassPath,
|
|
94
|
-
state
|
|
95
|
-
)
|
|
96
|
-
)
|
|
91
|
+
classArrayExpression = processClassArrayExpression(classValuePath)
|
|
97
92
|
} else if (
|
|
98
93
|
classValuePath.isStringLiteral() || // :class="'a'"
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
94
|
+
classValuePath.isIdentifier() || // TODO 需要优化到下一个条件,:class="classObject"
|
|
95
|
+
classValuePath.isMemberExpression() || // 需要优化到下一个条件,:class="item.classObject"
|
|
96
|
+
classValuePath.isConditionalExpression() ||
|
|
97
|
+
classValuePath.isLogicalExpression() ||
|
|
98
|
+
classValuePath.isBinaryExpression()
|
|
104
99
|
) {
|
|
105
100
|
// 理论上 ConditionalExpression,LogicalExpression 可能存在 classObject,应该__get_class,还是先不考虑这种情况吧
|
|
106
101
|
// ConditionalExpression :class="index === currentIndex ? activeStyle : itemStyle"
|
|
107
102
|
// BinaryExpression :class="'m-content-head-'+message.user"
|
|
108
|
-
classValuePath.
|
|
109
|
-
processStaticClass(
|
|
110
|
-
t.arrayExpression([classValuePath.node]),
|
|
111
|
-
staticClassPath,
|
|
112
|
-
state
|
|
113
|
-
)
|
|
114
|
-
)
|
|
103
|
+
classArrayExpression = t.arrayExpression([classValuePath.node])
|
|
115
104
|
} else if (
|
|
116
105
|
classValuePath.isIdentifier() ||
|
|
117
|
-
|
|
106
|
+
classValuePath.isMemberExpression()
|
|
118
107
|
) { // classObject :class="classObject" :class="vm.classObject"
|
|
119
108
|
// TODO 目前先不考虑 classObject,styleObject
|
|
120
109
|
|
|
@@ -135,5 +124,20 @@ module.exports = function processClass (paths, path, state) {
|
|
|
135
124
|
state.errors.add(':class' + uniI18n.__('templateCompiler.noSupportSyntax', { 0: getCode(classValuePath.node) }))
|
|
136
125
|
}
|
|
137
126
|
}
|
|
127
|
+
if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
|
|
128
|
+
const virtualHostClass = t.identifier(VIRTUAL_HOST_CLASS)
|
|
129
|
+
if (classArrayExpression) {
|
|
130
|
+
classArrayExpression.elements.push(virtualHostClass)
|
|
131
|
+
} else {
|
|
132
|
+
classArrayExpression = t.arrayExpression([virtualHostClass])
|
|
133
|
+
const property = t.objectProperty(t.identifier('class'), processStaticClass(classArrayExpression, staticClassPath, state))
|
|
134
|
+
path.node.properties.push(property)
|
|
135
|
+
return []
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
if (classArrayExpression) {
|
|
139
|
+
const classValuePath = classPath.get('value')
|
|
140
|
+
classValuePath.replaceWith(processStaticClass(classArrayExpression, staticClassPath, state))
|
|
141
|
+
}
|
|
138
142
|
return []
|
|
139
143
|
}
|
|
@@ -2,12 +2,14 @@ const t = require('@babel/types')
|
|
|
2
2
|
|
|
3
3
|
const {
|
|
4
4
|
IDENTIFIER_STYLE,
|
|
5
|
-
INTERNAL_GET_STYLE
|
|
5
|
+
INTERNAL_GET_STYLE,
|
|
6
|
+
VIRTUAL_HOST_STYLE
|
|
6
7
|
} = require('../../../constants')
|
|
7
8
|
|
|
8
9
|
const {
|
|
9
10
|
getCode,
|
|
10
|
-
hyphenate
|
|
11
|
+
hyphenate,
|
|
12
|
+
isRootElement
|
|
11
13
|
} = require('../../../util')
|
|
12
14
|
|
|
13
15
|
const getMemberExpr = require('../member-expr')
|
|
@@ -120,6 +122,7 @@ function generateGetStyle (stylePath, styleValuePath, staticStylePath, state) {
|
|
|
120
122
|
module.exports = function processStyle (paths, path, state) {
|
|
121
123
|
const stylePath = paths.style
|
|
122
124
|
const staticStylePath = paths.staticStyle
|
|
125
|
+
const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
|
|
123
126
|
if (stylePath) {
|
|
124
127
|
const styleValuePath = stylePath.get('value')
|
|
125
128
|
if (styleValuePath.isObjectExpression()) {
|
|
@@ -146,11 +149,11 @@ module.exports = function processStyle (paths, path, state) {
|
|
|
146
149
|
}
|
|
147
150
|
} else if (
|
|
148
151
|
styleValuePath.isStringLiteral() || // :style="'background:red'"
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
152
|
+
styleValuePath.isIdentifier() || // TODO 需要优化到下一个条件,:style="styleObject"
|
|
153
|
+
styleValuePath.isMemberExpression() || // TODO 需要优化到下一个条件,:style="item.styleObject"
|
|
154
|
+
styleValuePath.isConditionalExpression() ||
|
|
155
|
+
styleValuePath.isLogicalExpression() ||
|
|
156
|
+
styleValuePath.isBinaryExpression()
|
|
154
157
|
) {
|
|
155
158
|
// 理论上 ConditionalExpression,LogicalExpression 可能存在 styleObject,应该__get_style,还是先不考虑这种情况吧
|
|
156
159
|
// ConditionalExpression :style="index === currentIndex ? activeStyle : itemStyle"
|
|
@@ -164,14 +167,28 @@ module.exports = function processStyle (paths, path, state) {
|
|
|
164
167
|
)
|
|
165
168
|
} else if (
|
|
166
169
|
styleValuePath.isIdentifier() ||
|
|
167
|
-
|
|
170
|
+
styleValuePath.isMemberExpression()
|
|
168
171
|
) { // TODO 目前先不考虑 classObject,styleObject
|
|
169
172
|
// generateGetStyle(stylePath, styleValuePath, staticStylePath, state)
|
|
170
173
|
} else {
|
|
171
174
|
state.errors.add(`:style 不支持 ${getCode(styleValuePath.node)} 语法`)
|
|
172
175
|
}
|
|
176
|
+
if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
|
|
177
|
+
styleValuePath.replaceWith(t.binaryExpression('+', styleValuePath.node, t.identifier(VIRTUAL_HOST_STYLE)))
|
|
178
|
+
}
|
|
173
179
|
} else if (staticStylePath) {
|
|
180
|
+
if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
|
|
181
|
+
const styleNode = processStaticStyle([t.identifier(VIRTUAL_HOST_STYLE)], staticStylePath, state)
|
|
182
|
+
const property = t.objectProperty(t.identifier('style'), styleNode)
|
|
183
|
+
path.node.properties.push(property)
|
|
184
|
+
return []
|
|
185
|
+
}
|
|
174
186
|
staticStylePath.get('value').replaceWith(getStaticStyleStringLiteral(staticStylePath, state))
|
|
187
|
+
} else {
|
|
188
|
+
if (mergeVirtualHostAttributes && isRootElement(path.parentPath)) {
|
|
189
|
+
const property = t.objectProperty(t.identifier('style'), t.identifier(VIRTUAL_HOST_STYLE))
|
|
190
|
+
path.node.properties.push(property)
|
|
191
|
+
}
|
|
175
192
|
}
|
|
176
193
|
return []
|
|
177
|
-
}
|
|
194
|
+
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
const t = require('@babel/types')
|
|
2
|
-
const babelTraverse = require('@babel/traverse').default
|
|
3
|
-
|
|
4
|
-
const {
|
|
5
|
-
INTERNAL_SET_MODEL
|
|
6
|
-
} = require('../../../constants')
|
|
7
|
-
|
|
1
|
+
const t = require('@babel/types')
|
|
2
|
+
const babelTraverse = require('@babel/traverse').default
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
INTERNAL_SET_MODEL
|
|
6
|
+
} = require('../../../constants')
|
|
7
|
+
|
|
8
8
|
module.exports = {
|
|
9
9
|
getModelEventFunctionExpr (funcExpr, propPath, modifiers = []) {
|
|
10
10
|
let targetExpr
|
|
@@ -44,4 +44,4 @@ module.exports = {
|
|
|
44
44
|
)
|
|
45
45
|
)
|
|
46
46
|
}
|
|
47
|
-
}
|
|
47
|
+
}
|
|
@@ -32,7 +32,7 @@ module.exports = function getRenderSlot (path, state) {
|
|
|
32
32
|
const scoped = state.scoped
|
|
33
33
|
// TODO 判断是否包含作用域内变量
|
|
34
34
|
const renderSlotStatementArray = scoped && scoped.length ? scoped[scoped.length - 1].renderSlotStatementArray : state.renderSlotStatementArray
|
|
35
|
-
renderSlotStatementArray.push(t.expressionStatement(t.callExpression(t.identifier('$setScopedSlotsParams'), [
|
|
35
|
+
renderSlotStatementArray.push(t.expressionStatement(t.callExpression(t.identifier('$setScopedSlotsParams'), [name.node, valueNode])))
|
|
36
36
|
}
|
|
37
37
|
// TODO 组件嵌套
|
|
38
38
|
}
|
|
@@ -51,7 +51,12 @@ function replaceId (path, ids) {
|
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
module.exports = function getResolveScopedSlots (parent, state) {
|
|
54
|
-
|
|
54
|
+
let objectPath = parent.get('arguments.0.elements.0')
|
|
55
|
+
// TODO v-else
|
|
56
|
+
if (objectPath.isConditionalExpression()) {
|
|
57
|
+
objectPath = objectPath.get('consequent')
|
|
58
|
+
}
|
|
59
|
+
const properties = objectPath.get('properties')
|
|
55
60
|
const fn = properties.find(path => path.get('key').isIdentifier({ name: 'fn' }))
|
|
56
61
|
const params = fn.get('value.params.0')
|
|
57
62
|
if (!params) {
|
|
@@ -83,7 +88,7 @@ module.exports = function getResolveScopedSlots (parent, state) {
|
|
|
83
88
|
const test = t.callExpression(t.identifier('$hasScopedSlotsParams'), [vueId])
|
|
84
89
|
orgin.replaceWith(t.arrayExpression([t.conditionalExpression(test, node, t.callExpression(t.identifier(METHOD_CREATE_EMPTY_VNODE), []))]))
|
|
85
90
|
// scopedSlotsCompiler auto
|
|
86
|
-
|
|
91
|
+
objectPath.node.scopedSlotsCompiler = 'augmented'
|
|
87
92
|
}
|
|
88
93
|
}
|
|
89
94
|
}
|
|
@@ -24,7 +24,8 @@ const {
|
|
|
24
24
|
hyphenate,
|
|
25
25
|
traverseFilter,
|
|
26
26
|
getComponentName,
|
|
27
|
-
hasEscapeQuote
|
|
27
|
+
hasEscapeQuote,
|
|
28
|
+
isRootElement
|
|
28
29
|
} = require('../../util')
|
|
29
30
|
|
|
30
31
|
const traverseData = require('./data')
|
|
@@ -182,7 +183,10 @@ module.exports = {
|
|
|
182
183
|
if (this.options.scopeId) {
|
|
183
184
|
addStaticClass(path, this.options.scopeId)
|
|
184
185
|
}
|
|
185
|
-
|
|
186
|
+
// 根节点无 attrs 时添加空对象,方便后续合并外层 attrs
|
|
187
|
+
if (this.options.mergeVirtualHostAttributes && !t.isObjectExpression(path.node.arguments[1]) && isRootElement(path)) {
|
|
188
|
+
path.node.arguments.splice(1, 0, t.objectExpression([]))
|
|
189
|
+
}
|
|
186
190
|
const dataPath = path.get('arguments.1')
|
|
187
191
|
dataPath && dataPath.isObjectExpression() && traverseData(dataPath, this, tagNode.value)
|
|
188
192
|
}
|
package/lib/template/generate.js
CHANGED
|
@@ -4,12 +4,16 @@ const {
|
|
|
4
4
|
|
|
5
5
|
const {
|
|
6
6
|
SELF_CLOSING_TAGS,
|
|
7
|
-
INTERNAL_EVENT_LINK
|
|
7
|
+
INTERNAL_EVENT_LINK,
|
|
8
|
+
VIRTUAL_HOST_STYLE,
|
|
9
|
+
VIRTUAL_HOST_CLASS
|
|
8
10
|
} = require('../constants')
|
|
9
11
|
const uniI18n = require('@dcloudio/uni-cli-i18n')
|
|
10
12
|
|
|
11
13
|
function processElement (ast, state, isRoot) {
|
|
12
|
-
const
|
|
14
|
+
const platform = state.options.platform
|
|
15
|
+
const platformName = platform.name
|
|
16
|
+
const mergeVirtualHostAttributes = state.options.mergeVirtualHostAttributes
|
|
13
17
|
// <template slot="f"></template>
|
|
14
18
|
if (ast.type === 'template' && hasOwn(ast.attr, 'slot')) {
|
|
15
19
|
ast.type = 'view'
|
|
@@ -36,13 +40,29 @@ function processElement (ast, state, isRoot) {
|
|
|
36
40
|
}]
|
|
37
41
|
delete ast.attr.innerHTML
|
|
38
42
|
}
|
|
39
|
-
if (
|
|
43
|
+
if (platform.isComponent(ast.type)) {
|
|
40
44
|
if (platformName === 'mp-alipay') {
|
|
41
45
|
ast.attr.onVueInit = INTERNAL_EVENT_LINK
|
|
42
46
|
} else if (platformName !== 'mp-baidu') {
|
|
43
47
|
ast.attr['bind:' + INTERNAL_EVENT_LINK] = INTERNAL_EVENT_LINK
|
|
44
48
|
}
|
|
45
49
|
|
|
50
|
+
if (mergeVirtualHostAttributes && platform.isComponent(ast.type)) {
|
|
51
|
+
const obj = {
|
|
52
|
+
style: VIRTUAL_HOST_STYLE,
|
|
53
|
+
class: VIRTUAL_HOST_CLASS
|
|
54
|
+
}
|
|
55
|
+
Object.keys(obj).forEach(key => {
|
|
56
|
+
if (key in ast.attr) {
|
|
57
|
+
ast.attr[obj[key]] = ast.attr[key]
|
|
58
|
+
}
|
|
59
|
+
// 支付宝小程序自定义组件外部属性始终无效
|
|
60
|
+
if (platformName === 'mp-alipay') {
|
|
61
|
+
delete ast.attr[key]
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
|
|
46
66
|
const children = ast.children
|
|
47
67
|
// default slot
|
|
48
68
|
let defaultSlot = false
|
package/lib/template/traverse.js
CHANGED
|
@@ -270,7 +270,7 @@ function traverseArrayExpression (arrayExprNodes, state) {
|
|
|
270
270
|
}, [])
|
|
271
271
|
}
|
|
272
272
|
|
|
273
|
-
function genSlotNode (slotName, slotNode, fallbackNodes, state) {
|
|
273
|
+
function genSlotNode (slotName, slotNode, fallbackNodes, state, isStaticSlotName = true) {
|
|
274
274
|
if (!fallbackNodes || t.isNullLiteral(fallbackNodes)) {
|
|
275
275
|
return slotNode
|
|
276
276
|
}
|
|
@@ -282,7 +282,7 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
|
|
|
282
282
|
return [{
|
|
283
283
|
type: 'block',
|
|
284
284
|
attr: {
|
|
285
|
-
[prefix + 'if']: '{{$slots.' + slotName + '}}'
|
|
285
|
+
[prefix + 'if']: isStaticSlotName ? '{{$slots.' + slotName + '}}' : '{{$slots[' + slotName.replace(/^{{/, '').replace(/}}$/, '') + ']}}'
|
|
286
286
|
},
|
|
287
287
|
children: [].concat(slotNode)
|
|
288
288
|
}, {
|
|
@@ -297,12 +297,9 @@ function genSlotNode (slotName, slotNode, fallbackNodes, state) {
|
|
|
297
297
|
}
|
|
298
298
|
|
|
299
299
|
function traverseRenderSlot (callExprNode, state) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
const slotName = callExprNode.arguments[0].value
|
|
300
|
+
const slotNameNode = callExprNode.arguments[0]
|
|
301
|
+
const isStaticSlotName = t.isStringLiteral(slotNameNode)
|
|
302
|
+
const slotName = isStaticSlotName ? slotNameNode.value : genCode(slotNameNode)
|
|
306
303
|
|
|
307
304
|
let deleteSlotName = false // 标记是否组件 slot 手动指定了 name="default"
|
|
308
305
|
if (state.options.scopedSlotsCompiler !== 'augmented' && callExprNode.arguments.length > 2) { // 作用域插槽
|
|
@@ -312,6 +309,10 @@ function traverseRenderSlot (callExprNode, state) {
|
|
|
312
309
|
})
|
|
313
310
|
deleteSlotName = props.SLOT_DEFAULT && Object.keys(props).length === 1
|
|
314
311
|
if (!deleteSlotName) {
|
|
312
|
+
if (!isStaticSlotName) {
|
|
313
|
+
state.errors.add(uniI18n.__('templateCompiler.notSupportDynamicSlotName', { 0: 'v-slot' }))
|
|
314
|
+
return
|
|
315
|
+
}
|
|
315
316
|
delete props.SLOT_DEFAULT
|
|
316
317
|
return genSlotNode(
|
|
317
318
|
slotName,
|
|
@@ -334,28 +335,50 @@ function traverseRenderSlot (callExprNode, state) {
|
|
|
334
335
|
delete node.attr.name
|
|
335
336
|
}
|
|
336
337
|
|
|
337
|
-
return genSlotNode(slotName, node, callExprNode.arguments[1], state)
|
|
338
|
+
return genSlotNode(slotName, node, callExprNode.arguments[1], state, isStaticSlotName)
|
|
338
339
|
}
|
|
339
340
|
|
|
340
341
|
function traverseResolveScopedSlots (callExprNode, state) {
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
if (
|
|
348
|
-
|
|
342
|
+
const options = state.options
|
|
343
|
+
const vIfAttrName = options.platform.directive + 'if'
|
|
344
|
+
function single (node, slotName, vIfCode, ignore) {
|
|
345
|
+
let last = node
|
|
346
|
+
const vIfs = vIfCode ? [vIfCode] : []
|
|
347
|
+
function find (children) {
|
|
348
|
+
if (Array.isArray(children) && children.length === 1) {
|
|
349
|
+
const child = children[0]
|
|
350
|
+
if (!child.type) {
|
|
351
|
+
return
|
|
352
|
+
}
|
|
353
|
+
last = child
|
|
354
|
+
if (child.attr && child.attr[vIfAttrName]) {
|
|
355
|
+
vIfs.push(child.attr[vIfAttrName])
|
|
356
|
+
delete child.attr[vIfAttrName]
|
|
357
|
+
}
|
|
358
|
+
if (ignore.includes(child.type) && !(child.attr && Object.keys(child.attr).length)) {
|
|
359
|
+
find(child.children, ignore)
|
|
360
|
+
}
|
|
349
361
|
}
|
|
350
|
-
child.attr = child.attr || {}
|
|
351
|
-
child.attr.slot = slotName
|
|
352
|
-
return true
|
|
353
362
|
}
|
|
363
|
+
find(node.children)
|
|
364
|
+
last.attr = last.attr || {}
|
|
365
|
+
last.attr.slot = slotName
|
|
366
|
+
if (vIfs.length) {
|
|
367
|
+
// 简易合并
|
|
368
|
+
last.attr[vIfAttrName] = vIfs.length > 1 ? `{{${vIfs.map(str => str.replace(/^\{\{(.+)\}\}$/, '($1)')).join('&&')}}}` : vIfs[0]
|
|
369
|
+
}
|
|
370
|
+
return last
|
|
354
371
|
}
|
|
355
372
|
return callExprNode.arguments[0].elements.map(slotNode => {
|
|
356
373
|
let keyProperty = false
|
|
357
374
|
let fnProperty = false
|
|
358
375
|
let proxyProperty = false
|
|
376
|
+
let vIfCode
|
|
377
|
+
// TODO v-else
|
|
378
|
+
if (t.isConditionalExpression(slotNode)) {
|
|
379
|
+
vIfCode = genCode(slotNode.test)
|
|
380
|
+
slotNode = slotNode.consequent
|
|
381
|
+
}
|
|
359
382
|
slotNode.properties.forEach(property => {
|
|
360
383
|
switch (property.key.name) {
|
|
361
384
|
case 'key':
|
|
@@ -371,14 +394,14 @@ function traverseResolveScopedSlots (callExprNode, state) {
|
|
|
371
394
|
const slotName = keyProperty.value.value
|
|
372
395
|
const returnExprNodes = fnProperty.value.body.body[0].argument
|
|
373
396
|
const parentNode = callExprNode.$node
|
|
374
|
-
if (slotNode.scopedSlotsCompiler !== 'augmented' && !proxyProperty) {
|
|
375
|
-
const resourcePath =
|
|
397
|
+
if (options.scopedSlotsCompiler !== 'augmented' && slotNode.scopedSlotsCompiler !== 'augmented' && !proxyProperty) {
|
|
398
|
+
const resourcePath = options.resourcePath
|
|
376
399
|
const ownerName = path.basename(resourcePath, path.extname(resourcePath))
|
|
377
400
|
|
|
378
401
|
const parentName = parentNode.type
|
|
379
402
|
|
|
380
403
|
const paramExprNode = fnProperty.value.params[0]
|
|
381
|
-
return
|
|
404
|
+
return options.platform.resolveScopedSlots(
|
|
382
405
|
slotName, {
|
|
383
406
|
genCode,
|
|
384
407
|
generate,
|
|
@@ -398,21 +421,14 @@ function traverseResolveScopedSlots (callExprNode, state) {
|
|
|
398
421
|
state
|
|
399
422
|
)
|
|
400
423
|
}
|
|
401
|
-
if (
|
|
424
|
+
if (options.scopedSlotsCompiler === 'auto' && slotNode.scopedSlotsCompiler === 'augmented') {
|
|
402
425
|
parentNode.attr['scoped-slots-compiler'] = 'augmented'
|
|
403
426
|
}
|
|
404
|
-
const children = normalizeChildren(traverseExpr(returnExprNodes, state))
|
|
405
427
|
// 除百度、字节外其他小程序仅默认插槽可以支持多个节点
|
|
406
|
-
|
|
407
|
-
return children[0]
|
|
408
|
-
}
|
|
409
|
-
return {
|
|
428
|
+
return single({
|
|
410
429
|
type: 'block',
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
},
|
|
414
|
-
children
|
|
415
|
-
}
|
|
430
|
+
children: normalizeChildren(traverseExpr(returnExprNodes, state))
|
|
431
|
+
}, slotName, vIfCode, ['template', 'block'])
|
|
416
432
|
})
|
|
417
433
|
}
|
|
418
434
|
|
package/lib/util.js
CHANGED
|
@@ -5,7 +5,8 @@ const uniI18n = require('@dcloudio/uni-cli-i18n')
|
|
|
5
5
|
|
|
6
6
|
const {
|
|
7
7
|
METHOD_RENDER_LIST,
|
|
8
|
-
METHOD_RESOLVE_SCOPED_SLOTS
|
|
8
|
+
METHOD_RESOLVE_SCOPED_SLOTS,
|
|
9
|
+
METHOD_CREATE_ELEMENT
|
|
9
10
|
} = require('./constants')
|
|
10
11
|
|
|
11
12
|
function cached (fn) {
|
|
@@ -270,6 +271,11 @@ function hasEscapeQuote (path) {
|
|
|
270
271
|
return has
|
|
271
272
|
}
|
|
272
273
|
|
|
274
|
+
function isRootElement (path) {
|
|
275
|
+
const result = path.findParent(path => (path.isCallExpression() && path.get('callee').isIdentifier({ name: METHOD_CREATE_ELEMENT })) || path.isReturnStatement())
|
|
276
|
+
return result.isReturnStatement()
|
|
277
|
+
}
|
|
278
|
+
|
|
273
279
|
module.exports = {
|
|
274
280
|
hasOwn,
|
|
275
281
|
isUnaryTag: makeMap(
|
|
@@ -301,5 +307,6 @@ module.exports = {
|
|
|
301
307
|
processMemberExpression,
|
|
302
308
|
getForIndexIdentifier,
|
|
303
309
|
isSimpleObjectExpression,
|
|
304
|
-
hasEscapeQuote
|
|
310
|
+
hasEscapeQuote,
|
|
311
|
+
isRootElement
|
|
305
312
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dcloudio/uni-template-compiler",
|
|
3
|
-
"version": "2.0.1-
|
|
3
|
+
"version": "2.0.1-35320220729002",
|
|
4
4
|
"description": "uni-template-compiler",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"repository": {
|
|
@@ -22,5 +22,5 @@
|
|
|
22
22
|
"@babel/types": "^7.3.3",
|
|
23
23
|
"vue-template-compiler": "^2.6.10"
|
|
24
24
|
},
|
|
25
|
-
"gitHead": "
|
|
25
|
+
"gitHead": "99c489d7a19425ade612162abd21b441260e7ba7"
|
|
26
26
|
}
|