Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 小程序组件自调用时自动添加到usingComponents #4913

Open
wants to merge 13 commits into
base: next
Choose a base branch
from
Prev Previous commit
Next Next commit
chore: 添加测试用例
  • Loading branch information
acyza committed May 21, 2024
commit 09440a2114d2ea3e2233704a811f351ec4cd247f
38 changes: 38 additions & 0 deletions packages/uni-cli-shared/__tests__/usingComponents.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,5 +301,43 @@ export function createApp() {
`const _easycom_test = ()=>import('${inputDir}/components/test/test.vue')`
)
})
test(`recursion`, async () => {
await testLocal(
`
const _sfc_main = {
};
const __BINDING_COMPONENTS__ = '{"index":{"name":"_component_index","type":"unknown"}}';
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
return {};
}
import "${filename}?vue&type=style&index=0&lang.css";
import _export_sfc from "plugin-vue:export-helper";
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
`,
{
index: '/pages/index/index',
},
``
)
await testLocal(
`import { defineComponent as _defineComponent } from "vue";
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';

export default /* @__PURE__ */ _defineComponent({
__name: "test",
setup(__props) {
return (_ctx, _cache) => {
return {};
};
}
});
import "${inputDir}/pages/index/index.vue?vue&type=style&index=0&lang.css";
`,
{
test: '/pages/index/index',
},
``
)
})
})
})
13 changes: 8 additions & 5 deletions packages/uni-cli-shared/src/mp/usingComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ function parseVueComponentName(filename: string) {
} else if (
declaration.type === 'CallExpression' &&
declaration.callee.type === 'Identifier' &&
declaration.callee.name === '_defineComponent'
/_*defineComponent/.test(declaration.callee.name)
) {
defineComponentDeclaration =
(declaration.arguments[0] as ObjectExpression | undefined) || null
Expand All @@ -228,7 +228,7 @@ function parseVueComponentName(filename: string) {
if (
prop.type === 'ObjectProperty' &&
prop.key.type === 'Identifier' &&
prop.key.name === '__name' &&
/(__)?name/.test(prop.key.name) &&
prop.value.type === 'StringLiteral'
) {
return prop.value.value
Expand Down Expand Up @@ -261,15 +261,18 @@ function createUsingComponents(
})

if (filename) {
const name = parseVueComponentName(filename)
const componentName = normalizeComponentName(
hyphenate(parseVueComponentName(filename))
)

if (
!Object.keys(bindingComponents).find(
(v) => bindingComponents[v].tag === name
(v) =>
normalizeComponentName(hyphenate(bindingComponents[v].tag)) ===
componentName
)
)
return usingComponents
const componentName = normalizeComponentName(hyphenate(name))
if (!usingComponents[componentName]) {
usingComponents[componentName] = addLeadingSlash(
removeExt(normalizeMiniProgramFilename(filename, inputDir))
Expand Down