1
1
import {
2
- type ExportDefaultDeclaration ,
3
2
type IfStatement ,
4
3
type ImportDeclaration ,
5
4
type Node ,
@@ -8,11 +7,15 @@ import {
8
7
type Program ,
9
8
type Statement ,
10
9
type StringLiteral ,
10
+ assertExportDefaultDeclaration ,
11
+ assertIdentifier ,
11
12
isBlockStatement ,
12
13
isCallExpression ,
14
+ isExportDefaultDeclaration ,
13
15
isIdentifier ,
14
16
isIfStatement ,
15
17
isImportDeclaration ,
18
+ isImportSpecifier ,
16
19
isMemberExpression ,
17
20
isObjectExpression ,
18
21
isObjectProperty ,
@@ -196,40 +199,79 @@ export async function updateMiniProgramGlobalComponents(
196
199
function parseVueComponentName ( filename : string ) {
197
200
let name = path . basename ( removeExt ( filename ) )
198
201
199
- const ast = scriptDescriptors . get ( filename )
202
+ const ast = scriptDescriptors . get ( filename ) ?. ast
200
203
if ( ! ast ) return name
201
204
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 )
207
211
208
212
if ( ! exportDefaultDecliaration ) return name
209
213
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
+ // 获取组件定义对象
210
234
let defineComponentDeclaration : ObjectExpression | null = null
211
235
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
+ }
213
255
214
- if ( declaration . type === 'ObjectExpression' ) {
256
+ if ( isObjectExpression ( declaration ) ) {
215
257
defineComponentDeclaration = declaration
216
258
} 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 ] )
220
263
) {
221
- defineComponentDeclaration =
222
- ( declaration . arguments [ 0 ] as ObjectExpression | undefined ) || null
264
+ defineComponentDeclaration = declaration . arguments [ 0 ]
223
265
}
224
266
225
267
if ( ! defineComponentDeclaration ) return name
226
268
227
269
for ( const prop of defineComponentDeclaration . properties ) {
228
270
if (
229
- prop . type === 'ObjectProperty' &&
230
- prop . key . type === 'Identifier' &&
271
+ isObjectProperty ( prop ) &&
272
+ isIdentifier ( prop . key ) &&
231
273
/ ( _ _ ) ? n a m e / . test ( prop . key . name ) &&
232
- prop . value . type === 'StringLiteral'
274
+ isStringLiteral ( prop . value )
233
275
) {
234
276
return prop . value . value
235
277
}
0 commit comments