Skip to content

Commit 5ff93a8

Browse files
committed
fix(mp-weixin): 修复 editor-portal 带有 key prop 并作为 v-for 子节点时编译报错的 Bug (question/208703)
1 parent 137bf1c commit 5ff93a8

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

packages/uni-cli-shared/src/mp/template.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ export interface MiniProgramCompilerOptions {
100100
}
101101
directive: string
102102
emitFile?: (emittedFile: EmittedAsset) => string
103+
/**
104+
* 允许作为 v-for 子节点并保留 key 的组件列表
105+
*/
106+
keyEnabledElements?: string[]
103107
}
104108
export interface MiniProgramFilterOptions {
105109
id: string

packages/uni-mp-compiler/src/compile.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
120120
lazyElement,
121121
component,
122122
checkPropName,
123+
keyEnabledElements,
123124
} = options.miniProgram
124125
genTemplate(ast, {
125126
class: clazz,
@@ -135,6 +136,7 @@ export function baseCompile(template: string, options: CompilerOptions = {}) {
135136
isBuiltInComponent: context.isBuiltInComponent,
136137
isMiniProgramComponent: context.isMiniProgramComponent,
137138
checkPropName,
139+
keyEnabledElements,
138140
autoImportFilters: context.autoImportFilters,
139141
filter: options.miniProgram?.filter,
140142
})

packages/uni-mp-compiler/src/template/codegen.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export interface TemplateCodegenContext {
5454
isMiniProgramComponent: TransformContext['isMiniProgramComponent']
5555
push(code: string): void
5656
checkPropName: TemplateCodegenOptions['checkPropName']
57+
keyEnabledElements: TemplateCodegenOptions['keyEnabledElements']
5758
}
5859

5960
/**
@@ -88,6 +89,7 @@ export function generate(
8889
isBuiltInComponent,
8990
isMiniProgramComponent,
9091
checkPropName,
92+
keyEnabledElements,
9193
component,
9294
autoImportFilters,
9395
filter,
@@ -105,6 +107,7 @@ export function generate(
105107
isBuiltInComponent,
106108
isMiniProgramComponent,
107109
checkPropName,
110+
keyEnabledElements,
108111
push(code) {
109112
context.code += code
110113
},

packages/uni-mp-compiler/src/transforms/vFor.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,16 +185,20 @@ export const transformFor = createStructuralDirectiveTransform(
185185
scopes.vFor--
186186
if (isTemplateNode(node)) {
187187
node.children.some((c) => {
188-
if (c.type === NodeTypes.ELEMENT && !isForElementNode(c)) {
188+
if (isElementNode(c) && !isForElementNode(c)) {
189189
const key = findProp(c, 'key')
190190
if (key) {
191-
context.onError(
192-
createCompilerError(
193-
ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT,
194-
key.loc
191+
const keyEnabledElements =
192+
context.miniProgram.keyEnabledElements || []
193+
if (!keyEnabledElements.includes(c.tag)) {
194+
context.onError(
195+
createCompilerError(
196+
ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT,
197+
key.loc
198+
)
195199
)
196-
)
197-
return true
200+
return true
201+
}
198202
}
199203
}
200204
})

packages/uni-mp-vite/src/plugin/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export interface UniMiniProgramPluginOptions {
9696
generate: Parameters<typeof findMiniProgramTemplateFiles>[0]
9797
}
9898
compilerOptions?: CompilerOptions
99+
keyEnabledElements?: MiniProgramCompilerOptions['keyEnabledElements']
99100
checkPropName?: MiniProgramCompilerOptions['checkPropName']
100101
}
101102
style: {
@@ -141,6 +142,7 @@ export function uniMiniProgramPlugin(
141142
component: template.component,
142143
emitFile,
143144
slot: template.slot,
145+
keyEnabledElements: template.keyEnabledElements,
144146
checkPropName: template.checkPropName,
145147
},
146148
compilerOptions: template.compilerOptions,

packages/uni-mp-weixin/src/compiler/options.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ export function getMiniProgramOptions(
135135
lang: 'wxs',
136136
setStyle: true,
137137
},
138+
keyEnabledElements: ['editor-portal'],
138139
}
139140
}
140141

0 commit comments

Comments
 (0)