1
1
import {
2
+ type ExportDefaultDeclaration ,
2
3
type IfStatement ,
3
4
type ImportDeclaration ,
4
5
type Node ,
6
+ type ObjectExpression ,
5
7
type ObjectProperty ,
6
8
type Program ,
7
9
type Statement ,
@@ -28,6 +30,7 @@ import { BINDING_COMPONENTS, EXTNAME_VUE_RE } from '../constants'
28
30
import { isAppVue , normalizeMiniProgramFilename , removeExt } from '../utils'
29
31
import { cleanUrl , parseVueRequest } from '../vite/utils'
30
32
import { addMiniProgramUsingComponents } from '../json/mp/jsonFile'
33
+ import path from 'path'
31
34
32
35
type BindingComponents = Record <
33
36
string ,
@@ -190,11 +193,56 @@ export async function updateMiniProgramGlobalComponents(
190
193
}
191
194
}
192
195
196
+ function parseVueComponentName ( filename : string ) {
197
+ let name = path . basename ( removeExt ( filename ) )
198
+
199
+ const ast = scriptDescriptors . get ( filename )
200
+ if ( ! ast ) return name
201
+
202
+ const exportDefaultDecliaration = scriptDescriptors
203
+ . get ( filename )
204
+ ?. ast . body . find (
205
+ ( v ) => v . type === 'ExportDefaultDeclaration'
206
+ ) as ExportDefaultDeclaration | null
207
+
208
+ if ( ! exportDefaultDecliaration ) return name
209
+
210
+ let defineComponentDeclaration : ObjectExpression | null = null
211
+
212
+ const { declaration } = exportDefaultDecliaration
213
+
214
+ if ( declaration . type === 'ObjectExpression' ) {
215
+ defineComponentDeclaration = declaration
216
+ } else if (
217
+ declaration . type === 'CallExpression' &&
218
+ declaration . callee . type === 'Identifier' &&
219
+ declaration . callee . name === '_defineComponent'
220
+ ) {
221
+ defineComponentDeclaration =
222
+ ( declaration . arguments [ 0 ] as ObjectExpression | undefined ) || null
223
+ }
224
+
225
+ if ( ! defineComponentDeclaration ) return name
226
+
227
+ for ( const prop of defineComponentDeclaration . properties ) {
228
+ if (
229
+ prop . type === 'ObjectProperty' &&
230
+ prop . key . type === 'Identifier' &&
231
+ prop . key . name === '__name' &&
232
+ prop . value . type === 'StringLiteral'
233
+ ) {
234
+ return prop . value . value
235
+ }
236
+ }
237
+ return name
238
+ }
239
+
193
240
function createUsingComponents (
194
241
bindingComponents : BindingComponents ,
195
242
imports : ImportDeclaration [ ] ,
196
243
inputDir : string ,
197
- normalizeComponentName : ( name : string ) => string
244
+ normalizeComponentName : ( name : string ) => string ,
245
+ filename ?: string
198
246
) {
199
247
const usingComponents : Record < string , string > = { }
200
248
imports . forEach ( ( { source : { value } , specifiers : [ specifier ] } ) => {
@@ -211,6 +259,22 @@ function createUsingComponents(
211
259
)
212
260
}
213
261
} )
262
+
263
+ if ( filename ) {
264
+ const componentName = parseVueComponentName ( filename )
265
+
266
+ if (
267
+ Object . keys ( bindingComponents ) . find (
268
+ ( v ) => bindingComponents [ v ] . tag === componentName
269
+ ) &&
270
+ ! usingComponents [ componentName ]
271
+ ) {
272
+ usingComponents [ componentName ] = addLeadingSlash (
273
+ removeExt ( normalizeMiniProgramFilename ( filename , inputDir ) )
274
+ )
275
+ }
276
+ }
277
+
214
278
return usingComponents
215
279
}
216
280
@@ -250,7 +314,8 @@ export function updateMiniProgramComponentsByMainFilename(
250
314
bindingComponents ,
251
315
imports ,
252
316
inputDir ,
253
- normalizeComponentName
317
+ normalizeComponentName ,
318
+ mainFilename
254
319
)
255
320
)
256
321
}
@@ -347,6 +412,7 @@ interface ParseDescriptor {
347
412
}
348
413
export interface ScriptDescriptor extends TemplateDescriptor {
349
414
setupBindingComponents : BindingComponents
415
+ ast : Program
350
416
}
351
417
352
418
async function parseGlobalDescriptor (
@@ -396,6 +462,7 @@ export async function parseScriptDescriptor(
396
462
bindingComponents : parseComponents ( ast ) ,
397
463
setupBindingComponents : findBindingComponents ( ast . body ) ,
398
464
imports,
465
+ ast,
399
466
}
400
467
401
468
scriptDescriptors . set ( filename , descriptor )
0 commit comments