From b619458a52247db2919831fcd32c77eb985bc3a8 Mon Sep 17 00:00:00 2001 From: qiang Date: Mon, 29 Jun 2020 19:02:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=A7=A3=E5=86=B3App=E7=AB=AF=E5=B1=80?= =?UTF-8?q?=E9=83=A8=E7=BB=84=E4=BB=B6props=E4=B8=AD=E7=9A=84id=E4=BC=9A?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E6=A0=B9=E8=8A=82=E7=82=B9id=E5=B1=9E?= =?UTF-8?q?=E6=80=A7=E7=9A=84=E9=97=AE=E9=A2=98=20question/99900?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../webpack-uni-app-loader/view/util.js | 5 +- .../__tests__/components.spec.js | 62 +++++++++++++++++++ .../lib/babel/scoped-component-traverse.js | 18 +++++- 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/util.js b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/util.js index ff5340bfe..e30347e92 100644 --- a/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/util.js +++ b/packages/vue-cli-plugin-uni/packages/webpack-uni-app-loader/view/util.js @@ -19,7 +19,8 @@ function parseComponents (content, traverse) { components: [], options: { name: null, - inheritAttrs: null + inheritAttrs: null, + props: null } }) return { @@ -30,4 +31,4 @@ function parseComponents (content, traverse) { module.exports = { parseComponents -} +} diff --git a/packages/webpack-uni-mp-loader/__tests__/components.spec.js b/packages/webpack-uni-mp-loader/__tests__/components.spec.js index e4e1a7132..cbe2109b9 100644 --- a/packages/webpack-uni-mp-loader/__tests__/components.spec.js +++ b/packages/webpack-uni-mp-loader/__tests__/components.spec.js @@ -22,6 +22,28 @@ function assertCodegen (content, expectedComponents, isScoped = true) { expect(JSON.stringify(components)).toBe(JSON.stringify(expectedComponents)) } +function assertCodegenOptions (content, expectedOptions, isScoped = true) { + const { + state: { + options + } + } = (isScoped ? scopedComponentTraverse : globalComponentTraverse)(parser.parse(content, { + sourceType: 'module', + plugins: [ + 'typescript' + ] + }), { + components: [], + options: { + name: null, + inheritAttrs: null, + props: null + } + }) + + expect(JSON.stringify(options)).toBe(JSON.stringify(expectedOptions)) +} + describe('mp:loader', () => { it('parse scoped component', () => { assertCodegen( @@ -153,6 +175,46 @@ global['__wxVueOptions'] = { source: '@/components/my-button/my-button.vue' } ]) + + assertCodegenOptions( + `export default { + name: 'test' + }`, + { + name: '"test"', + inheritAttrs: null, + props: null + } + ) + + assertCodegenOptions( + `export default { + props: ['id', 'test'] + }`, + { + name: null, + inheritAttrs: null, + props: '["id","test"]' + } + ) + + assertCodegenOptions( + `export default { + props: { + id: { + type: String + }, + 'test': { + type: String + } + } + }`, + { + name: null, + inheritAttrs: null, + props: '["id","test"]' + } + ) }) it('parse global component', () => { diff --git a/packages/webpack-uni-mp-loader/lib/babel/scoped-component-traverse.js b/packages/webpack-uni-mp-loader/lib/babel/scoped-component-traverse.js index 42e320568..b6ee2b10a 100644 --- a/packages/webpack-uni-mp-loader/lib/babel/scoped-component-traverse.js +++ b/packages/webpack-uni-mp-loader/lib/babel/scoped-component-traverse.js @@ -6,7 +6,7 @@ const { } = require('./util') function handleObjectExpression (declaration, path, state) { - if (state.options) { // name,inheritAttrs + if (state.options) { // name,inheritAttrs,props Object.keys(state.options).forEach(name => { const optionProperty = declaration.properties.filter(prop => { return t.isObjectProperty(prop) && @@ -14,7 +14,21 @@ function handleObjectExpression (declaration, path, state) { prop.key.name === name })[0] if (optionProperty) { - if (t.isStringLiteral(optionProperty.value)) { + if (name === 'props') { + if (t.isArrayExpression(optionProperty.value)) { + state.options[name] = JSON.stringify(optionProperty.value.elements.filter(element => t.isStringLiteral(element)).map(({ value }) => value)) + } else if (t.isObjectExpression(optionProperty.value)) { + const props = [] + optionProperty.value.properties.forEach(({ key }) => { + if (t.isIdentifier(key)) { + props.push(key.name) + } else if (t.isStringLiteral(key)) { + props.push(key.value) + } + }) + state.options[name] = JSON.stringify(props) + } + } else if (t.isStringLiteral(optionProperty.value)) { state.options[name] = JSON.stringify(optionProperty.value.value) } else { state.options[name] = optionProperty.value.value -- GitLab