Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
acyza committed May 21, 2024
1 parent 09440a2 commit c4e5b8b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 17 deletions.
23 changes: 23 additions & 0 deletions packages/uni-cli-shared/__tests__/usingComponents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ export function createApp() {
},
``
)

await testLocal(
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
Expand All @@ -338,6 +339,28 @@ export function createApp() {
},
``
)

await testLocal(
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
const component = _defineComponent({
__name: "test",
setup(__props) {
return (_ctx, _cache) => {
return {};
};
}
});
export default component;
import "${inputDir}/pages/index/index.vue?vue&type=style&index=0&lang.css";
`,
{
test: '/pages/index/index',
},
``
)
})
})
})
76 changes: 59 additions & 17 deletions packages/uni-cli-shared/src/mp/usingComponents.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
type ExportDefaultDeclaration,
type IfStatement,
type ImportDeclaration,
type Node,
Expand All @@ -8,11 +7,15 @@ import {
type Program,
type Statement,
type StringLiteral,
assertExportDefaultDeclaration,
assertIdentifier,
isBlockStatement,
isCallExpression,
isExportDefaultDeclaration,
isIdentifier,
isIfStatement,
isImportDeclaration,
isImportSpecifier,
isMemberExpression,
isObjectExpression,
isObjectProperty,
Expand Down Expand Up @@ -196,40 +199,79 @@ export async function updateMiniProgramGlobalComponents(
function parseVueComponentName(filename: string) {
let name = path.basename(removeExt(filename))

const ast = scriptDescriptors.get(filename)
const ast = scriptDescriptors.get(filename)?.ast
if (!ast) return name

const exportDefaultDecliaration = scriptDescriptors
.get(filename)
?.ast.body.find(
(v) => v.type === 'ExportDefaultDeclaration'
) as ExportDefaultDeclaration | null
// 获取默认导出定义
const exportDefaultDecliaration = ast.body.find((node) =>
isExportDefaultDeclaration(node)
)

assertExportDefaultDeclaration(exportDefaultDecliaration)

if (!exportDefaultDecliaration) return name

// 获取vue的defineComponent导入变量名
let defineComponentLocalName: string | null = null

for (const node of ast.body) {
if (
isImportDeclaration(node) &&
isStringLiteral(node.source, { value: 'vue' })
) {
const importSpecifer = node.specifiers.find(
(specifer) =>
isImportSpecifier(specifer) &&
isIdentifier(specifer.imported, { name: 'defineComponent' })
)
if (isImportSpecifier(importSpecifer)) {
defineComponentLocalName = importSpecifer.local.name
}
}
}

// 获取组件定义对象
let defineComponentDeclaration: ObjectExpression | null = null

const { declaration } = exportDefaultDecliaration
let { declaration } = exportDefaultDecliaration

// 如果默认导出了变量则尝试查找该变量
if (isIdentifier(declaration)) {
const { name } = declaration
for (const node of ast.body) {
if (isVariableDeclaration(node)) {
assertIdentifier(declaration)
const declarator = node.declarations.find((declarator) =>
isIdentifier(declarator.id, { name })
)
if (declarator?.init) {
declaration = declarator.init
}
} else if (isExportDefaultDeclaration(node)) {
break
}
}
}

if (declaration.type === 'ObjectExpression') {
if (isObjectExpression(declaration)) {
defineComponentDeclaration = declaration
} else if (
declaration.type === 'CallExpression' &&
declaration.callee.type === 'Identifier' &&
/_*defineComponent/.test(declaration.callee.name)
defineComponentLocalName &&
isCallExpression(declaration) &&
isIdentifier(declaration.callee, { name: defineComponentLocalName }) &&
isObjectExpression(declaration.arguments[0])
) {
defineComponentDeclaration =
(declaration.arguments[0] as ObjectExpression | undefined) || null
defineComponentDeclaration = declaration.arguments[0]
}

if (!defineComponentDeclaration) return name

for (const prop of defineComponentDeclaration.properties) {
if (
prop.type === 'ObjectProperty' &&
prop.key.type === 'Identifier' &&
isObjectProperty(prop) &&
isIdentifier(prop.key) &&
/(__)?name/.test(prop.key.name) &&
prop.value.type === 'StringLiteral'
isStringLiteral(prop.value)
) {
return prop.value.value
}
Expand Down

0 comments on commit c4e5b8b

Please sign in to comment.