提交 0226eac6 编写于 作者: fxy060608's avatar fxy060608

fix(mp): add wxsCallMethods (#3218)

上级 377b2f59
export * from './ast'
export * from './wxs'
export * from './nvue'
export * from './event'
export * from './style'
export * from './template'
export * from './constants'
export { HTML_TO_MINI_PROGRAM_TAGS } from './tags'
export { copyMiniProgramPluginJson } from './plugin'
export {
......
import {
isCallExpression,
isIdentifier,
isMemberExpression,
isStringLiteral,
} from '@babel/types'
import { walk } from 'estree-walker'
import { parseProgram } from './ast'
export function parseWxsCallMethods(code: string) {
if (!code.includes('callMethod')) {
return []
}
const ast = parseProgram(code, '', {})
const wxsCallMethods = new Set<string>()
;(walk as any)(ast, {
enter(child: Node) {
if (!isCallExpression(child)) {
return
}
const { callee } = child
// .callMethod
if (
!isMemberExpression(callee) ||
!isIdentifier(callee.property) ||
callee.property.name !== 'callMethod'
) {
return
}
// .callMethod('test',...)
const args = child.arguments
if (!args.length) {
return
}
const [name] = args
if (!isStringLiteral(name)) {
return
}
wxsCallMethods.add(name.value)
},
})
return [...wxsCallMethods]
}
export function genWxsCallMethodsCode(code: string) {
const wxsCallMethods = parseWxsCallMethods(code)
if (!wxsCallMethods.length) {
return `export default {}`
}
return `export default (Component) => {
if(!Component.wxsCallMethods){
Component.wxsCallMethods = []
}
Component.wxsCallMethods.push(${wxsCallMethods
.map((m) => `'${m}'`)
.join(', ')})
}
`
}
......@@ -6,6 +6,7 @@ import {
MiniProgramFilterOptions,
missingModuleName,
parseRenderjs,
genWxsCallMethodsCode,
} from '@dcloudio/uni-cli-shared'
const debugRenderjs = debug('uni:mp-renderjs')
......@@ -50,7 +51,7 @@ export function uniRenderjsPlugin({ lang }: { lang?: string }): Plugin {
})
}
return {
code: 'export default {}',
code: genWxsCallMethodsCode(code),
map: { mappings: '' },
}
},
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册