@@ -8,13 +8,14 @@ import {
88 type Statement ,
99 type StringLiteral ,
1010 assertExportDefaultDeclaration ,
11- assertIdentifier ,
1211 isBlockStatement ,
1312 isCallExpression ,
1413 isExportDefaultDeclaration ,
14+ isExpression ,
1515 isIdentifier ,
1616 isIfStatement ,
1717 isImportDeclaration ,
18+ isImportDefaultSpecifier ,
1819 isImportSpecifier ,
1920 isMemberExpression ,
2021 isObjectExpression ,
@@ -197,87 +198,108 @@ export async function updateMiniProgramGlobalComponents(
197198}
198199
199200function parseVueComponentName ( filename : string ) {
200- let name = path . basename ( removeExt ( filename ) )
201+ try {
202+ let name = path . basename ( removeExt ( filename ) )
201203
202- const ast = scriptDescriptors . get ( filename ) ?. ast
203- if ( ! ast ) return name
204+ const ast = scriptDescriptors . get ( filename ) ?. ast
205+ if ( ! ast ) return name
204206
205- // 获取默认导出定义
206- const exportDefaultDecliaration = ast . body . find ( ( node ) =>
207- isExportDefaultDeclaration ( node )
208- )
207+ // 获取默认导出定义
208+ const exportDefaultDecliaration = ast . body . find ( ( node ) =>
209+ isExportDefaultDeclaration ( node )
210+ )
209211
210- assertExportDefaultDeclaration ( exportDefaultDecliaration )
212+ assertExportDefaultDeclaration ( exportDefaultDecliaration )
211213
212- if ( ! exportDefaultDecliaration ) return name
214+ if ( ! exportDefaultDecliaration ) return name
213215
214- // 获取vue的defineComponent导入变量名
215- let defineComponentLocalName : string | null = null
216+ // 获取vue的defineComponent导入变量名和plugin-vue:export-helper默认导入的本地变量名
217+ let defineComponentLocalName : string | null = null
218+ let exportHelperLocalName : string | null = null
216219
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
220+ for ( const node of ast . body ) {
221+ if ( ! isImportDeclaration ( node ) ) continue
222+ if ( isStringLiteral ( node . source , { value : 'vue' } ) ) {
223+ const importSpecifer = node . specifiers . find (
224+ ( specifer ) =>
225+ isImportSpecifier ( specifer ) &&
226+ isIdentifier ( specifer . imported , { name : 'defineComponent' } )
227+ )
228+ if ( isImportSpecifier ( importSpecifer ) ) {
229+ defineComponentLocalName = importSpecifer . local . name
230+ }
231+ } else if (
232+ isStringLiteral ( node . source , { value : 'plugin-vue:export-helper' } )
233+ ) {
234+ const importSpecifer = node . specifiers . find ( ( specifer ) =>
235+ isImportDefaultSpecifier ( specifer )
236+ )
237+ if ( isImportDefaultSpecifier ( importSpecifer ) ) {
238+ exportHelperLocalName = importSpecifer . local . name
239+ }
229240 }
230241 }
231- }
232-
233- // 获取组件定义对象
234- let defineComponentDeclaration : ObjectExpression | null = null
235242
236- let { declaration } = exportDefaultDecliaration
243+ let { declaration } = exportDefaultDecliaration
244+ // 如果默认导出调用plugin-vue:export-helper默认导入的方法则取方法的第一个参数
245+ if (
246+ exportHelperLocalName &&
247+ isCallExpression ( declaration ) &&
248+ isIdentifier ( declaration . callee , { name : exportHelperLocalName } ) &&
249+ isExpression ( declaration . arguments [ 0 ] )
250+ ) {
251+ declaration = declaration . arguments [ 0 ]
252+ }
237253
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
254+ // 获取组件定义对象
255+ let defineComponentDeclaration : ObjectExpression | null = null
256+
257+ // 如果declaration是变量则尝试查找该变量
258+ if ( isIdentifier ( declaration ) ) {
259+ const { name } = declaration
260+ for ( const node of ast . body ) {
261+ if ( isVariableDeclaration ( node ) ) {
262+ const declarator = node . declarations . find ( ( declarator ) =>
263+ isIdentifier ( declarator . id , { name } )
264+ )
265+ if ( declarator ?. init ) {
266+ declaration = declarator . init
267+ }
268+ } else if ( isExportDefaultDeclaration ( node ) ) {
269+ break
249270 }
250- } else if ( isExportDefaultDeclaration ( node ) ) {
251- break
252271 }
253272 }
254- }
255273
256- if ( isObjectExpression ( declaration ) ) {
257- defineComponentDeclaration = declaration
258- } else if (
259- defineComponentLocalName &&
260- isCallExpression ( declaration ) &&
261- isIdentifier ( declaration . callee , { name : defineComponentLocalName } ) &&
262- isObjectExpression ( declaration . arguments [ 0 ] )
263- ) {
264- defineComponentDeclaration = declaration . arguments [ 0 ]
265- }
274+ if ( isObjectExpression ( declaration ) ) {
275+ defineComponentDeclaration = declaration
276+ } else if (
277+ defineComponentLocalName &&
278+ isCallExpression ( declaration ) &&
279+ isIdentifier ( declaration . callee , { name : defineComponentLocalName } ) &&
280+ isObjectExpression ( declaration . arguments [ 0 ] )
281+ ) {
282+ defineComponentDeclaration = declaration . arguments [ 0 ]
283+ }
266284
267- if ( ! defineComponentDeclaration ) return name
285+ if ( ! defineComponentDeclaration ) return name
268286
269- // 尝试从组件定义对象中获取组件名
270- for ( const prop of defineComponentDeclaration . properties ) {
271- if (
272- isObjectProperty ( prop ) &&
273- isIdentifier ( prop . key ) &&
274- / ( _ _ ) ? n a m e / . test ( prop . key . name ) &&
275- isStringLiteral ( prop . value )
276- ) {
277- return prop . value . value
287+ // 尝试从组件定义对象中获取组件名
288+ for ( const prop of defineComponentDeclaration . properties ) {
289+ if (
290+ isObjectProperty ( prop ) &&
291+ isIdentifier ( prop . key ) &&
292+ / ( _ _ ) ? n a m e / . test ( prop . key . name ) &&
293+ isStringLiteral ( prop . value )
294+ ) {
295+ return prop . value . value || name
296+ }
278297 }
298+ return name
299+ } catch ( e ) {
300+ console . log ( e )
301+ return ''
279302 }
280- return name
281303}
282304
283305function createUsingComponents (
0 commit comments