From 0226eac6743a62da071b76fd6377615616023a74 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 21 Jan 2022 18:03:22 +0800 Subject: [PATCH] fix(mp): add wxsCallMethods (#3218) --- packages/uni-cli-shared/src/mp/index.ts | 2 + packages/uni-cli-shared/src/mp/wxs.ts | 59 ++++++++++++++++++++ packages/uni-mp-vite/src/plugins/renderjs.ts | 3 +- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 packages/uni-cli-shared/src/mp/wxs.ts diff --git a/packages/uni-cli-shared/src/mp/index.ts b/packages/uni-cli-shared/src/mp/index.ts index ea1aa264fa..a7ff79c7d9 100644 --- a/packages/uni-cli-shared/src/mp/index.ts +++ b/packages/uni-cli-shared/src/mp/index.ts @@ -1,9 +1,11 @@ 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 { diff --git a/packages/uni-cli-shared/src/mp/wxs.ts b/packages/uni-cli-shared/src/mp/wxs.ts new file mode 100644 index 0000000000..0d354cebe6 --- /dev/null +++ b/packages/uni-cli-shared/src/mp/wxs.ts @@ -0,0 +1,59 @@ +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() + ;(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(', ')}) +} +` +} diff --git a/packages/uni-mp-vite/src/plugins/renderjs.ts b/packages/uni-mp-vite/src/plugins/renderjs.ts index d287e68c51..2e0eb847c9 100644 --- a/packages/uni-mp-vite/src/plugins/renderjs.ts +++ b/packages/uni-mp-vite/src/plugins/renderjs.ts @@ -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: '' }, } }, -- GitLab