提交 9c066a9c 编写于 作者: fxy060608's avatar fxy060608

fix(mp): pass props to plugin components (#3257)

上级 0d5a49bd
......@@ -127,7 +127,11 @@ export function isMiniProgramUsingComponent(
componentsDir?: string
}
) {
return findMiniProgramUsingComponents(options).includes(name)
return !!findMiniProgramUsingComponents(options)[name]
}
interface MiniProgramComponents {
[name: string]: 'plugin' | 'component'
}
export function findMiniProgramUsingComponents({
......@@ -138,15 +142,13 @@ export function findMiniProgramUsingComponents({
filename: string
inputDir: string
componentsDir?: string
}) {
if (!componentsDir) {
return []
}
}): MiniProgramComponents {
const globalUsingComponents = appJsonCache && appJsonCache.usingComponents
const miniProgramComponents: string[] = []
const miniProgramComponents: MiniProgramComponents = {}
if (globalUsingComponents) {
miniProgramComponents.push(
...findMiniProgramUsingComponent(globalUsingComponents, componentsDir)
extend(
miniProgramComponents,
findMiniProgramUsingComponent(globalUsingComponents, componentsDir)
)
}
......@@ -154,8 +156,9 @@ export function findMiniProgramUsingComponents({
removeExt(normalizeMiniProgramFilename(filename, inputDir))
)
if (jsonFile?.usingComponents) {
miniProgramComponents.push(
...findMiniProgramUsingComponent(jsonFile.usingComponents, componentsDir)
extend(
miniProgramComponents,
findMiniProgramUsingComponent(jsonFile.usingComponents, componentsDir)
)
}
return miniProgramComponents
......@@ -163,10 +166,18 @@ export function findMiniProgramUsingComponents({
function findMiniProgramUsingComponent(
usingComponents: Record<string, string>,
componentsDir: string
componentsDir?: string
) {
return Object.keys(usingComponents).filter((name) => {
const path = usingComponents[name]
return path.includes(componentsDir + '/')
})
return Object.keys(usingComponents).reduce<MiniProgramComponents>(
(res, name) => {
const path = usingComponents[name]
if (componentsDir && path.includes(componentsDir + '/')) {
res[name] = 'component'
} else if (path.includes('plugin://')) {
res[name] = 'plugin'
}
return res
},
{}
)
}
......@@ -120,13 +120,14 @@ describe('compiler: transform component', () => {
addMiniProgramPageJson(filename, {
usingComponents: {
'van-button': 'wxcomponents/button/index',
wxparser: 'plugin://wxparserPlugin/wxparser',
},
})
assert(
`<van-button custom-style="background-color: unset;" :close-on-click-overlay="true"><template #default><view/></template><template #head><view/></template></van-button>`,
`<van-button u-t="m" u-i="dc555fe4-0" bind:__l="__l" u-p="{{a}}"><view/><view slot="head"/></van-button>`,
`<wxparser :rich-text="richText" /><van-button custom-style="background-color: unset;" :close-on-click-overlay="true"><template #default><view/></template><template #head><view/></template></van-button>`,
`<wxparser rich-text="{{a}}" u-t="m" u-i="dc555fe4-0" bind:__l="__l"/><van-button u-t="m" u-i="dc555fe4-1" bind:__l="__l" u-p="{{b}}"><view/><view slot="head"/></van-button>`,
`(_ctx, _cache) => {
return { a: _p({ customStyle: 'background-color: unset;', closeOnClickOverlay: true }) }
return { a: _ctx.richText, b: _p({ customStyle: 'background-color: unset;', closeOnClickOverlay: true }) }
}`,
{
filename,
......
......@@ -123,7 +123,7 @@ export interface TransformContext
addVIfScope(initScope: CodegenVIfScopeInit): CodegenVIfScope
addVForScope(initScope: CodegenVForScopeInit): CodegenVForScope
cache<T extends JSChildNode>(exp: T, isVNode?: boolean): CacheExpression | T
isMiniProgramComponent(name: string): boolean
isMiniProgramComponent(name: string): 'plugin' | 'component' | undefined
}
export function isRootScope(scope: CodegenScope): scope is CodegenRootScope {
......@@ -474,7 +474,7 @@ export function createTransformContext(
return createCacheExpression(context.cached++, exp, isVNode)
},
isMiniProgramComponent(name) {
return miniProgramComponents.includes(name)
return miniProgramComponents[name]
},
}
......
......@@ -149,6 +149,10 @@ export function rewriteBinding(
context: TransformContext
) {
const isMiniProgramComponent = context.isMiniProgramComponent(tag)
if (isMiniProgramComponent === 'plugin') {
// 因无法介入插件类型组件内部实现,故保留原始属性
return
}
const createObjectProperty = isMiniProgramComponent
? (name: string, value: Expression) =>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册