From 5e6c5bd84a5f5435acefdfa370c1435a816eaaf0 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 24 Aug 2019 13:23:43 +0800 Subject: [PATCH] feat(cli): wxsCallMethods --- .../lib/h5/compiler-options.js | 10 ++--- packages/vue-cli-plugin-uni/lib/h5/index.js | 6 +-- packages/vue-cli-plugin-uni/package.json | 2 +- .../webpack-uni-filter-loader/index.js | 38 ++++++++++++++++++- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js index 9318fe623..30782e39b 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js +++ b/packages/vue-cli-plugin-uni/lib/h5/compiler-options.js @@ -4,7 +4,7 @@ const { const simplePathRE = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\['[^']*?']|\["[^"]*?"]|\[\d+]|\[[A-Za-z_$][\w$]*])*$/ -function processEvent(expr, filterModules) { +function processEvent (expr, filterModules) { const isMethodPath = simplePathRE.test(expr) if (isMethodPath) { if (filterModules.find(name => expr.indexOf(name + '.') === 0)) { @@ -22,7 +22,7 @@ ${expr} ` } -function hasOwn(obj, key) { +function hasOwn (obj, key) { return hasOwnProperty.call(obj, key) } @@ -33,7 +33,7 @@ const deprecated = { } } -function addTag(tag) { +function addTag (tag) { if (!process.UNI_TAGS) { process.UNI_TAGS = new Set() } @@ -43,7 +43,7 @@ function addTag(tag) { module.exports = { preserveWhitespace: false, modules: [require('../format-text'), { - preTransformNode(el, { + preTransformNode (el, { warn }) { if (el.tag.indexOf('v-uni-') === 0) { @@ -53,7 +53,7 @@ module.exports = { el.tag = 'v-uni-' + el.tag } }, - postTransformNode(el, { + postTransformNode (el, { warn, filterModules }) { diff --git a/packages/vue-cli-plugin-uni/lib/h5/index.js b/packages/vue-cli-plugin-uni/lib/h5/index.js index b29194012..6db016840 100644 --- a/packages/vue-cli-plugin-uni/lib/h5/index.js +++ b/packages/vue-cli-plugin-uni/lib/h5/index.js @@ -10,7 +10,7 @@ const { const WebpackHtmlAppendPlugin = require('../../packages/webpack-html-append-plugin') -function resolve(dir) { +function resolve (dir) { return path.resolve(__dirname, '../../', dir) } @@ -61,7 +61,7 @@ if (devServer && Object.keys(devServer).length) { module.exports = { vueConfig, - webpackConfig(webpackConfig) { + webpackConfig (webpackConfig) { return { devtool: process.env.NODE_ENV === 'production' ? false : 'source-map', resolve: { @@ -100,7 +100,7 @@ module.exports = { plugins } }, - chainWebpack(webpackConfig) { + chainWebpack (webpackConfig) { webpackConfig.plugins.delete('copy') if (!process.env.UNI_OPT_PREFETCH) { diff --git a/packages/vue-cli-plugin-uni/package.json b/packages/vue-cli-plugin-uni/package.json index c7c984326..455bc37d1 100644 --- a/packages/vue-cli-plugin-uni/package.json +++ b/packages/vue-cli-plugin-uni/package.json @@ -1,6 +1,6 @@ { "name": "@dcloudio/vue-cli-plugin-uni", - "version": "0.9.528", + "version": "0.9.529", "description": "uni-app plugin for vue-cli 3", "main": "index.js", "scripts": { diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js index e9dec9410..d9bc6b00c 100644 --- a/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-filter-loader/index.js @@ -1,5 +1,11 @@ const path = require('path') + const loaderUtils = require('loader-utils') + +const t = require('@babel/types') +const parser = require('@babel/parser') +const traverse = require('@babel/traverse').default + const { normalizeNodeModules } = require('@dcloudio/uni-cli-shared/lib/platform') @@ -18,10 +24,40 @@ module.exports = function(source, map) { map ) } else { // mp + const callMethods = new Set() + if (source.indexOf('callMethod') !== -1) { + traverse(parser.parse(source, { + sourceType: 'module' + }), { + MemberExpression(path, state) { + const property = path.node.property + const parentNode = path.parent + if ( + ( + property.name === 'callMethod' || + property.value === 'callMethod' + ) && + t.isCallExpression(parentNode) && + t.isLiteral(parentNode.arguments[0]) + ) { + callMethods.add(parentNode.arguments[0].value) + } + } + }) + } if (params.issuerPath) { const resourcePath = normalizeNodeModules(path.relative(process.env.UNI_INPUT_DIR, this.resourcePath)) this.emitFile(resourcePath, source) } - this.callback(null, `export default {}`, map) + this.callback(null, + `export default function (Component) { + if(!Component.options.wxsCallMethods){ + Component.options.wxsCallMethods = [] + } + ${[...callMethods].map(method=>{ + return "Component.options.wxsCallMethods.push('"+method+"')" + }).join('\n')} + }`, + map) } } -- GitLab