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

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

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