diff --git a/packages/webpack-uni-mp-loader/__tests__/components.spec.js b/packages/webpack-uni-mp-loader/__tests__/components.spec.js index 65ae7922ca4f29031c360ad85ea9b0b847c77aef..e1c563dd0bcfb4ee128724995dacd4aabe986d35 100644 --- a/packages/webpack-uni-mp-loader/__tests__/components.spec.js +++ b/packages/webpack-uni-mp-loader/__tests__/components.spec.js @@ -46,6 +46,24 @@ function assertCodegenOptions (content, expectedOptions, isScoped = true) { describe('mp:loader', () => { it('parse scoped component', () => { + assertCodegen( + `import { uniBadge,uniCard} from '@dcloudio/uni-ui'; +export default defineComponent({ + components: { + 'uni-badge':uniBadge, + 'uni-card':uniCard + } +})`, + [{ + name: 'uni-badge', + value: 'uniBadge', + source: '@dcloudio/uni-ui/lib/uni-badge/uni-badge' + }, { + name: 'uni-card', + value: 'uniCard', + source: '@dcloudio/uni-ui/lib/uni-card/uni-card' + }]) + assertCodegen( ` import mediaList from '@/components/tab-nvue/mediaList.vue'; @@ -158,29 +176,27 @@ global['__wxVueOptions'] = { 'van-button': VanButton, 'van-search': VanSearch, },exports.default.components || {})`, - [ - { - name: 'van-button', - value: 'VanButton', - source: '../button/index.vue' - }, - { - name: 'van-search', - value: 'VanSearch', - source: '../search/index.vue' - }, - { - name: 'myButton', - value: 'myButton', - source: '@/components/my-button/my-button.vue' - } + [{ + name: 'van-button', + value: 'VanButton', + source: '../button/index.vue' + }, + { + name: 'van-search', + value: 'VanSearch', + source: '../search/index.vue' + }, + { + name: 'myButton', + value: 'myButton', + source: '@/components/my-button/my-button.vue' + } ]) assertCodegenOptions( `export default { name: 'test' - }`, - { + }`, { name: '"test"', inheritAttrs: null, props: null @@ -191,8 +207,7 @@ global['__wxVueOptions'] = { `const options = { name: 'test' } - export default options`, - { + export default options`, { name: '"test"', inheritAttrs: null, props: null @@ -204,8 +219,7 @@ global['__wxVueOptions'] = { options = { name: 'test' } - export default options`, - { + export default options`, { name: '"test"', inheritAttrs: null, props: null @@ -216,8 +230,7 @@ global['__wxVueOptions'] = { `const options = Vue.extend({ name: 'test' }) - export default options`, - { + export default options`, { name: '"test"', inheritAttrs: null, props: null @@ -229,8 +242,7 @@ global['__wxVueOptions'] = { options = Vue.extend({ name: 'test' }) - export default options`, - { + export default options`, { name: '"test"', inheritAttrs: null, props: null @@ -241,8 +253,7 @@ global['__wxVueOptions'] = { `const options = { name: 'test' } - export default Vue.extend(options)`, - { + export default Vue.extend(options)`, { name: '"test"', inheritAttrs: null, props: null @@ -252,8 +263,7 @@ global['__wxVueOptions'] = { assertCodegenOptions( `export default { props: ['id', 'test'] - }`, - { + }`, { name: null, inheritAttrs: null, props: '["id","test"]' @@ -270,8 +280,7 @@ global['__wxVueOptions'] = { type: String } } - }`, - { + }`, { name: null, inheritAttrs: null, props: '["id","test"]' @@ -302,4 +311,4 @@ global['__wxVueOptions'] = { source: '@/components/tab-nvue/mediaList.vue' }], false) }) -}) +}) 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 62f4da0efc9e971118dc6b48dce74c2d7a7d574f..cc2fcc4b482409e98de3e926a3c9b2ce5bee7a2a 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 @@ -16,10 +16,15 @@ function handleObjectExpression (declaration, path, state) { if (optionProperty) { if (name === 'props') { if (t.isArrayExpression(optionProperty.value)) { - state.options[name] = JSON.stringify(optionProperty.value.elements.filter(element => t.isStringLiteral(element)).map(({ value }) => 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 }) => { + optionProperty.value.properties.forEach(({ + key + }) => { if (t.isIdentifier(key)) { props.push(key.name) } else if (t.isStringLiteral(key)) { @@ -60,7 +65,9 @@ function handleComponentsObjectExpression (componentsObjExpr, path, state, prepe state.components = prepend ? components.concat(state.components) : components } -function handleIdentifier ({ name }, path, state) { +function handleIdentifier ({ + name +}, path, state) { // 仅做有限查找 for (let i = path.container.length; i > 0; i--) { const node = path.container[i - 1] @@ -101,6 +108,19 @@ module.exports = function (ast, state = { options: {} }) { babelTraverse(ast, { + CallExpression (path) { + const callee = path.node.callee + const args = path.node.arguments + const objExpr = args[0] + if ( + t.isIdentifier(callee) && + callee.name === 'defineComponent' && + args.length === 1 && + t.isObjectExpression(objExpr) + ) { + handleObjectExpression(objExpr, path, state) + } + }, AssignmentExpression (path) { const leftExpression = path.node.left const rightExpression = path.node.right @@ -162,4 +182,4 @@ module.exports = function (ast, state = { ast, state } -} +}