11import {
2+ type ExportDefaultDeclaration ,
23 type IfStatement ,
34 type ImportDeclaration ,
45 type Node ,
6+ type ObjectExpression ,
57 type ObjectProperty ,
68 type Program ,
79 type Statement ,
@@ -28,6 +30,7 @@ import { BINDING_COMPONENTS, EXTNAME_VUE_RE } from '../constants'
2830import { isAppVue , normalizeMiniProgramFilename , removeExt } from '../utils'
2931import { cleanUrl , parseVueRequest } from '../vite/utils'
3032import { addMiniProgramUsingComponents } from '../json/mp/jsonFile'
33+ import path from 'path'
3134
3235type BindingComponents = Record <
3336 string ,
@@ -190,11 +193,56 @@ export async function updateMiniProgramGlobalComponents(
190193 }
191194}
192195
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+
193240function createUsingComponents (
194241 bindingComponents : BindingComponents ,
195242 imports : ImportDeclaration [ ] ,
196243 inputDir : string ,
197- normalizeComponentName : ( name : string ) => string
244+ normalizeComponentName : ( name : string ) => string ,
245+ filename ?: string
198246) {
199247 const usingComponents : Record < string , string > = { }
200248 imports . forEach ( ( { source : { value } , specifiers : [ specifier ] } ) => {
@@ -211,6 +259,22 @@ function createUsingComponents(
211259 )
212260 }
213261 } )
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+
214278 return usingComponents
215279}
216280
@@ -250,7 +314,8 @@ export function updateMiniProgramComponentsByMainFilename(
250314 bindingComponents ,
251315 imports ,
252316 inputDir ,
253- normalizeComponentName
317+ normalizeComponentName ,
318+ mainFilename
254319 )
255320 )
256321}
@@ -347,6 +412,7 @@ interface ParseDescriptor {
347412}
348413export interface ScriptDescriptor extends TemplateDescriptor {
349414 setupBindingComponents : BindingComponents
415+ ast : Program
350416}
351417
352418async function parseGlobalDescriptor (
@@ -396,6 +462,7 @@ export async function parseScriptDescriptor(
396462 bindingComponents : parseComponents ( ast ) ,
397463 setupBindingComponents : findBindingComponents ( ast . body ) ,
398464 imports,
465+ ast,
399466 }
400467
401468 scriptDescriptors . set ( filename , descriptor )
0 commit comments