11import  { 
2-   type  ExportDefaultDeclaration , 
32  type  IfStatement , 
43  type  ImportDeclaration , 
54  type  Node , 
@@ -8,11 +7,15 @@ import {
87  type  Program , 
98  type  Statement , 
109  type  StringLiteral , 
10+   assertExportDefaultDeclaration , 
11+   assertIdentifier , 
1112  isBlockStatement , 
1213  isCallExpression , 
14+   isExportDefaultDeclaration , 
1315  isIdentifier , 
1416  isIfStatement , 
1517  isImportDeclaration , 
18+   isImportSpecifier , 
1619  isMemberExpression , 
1720  isObjectExpression , 
1821  isObjectProperty , 
@@ -196,40 +199,79 @@ export async function updateMiniProgramGlobalComponents(
196199function  parseVueComponentName ( filename : string )  { 
197200  let  name  =  path . basename ( removeExt ( filename ) ) 
198201
199-   const  ast  =  scriptDescriptors . get ( filename ) 
202+   const  ast  =  scriptDescriptors . get ( filename ) ?. ast 
200203  if  ( ! ast )  return  name 
201204
202-   const  exportDefaultDecliaration  =  scriptDescriptors 
203-     . get ( filename ) 
204-     ?. ast . body . find ( 
205-       ( v )  =>  v . type  ===  'ExportDefaultDeclaration' 
206-     )  as  ExportDefaultDeclaration  |  null 
205+   // 获取默认导出定义 
206+   const  exportDefaultDecliaration  =  ast . body . find ( ( node )  => 
207+     isExportDefaultDeclaration ( node ) 
208+   ) 
209+ 
210+   assertExportDefaultDeclaration ( exportDefaultDecliaration ) 
207211
208212  if  ( ! exportDefaultDecliaration )  return  name 
209213
214+   // 获取vue的defineComponent导入变量名 
215+   let  defineComponentLocalName : string  |  null  =  null 
216+ 
217+   for  ( const  node  of  ast . body )  { 
218+     if  ( 
219+       isImportDeclaration ( node )  && 
220+       isStringLiteral ( node . source ,  {  value : 'vue'  } ) 
221+     )  { 
222+       const  importSpecifer  =  node . specifiers . find ( 
223+         ( specifer )  => 
224+           isImportSpecifier ( specifer )  && 
225+           isIdentifier ( specifer . imported ,  {  name : 'defineComponent'  } ) 
226+       ) 
227+       if  ( isImportSpecifier ( importSpecifer ) )  { 
228+         defineComponentLocalName  =  importSpecifer . local . name 
229+       } 
230+     } 
231+   } 
232+ 
233+   // 获取组件定义对象 
210234  let  defineComponentDeclaration : ObjectExpression  |  null  =  null 
211235
212-   const  {  declaration }  =  exportDefaultDecliaration 
236+   let  {  declaration }  =  exportDefaultDecliaration 
237+ 
238+   // 如果默认导出了变量则尝试查找该变量 
239+   if  ( isIdentifier ( declaration ) )  { 
240+     const  {  name }  =  declaration 
241+     for  ( const  node  of  ast . body )  { 
242+       if  ( isVariableDeclaration ( node ) )  { 
243+         assertIdentifier ( declaration ) 
244+         const  declarator  =  node . declarations . find ( ( declarator )  => 
245+           isIdentifier ( declarator . id ,  {  name } ) 
246+         ) 
247+         if  ( declarator ?. init )  { 
248+           declaration  =  declarator . init 
249+         } 
250+       }  else  if  ( isExportDefaultDeclaration ( node ) )  { 
251+         break 
252+       } 
253+     } 
254+   } 
213255
214-   if  ( declaration . type   ===   'ObjectExpression' )  { 
256+   if  ( isObjectExpression ( declaration ) )  { 
215257    defineComponentDeclaration  =  declaration 
216258  }  else  if  ( 
217-     declaration . type  ===  'CallExpression'  && 
218-     declaration . callee . type  ===  'Identifier'  && 
219-     / _ * d e f i n e C o m p o n e n t / . test ( declaration . callee . name ) 
259+     defineComponentLocalName  && 
260+     isCallExpression ( declaration )  && 
261+     isIdentifier ( declaration . callee ,  {  name : defineComponentLocalName  } )  && 
262+     isObjectExpression ( declaration . arguments [ 0 ] ) 
220263  )  { 
221-     defineComponentDeclaration  = 
222-       ( declaration . arguments [ 0 ]  as  ObjectExpression  |  undefined )  ||  null 
264+     defineComponentDeclaration  =  declaration . arguments [ 0 ] 
223265  } 
224266
225267  if  ( ! defineComponentDeclaration )  return  name 
226268
227269  for  ( const  prop  of  defineComponentDeclaration . properties )  { 
228270    if  ( 
229-       prop . type   ===   'ObjectProperty'  && 
230-       prop . key . type   ===   'Identifier'  && 
271+       isObjectProperty ( prop )  && 
272+       isIdentifier ( prop . key )  && 
231273      / ( _ _ ) ? n a m e / . test ( prop . key . name )  && 
232-       prop . value . type   ===   'StringLiteral' 
274+       isStringLiteral ( prop . value ) 
233275    )  { 
234276      return  prop . value . value 
235277    } 
0 commit comments