Skip to content

Commit c4e5b8b

Browse files
committed
chore: update
1 parent 09440a2 commit c4e5b8b

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

packages/uni-cli-shared/__tests__/usingComponents.spec.ts

+23
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ export function createApp() {
319319
},
320320
``
321321
)
322+
322323
await testLocal(
323324
`import { defineComponent as _defineComponent } from "vue";
324325
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
@@ -338,6 +339,28 @@ export function createApp() {
338339
},
339340
``
340341
)
342+
343+
await testLocal(
344+
`import { defineComponent as _defineComponent } from "vue";
345+
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
346+
const component = _defineComponent({
347+
__name: "test",
348+
setup(__props) {
349+
return (_ctx, _cache) => {
350+
return {};
351+
};
352+
}
353+
});
354+
355+
export default component;
356+
357+
import "${inputDir}/pages/index/index.vue?vue&type=style&index=0&lang.css";
358+
`,
359+
{
360+
test: '/pages/index/index',
361+
},
362+
``
363+
)
341364
})
342365
})
343366
})

packages/uni-cli-shared/src/mp/usingComponents.ts

+59-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import {
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(
196199
function 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-
/_*defineComponent/.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
/(__)?name/.test(prop.key.name) &&
232-
prop.value.type === 'StringLiteral'
274+
isStringLiteral(prop.value)
233275
) {
234276
return prop.value.value
235277
}

0 commit comments

Comments
 (0)