diff --git a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
index a75db7311f33c24d715f3d1c9313defb0e2d3c8d..052e92b38a9649d4290f72e93edf20cfc338c171 100644
--- a/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
+++ b/packages/uni-template-compiler/__tests__/compiler-extra.spec.js
@@ -237,16 +237,28 @@ describe('mp:compiler-extra', () => {
assertCodegen(
'text',
- 'text'
+ 'text'
)
assertCodegen(
'text123213',
- 'text123213'
+ 'text123213'
)
assertCodegen(
'',
- ''
+ ''
+ )
+ assertCodegen(
+ 'view1view2',
+ 'view1view2'
+ )
+ assertCodegen(
+ 'view1view2',
+ 'view1view2'
+ )
+ assertCodegen(
+ 'view1view2',
+ 'view1view2'
)
assertCodegen(
`
@@ -258,7 +270,7 @@ describe('mp:compiler-extra', () => {
Here's some contact info
`,
- 'Here might be a page titleHere\'s some contact infoA paragraph for the main content.'
+ 'Here might be a page titleHere\'s some contact infoA paragraph for the main content.'
)
})
diff --git a/packages/uni-template-compiler/lib/template/generate.js b/packages/uni-template-compiler/lib/template/generate.js
index df216600c1a9a8717293f9e4e47bc5caa9af62e6..6c0b34db0351d3742448ae1c5ec7e485f0dd64ec 100644
--- a/packages/uni-template-compiler/lib/template/generate.js
+++ b/packages/uni-template-compiler/lib/template/generate.js
@@ -48,12 +48,17 @@ function processElement (ast, state, isRoot) {
const slots = []
for (let i = children.length - 1; i >= 0; i--) {
const childElement = children[i]
- // =>
+ /**
+ * 仅百度、字节支持使用 block 作为命名插槽根节点
+ * 此处为了统一仅忽略默认插槽
+ * =>
+ */
if (typeof childElement !== 'string' && childElement.attr.slot) {
- if (childElement.type === 'block') {
+ const slot = childElement.attr.slot
+ if (slot && slot !== 'default' && childElement.type === 'block') {
childElement.type = 'view'
}
- slots.push(childElement.attr.slot)
+ slots.push(slot)
} else {
defaultSlot = true
}
@@ -78,7 +83,7 @@ function processElement (ast, state, isRoot) {
if (ast.attr.id && ast.attr.id.indexOf('{{') === 0) {
state.tips.add(`id 作为属性保留名,不允许在自定义组件 ${ast.type} 中定义为 props`)
}
- if (hasOwn(ast.attr, 'data') && platformName !== 'mp-toutiao') { // 百度中会出现异常情况
+ if (hasOwn(ast.attr, 'data') && platformName !== 'mp-toutiao') { // 百度中会出现异常情况
// TODO 暂不输出
// state.tips.add(`data 作为属性保留名,不允许在自定义组件 ${ast.type} 中定义为 props`)
}
diff --git a/packages/uni-template-compiler/lib/template/traverse.js b/packages/uni-template-compiler/lib/template/traverse.js
index ae1c59ce7654951aa58b3bb99162ecf181b77273..4d50cb5447ad0cf47b1625678d079bb5580efc90 100644
--- a/packages/uni-template-compiler/lib/template/traverse.js
+++ b/packages/uni-template-compiler/lib/template/traverse.js
@@ -337,6 +337,20 @@ function traverseRenderSlot (callExprNode, state) {
}
function traverseResolveScopedSlots (callExprNode, state) {
+ function single (children, slotName, ignore) {
+ if (Array.isArray(children) && children.length === 1) {
+ const child = children[0]
+ if (!child.type) {
+ return
+ }
+ if (ignore.includes(child.type)) {
+ return single(child.children, slotName, ignore)
+ }
+ child.attr = child.attr || {}
+ child.attr.slot = slotName
+ return true
+ }
+ }
return callExprNode.arguments[0].elements.map(slotNode => {
let keyProperty = false
let fnProperty = false
@@ -383,14 +397,18 @@ function traverseResolveScopedSlots (callExprNode, state) {
state
)
}
- const node = {
- type: 'view',
+ const children = normalizeChildren(traverseExpr(returnExprNodes, state))
+ // 除百度、字节外其他小程序仅默认插槽可以支持多个节点
+ if (single(children, slotName, ['template', 'block'])) {
+ return children[0]
+ }
+ return {
+ type: 'block',
attr: {
slot: slotName
},
- children: normalizeChildren(traverseExpr(returnExprNodes, state))
+ children
}
- return node
})
}