Skip to content

Commit df5923d

Browse files
committed
chore: 考虑plugin-vue:export-helper
1 parent 60bcea4 commit df5923d

File tree

2 files changed

+134
-86
lines changed

2 files changed

+134
-86
lines changed

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

+48-22
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,10 @@ export function createApp() {
301301
`const _easycom_test = ()=>import('${inputDir}/components/test/test.vue')`
302302
)
303303
})
304-
test(`recursion`, async () => {
305-
await testLocal(
306-
`
304+
describe(`recursion`, () => {
305+
test('base', async () => {
306+
await testLocal(
307+
`
307308
const _sfc_main = {
308309
};
309310
const __BINDING_COMPONENTS__ = '{"index":{"name":"_component_index","type":"unknown"}}';
@@ -314,14 +315,16 @@ export function createApp() {
314315
import _export_sfc from "plugin-vue:export-helper";
315316
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
316317
`,
317-
{
318-
index: '/pages/index/index',
319-
},
320-
``
321-
)
318+
{
319+
index: '/pages/index/index',
320+
},
321+
``
322+
)
323+
})
322324

323-
await testLocal(
324-
`import { defineComponent as _defineComponent } from "vue";
325+
test('use defineComponent', async () => {
326+
await testLocal(
327+
`import { defineComponent as _defineComponent } from "vue";
325328
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
326329
327330
export default /* @__PURE__ */ _defineComponent({
@@ -334,14 +337,16 @@ export function createApp() {
334337
});
335338
import "${inputDir}/pages/index/index.vue?vue&type=style&index=0&lang.css";
336339
`,
337-
{
338-
test: '/pages/index/index',
339-
},
340-
``
341-
)
340+
{
341+
test: '/pages/index/index',
342+
},
343+
``
344+
)
345+
})
342346

343-
await testLocal(
344-
`import { defineComponent as _defineComponent } from "vue";
347+
test('export variable', async () => {
348+
await testLocal(
349+
`import { defineComponent as _defineComponent } from "vue";
345350
const __BINDING_COMPONENTS__ = '{"test":{"name":"test","type":"unknown"}}';
346351
const component = _defineComponent({
347352
__name: "test",
@@ -356,11 +361,32 @@ export function createApp() {
356361
357362
import "${inputDir}/pages/index/index.vue?vue&type=style&index=0&lang.css";
358363
`,
359-
{
360-
test: '/pages/index/index',
361-
},
362-
``
363-
)
364+
{
365+
test: '/pages/index/index',
366+
},
367+
``
368+
)
369+
})
370+
test('use plugin-vue:export-helper', async () => {
371+
await testLocal(
372+
`
373+
const _sfc_main = {
374+
__name: 'Test'
375+
};
376+
const __BINDING_COMPONENTS__ = '{"test":{"name":"_component_test","type":"unknown"}}';
377+
function _sfc_render(_ctx, _cache, $props, $setup, $data, $options) {
378+
return {};
379+
}
380+
import "${filename}?vue&type=style&index=0&lang.css";
381+
import _export_sfc from "plugin-vue:export-helper";
382+
export default /* @__PURE__ */ _export_sfc(_sfc_main, [["render", _sfc_render]]);
383+
`,
384+
{
385+
test: '/pages/index/index',
386+
},
387+
``
388+
)
389+
})
364390
})
365391
})
366392
})

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

+86-64
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@ import {
88
type Statement,
99
type StringLiteral,
1010
assertExportDefaultDeclaration,
11-
assertIdentifier,
1211
isBlockStatement,
1312
isCallExpression,
1413
isExportDefaultDeclaration,
14+
isExpression,
1515
isIdentifier,
1616
isIfStatement,
1717
isImportDeclaration,
18+
isImportDefaultSpecifier,
1819
isImportSpecifier,
1920
isMemberExpression,
2021
isObjectExpression,
@@ -197,87 +198,108 @@ export async function updateMiniProgramGlobalComponents(
197198
}
198199

199200
function parseVueComponentName(filename: string) {
200-
let name = path.basename(removeExt(filename))
201+
try {
202+
let name = path.basename(removeExt(filename))
201203

202-
const ast = scriptDescriptors.get(filename)?.ast
203-
if (!ast) return name
204+
const ast = scriptDescriptors.get(filename)?.ast
205+
if (!ast) return name
204206

205-
// 获取默认导出定义
206-
const exportDefaultDecliaration = ast.body.find((node) =>
207-
isExportDefaultDeclaration(node)
208-
)
207+
// 获取默认导出定义
208+
const exportDefaultDecliaration = ast.body.find((node) =>
209+
isExportDefaultDeclaration(node)
210+
)
209211

210-
assertExportDefaultDeclaration(exportDefaultDecliaration)
212+
assertExportDefaultDeclaration(exportDefaultDecliaration)
211213

212-
if (!exportDefaultDecliaration) return name
214+
if (!exportDefaultDecliaration) return name
213215

214-
// 获取vue的defineComponent导入变量名
215-
let defineComponentLocalName: string | null = null
216+
// 获取vue的defineComponent导入变量名和plugin-vue:export-helper默认导入的本地变量名
217+
let defineComponentLocalName: string | null = null
218+
let exportHelperLocalName: string | null = null
216219

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
220+
for (const node of ast.body) {
221+
if (!isImportDeclaration(node)) continue
222+
if (isStringLiteral(node.source, { value: 'vue' })) {
223+
const importSpecifer = node.specifiers.find(
224+
(specifer) =>
225+
isImportSpecifier(specifer) &&
226+
isIdentifier(specifer.imported, { name: 'defineComponent' })
227+
)
228+
if (isImportSpecifier(importSpecifer)) {
229+
defineComponentLocalName = importSpecifer.local.name
230+
}
231+
} else if (
232+
isStringLiteral(node.source, { value: 'plugin-vue:export-helper' })
233+
) {
234+
const importSpecifer = node.specifiers.find((specifer) =>
235+
isImportDefaultSpecifier(specifer)
236+
)
237+
if (isImportDefaultSpecifier(importSpecifer)) {
238+
exportHelperLocalName = importSpecifer.local.name
239+
}
229240
}
230241
}
231-
}
232-
233-
// 获取组件定义对象
234-
let defineComponentDeclaration: ObjectExpression | null = null
235242

236-
let { declaration } = exportDefaultDecliaration
243+
let { declaration } = exportDefaultDecliaration
244+
// 如果默认导出调用plugin-vue:export-helper默认导入的方法则取方法的第一个参数
245+
if (
246+
exportHelperLocalName &&
247+
isCallExpression(declaration) &&
248+
isIdentifier(declaration.callee, { name: exportHelperLocalName }) &&
249+
isExpression(declaration.arguments[0])
250+
) {
251+
declaration = declaration.arguments[0]
252+
}
237253

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
254+
// 获取组件定义对象
255+
let defineComponentDeclaration: ObjectExpression | null = null
256+
257+
// 如果declaration是变量则尝试查找该变量
258+
if (isIdentifier(declaration)) {
259+
const { name } = declaration
260+
for (const node of ast.body) {
261+
if (isVariableDeclaration(node)) {
262+
const declarator = node.declarations.find((declarator) =>
263+
isIdentifier(declarator.id, { name })
264+
)
265+
if (declarator?.init) {
266+
declaration = declarator.init
267+
}
268+
} else if (isExportDefaultDeclaration(node)) {
269+
break
249270
}
250-
} else if (isExportDefaultDeclaration(node)) {
251-
break
252271
}
253272
}
254-
}
255273

256-
if (isObjectExpression(declaration)) {
257-
defineComponentDeclaration = declaration
258-
} else if (
259-
defineComponentLocalName &&
260-
isCallExpression(declaration) &&
261-
isIdentifier(declaration.callee, { name: defineComponentLocalName }) &&
262-
isObjectExpression(declaration.arguments[0])
263-
) {
264-
defineComponentDeclaration = declaration.arguments[0]
265-
}
274+
if (isObjectExpression(declaration)) {
275+
defineComponentDeclaration = declaration
276+
} else if (
277+
defineComponentLocalName &&
278+
isCallExpression(declaration) &&
279+
isIdentifier(declaration.callee, { name: defineComponentLocalName }) &&
280+
isObjectExpression(declaration.arguments[0])
281+
) {
282+
defineComponentDeclaration = declaration.arguments[0]
283+
}
266284

267-
if (!defineComponentDeclaration) return name
285+
if (!defineComponentDeclaration) return name
268286

269-
// 尝试从组件定义对象中获取组件名
270-
for (const prop of defineComponentDeclaration.properties) {
271-
if (
272-
isObjectProperty(prop) &&
273-
isIdentifier(prop.key) &&
274-
/(__)?name/.test(prop.key.name) &&
275-
isStringLiteral(prop.value)
276-
) {
277-
return prop.value.value
287+
// 尝试从组件定义对象中获取组件名
288+
for (const prop of defineComponentDeclaration.properties) {
289+
if (
290+
isObjectProperty(prop) &&
291+
isIdentifier(prop.key) &&
292+
/(__)?name/.test(prop.key.name) &&
293+
isStringLiteral(prop.value)
294+
) {
295+
return prop.value.value || name
296+
}
278297
}
298+
return name
299+
} catch (e) {
300+
console.log(e)
301+
return ''
279302
}
280-
return name
281303
}
282304

283305
function createUsingComponents(

0 commit comments

Comments
 (0)