diff --git a/packages/uni-mp-baidu/__tests__/vFor.spec.ts b/packages/uni-mp-baidu/__tests__/vFor.spec.ts
index 2d4a84b76b6619eb9b5fa26bb721e5e867f7bd38..08f87d416888575e5fc1bbca1bbe88931c91388f 100644
--- a/packages/uni-mp-baidu/__tests__/vFor.spec.ts
+++ b/packages/uni-mp-baidu/__tests__/vFor.spec.ts
@@ -4,7 +4,7 @@ describe(`mp-baidu: transform v-for`, () => {
test(`with key`, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return { a: item.id }; }) }
}`
@@ -13,7 +13,7 @@ describe(`mp-baidu: transform v-for`, () => {
test(`without key`, () => {
assert(
``,
- ``,
+ ``,
`(_ctx, _cache) => {
return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
}`
diff --git a/packages/uni-mp-baidu/__tests__/vSlot.spec.ts b/packages/uni-mp-baidu/__tests__/vSlot.spec.ts
new file mode 100644
index 0000000000000000000000000000000000000000..a65562839e4c371956f129bb5d5a3203f979960f
--- /dev/null
+++ b/packages/uni-mp-baidu/__tests__/vSlot.spec.ts
@@ -0,0 +1,95 @@
+import { assert } from './testUtils'
+
+describe('compiler: transform v-slot', () => {
+ test('default slot', () => {
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return {}
+}`
+ )
+ assert(
+ `test`,
+ `test`,
+ `(_ctx, _cache) => {
+ return {}
+}`
+ )
+ })
+ test('named slots', () => {
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return {}
+}`
+ )
+ assert(
+ `{{error.message}}`,
+ `{{v0.c}}`,
+ `(_ctx, _cache) => {
+ return { a: _w(({ data, loading, error, options }, s0, i0) => { return _e({ a: s0, b: error }, error ? { c: _t(error.message) } : {}); }, { name: 'default', vueId: '2a9ec0b0-0' }) }
+}`
+ )
+ })
+
+ test('scoped slots', () => {
+ assert(
+ `{{ slotProps.item }}`,
+ `{{slotProps.b}}`,
+ `(_ctx, _cache) => {
+ return { a: _w((slotProps, s0, i0) => { return { a: s0, b: _t(slotProps.item) }; }, { name: 'default', vueId: '2a9ec0b0-0' }) }
+}`
+ )
+ })
+
+ test('scoped slots + scoped slots', () => {
+ assert(
+ `{{ slotProps.item }}{{ slotProps1.item }}`,
+ `{{slotProps.c}}{{slotProps1.b}}`,
+ `(_ctx, _cache) => {
+ return { a: _w((slotProps, s0, i0) => { return { a: s0, b: _w((slotProps1, s1, i1) => { return { a: s1, b: _t(slotProps1.item) }; }, { name: 'default', vueId: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }), c: _t(slotProps.item), d: '2a9ec0b0-1' + '-' + i0 + ',' + '2a9ec0b0-0' }; }, { name: 'default', vueId: '2a9ec0b0-0' }) }
+}`
+ )
+ })
+
+ test('v-if + scoped slots', () => {
+ assert(
+ `{{ slotProps.item }}`,
+ `{{slotProps.b}}`,
+ `(_ctx, _cache) => {
+ return _e({ a: _ctx.ok }, _ctx.ok ? { b: _w((slotProps, s0, i0) => { return { a: s0, b: _t(slotProps.item) }; }, { name: 'default', vueId: '2a9ec0b0-0' }) } : {})
+}`
+ )
+ })
+
+ test('v-for + scoped slots', () => {
+ assert(
+ `{{ slotProps.item }}`,
+ `{{slotProps.b}}`,
+ `(_ctx, _cache) => {
+ return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _w((slotProps, s1, i1) => { return { a: s1, b: _t(slotProps.item) }; }, { name: 'default', vueId: '2a9ec0b0-0' + '-' + i0 }), b: '2a9ec0b0-0' + '-' + i0 }; }) }
+}`
+ )
+ })
+
+ test('v-for + v-for + scoped slots', () => {
+ assert(
+ `{{ slotProps.item }}`,
+ `{{slotProps.b}}`,
+ `(_ctx, _cache) => {
+ return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _f(item.list, (item1, k1, i1) => { return { a: _w((slotProps, s2, i2) => { return { a: s2, b: _t(slotProps.item) }; }, { name: 'default', vueId: '2a9ec0b0-0' + '-' + i0 + '-' + i1 }), b: item1, c: '2a9ec0b0-0' + '-' + i0 + '-' + i1 }; }) }; }) }
+}`
+ )
+ })
+ test('old syntax', () => {
+ assert(
+ ``,
+ ``,
+ `(_ctx, _cache) => {
+ return {}
+}`
+ )
+ })
+})
diff --git a/packages/uni-mp-baidu/dist/uni.compiler.js b/packages/uni-mp-baidu/dist/uni.compiler.js
index 1abb9a515b710e7adf568e77d3f6924408f0dd6d..32c907e16f117c208afe8f81538965fec4356332 100644
--- a/packages/uni-mp-baidu/dist/uni.compiler.js
+++ b/packages/uni-mp-baidu/dist/uni.compiler.js
@@ -41,15 +41,19 @@ const transformFor = (node, context) => {
if (!uniMpCompiler.isForElementNode(node)) {
return;
}
+ const { vFor, props } = node;
+ let sourceCode = vFor.valueAlias + ' in ' + vFor.sourceAlias;
const keyProp = uniMpCompiler.findProp(node, 'key', true);
if (keyProp) {
const { exp } = keyProp;
if (exp) {
const key = uniMpCompiler.rewriteExpression(exp, context).content;
- node.vFor.sourceCode = `${node.vFor.sourceAlias} trackBy ${key}`;
- node.props.splice(node.props.indexOf(keyProp), 1);
+ sourceCode = sourceCode + ' trackBy ' + key;
+ props.splice(props.indexOf(keyProp), 1);
}
}
+ vFor.valueAlias = '';
+ vFor.sourceCode = sourceCode;
};
/**
@@ -156,6 +160,7 @@ const miniProgram = {
},
slot: {
fallbackContent: true,
+ // https://github.com/baidu/san/discussions/601
dynamicSlotNames: false,
},
directive: 's-',
diff --git a/packages/uni-mp-baidu/src/compiler/options.ts b/packages/uni-mp-baidu/src/compiler/options.ts
index f7ee57e12570fc9429a79b6674718b60c3ce4f96..b6a5d9dcc89d44e48b053b0bbca4b2ea8a1ca30e 100644
--- a/packages/uni-mp-baidu/src/compiler/options.ts
+++ b/packages/uni-mp-baidu/src/compiler/options.ts
@@ -24,6 +24,7 @@ export const miniProgram: MiniProgramCompilerOptions = {
},
slot: {
fallbackContent: true,
+ // https://github.com/baidu/san/discussions/601
dynamicSlotNames: false,
},
directive: 's-',
diff --git a/packages/uni-mp-baidu/src/compiler/transforms/vFor.ts b/packages/uni-mp-baidu/src/compiler/transforms/vFor.ts
index 6b7724b70032c91294de33446fbb1f4a5907a360..3fa282aec03ee2460b1b2f0e6a5aba6c11197c9d 100644
--- a/packages/uni-mp-baidu/src/compiler/transforms/vFor.ts
+++ b/packages/uni-mp-baidu/src/compiler/transforms/vFor.ts
@@ -10,13 +10,17 @@ export const transformFor: NodeTransform = (node, context) => {
if (!isForElementNode(node)) {
return
}
+ const { vFor, props } = node
+ let sourceCode = vFor.valueAlias + ' in ' + vFor.sourceAlias
const keyProp = findProp(node, 'key', true)
if (keyProp) {
const { exp } = keyProp as DirectiveNode
if (exp) {
const key = rewriteExpression(exp, context).content
- node.vFor.sourceCode = `${node.vFor.sourceAlias} trackBy ${key}`
- node.props.splice(node.props.indexOf(keyProp), 1)
+ sourceCode = sourceCode + ' trackBy ' + key
+ props.splice(props.indexOf(keyProp), 1)
}
}
+ vFor.valueAlias = ''
+ vFor.sourceCode = sourceCode
}
diff --git a/packages/uni-mp-compiler/src/transforms/transformSlot.ts b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
index e4529f784556517033a51cdf325d28c79663507e..6df8d76c18108ec1f2473a0e3b651a21b314d21d 100644
--- a/packages/uni-mp-compiler/src/transforms/transformSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/transformSlot.ts
@@ -72,24 +72,7 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) {
}
})
if (properties.length) {
- const slotKey = parseScopedSlotKey(context)
- const nameProps = findProp(node, 'name')
- if (!nameProps) {
- // 生成默认的 default 插槽名
- if (slotKey) {
- props.push(
- createBindDirectiveNode(
- 'name',
- rewriteExpression(
- createSimpleExpression('"default-"+' + slotKey),
- context
- ).content
- )
- )
- } else {
- props.push(createAttributeNode('name', 'default'))
- }
- }
+ const slotKey = transformScopedSlotKey(node, context)
rewriteExpression(
createCompoundExpression([
context.helperString(RENDER_SLOT) + '(',
@@ -105,6 +88,35 @@ export function rewriteSlot(node: SlotOutletNode, context: TransformContext) {
}
}
+function transformScopedSlotKey(
+ node: SlotOutletNode,
+ context: TransformContext
+) {
+ if (!context.miniProgram.slot.dynamicSlotNames) {
+ return
+ }
+ const { props } = node
+ const slotKey = parseScopedSlotKey(context)
+ const nameProps = findProp(node, 'name')
+ if (!nameProps) {
+ // 生成默认的 default 插槽名
+ if (slotKey) {
+ props.push(
+ createBindDirectiveNode(
+ 'name',
+ rewriteExpression(
+ createSimpleExpression('"default-"+' + slotKey),
+ context
+ ).content
+ )
+ )
+ } else {
+ props.push(createAttributeNode('name', 'default'))
+ }
+ }
+ return slotKey
+}
+
function parseScopedSlotKey(context: TransformContext) {
let { currentScope } = context
const indexs: string[] = []
diff --git a/packages/uni-mp-compiler/src/transforms/vSlot.ts b/packages/uni-mp-compiler/src/transforms/vSlot.ts
index 8db28e13adb57118dbc37b5eac7030f75466ab44..a50ffb1b755e0caa610f625c0208a42e92aaf76a 100644
--- a/packages/uni-mp-compiler/src/transforms/vSlot.ts
+++ b/packages/uni-mp-compiler/src/transforms/vSlot.ts
@@ -194,10 +194,6 @@ function createVForTemplate(
) {
const key = 's' + context.scopes.vFor
const keyProp: DirectiveNode = createBindDirectiveNode('key', key)
- const slotProp: DirectiveNode = createBindDirectiveNode(
- 'slot',
- `i${context.scopes.vFor}`
- )
const vForProp: DirectiveNode = {
type: NodeTypes.DIRECTIVE,
name: 'for',
@@ -210,13 +206,17 @@ function createVForTemplate(
})`
),
}
+ const props = [vForProp, keyProp]
+ if (context.miniProgram.slot.dynamicSlotNames) {
+ props.push(createBindDirectiveNode('slot', `i${context.scopes.vFor}`))
+ }
return {
loc: slotElement.loc,
ns: 0,
tag: 'template',
type: NodeTypes.ELEMENT,
tagType: ElementTypes.TEMPLATE,
- props: [vForProp, keyProp, slotProp],
+ props,
isSelfClosing: false,
codegenNode: undefined,
children: slotElement.children,