diff --git a/packages/uni-app-uts/__tests__/android/codegen.spec.ts b/packages/uni-app-uts/__tests__/android/codegen.spec.ts index 2f1ef10a547c3e699ff482b7063a63044e7e682e..37704ff257181621889a684853cb9803e1cb3ddb 100644 --- a/packages/uni-app-uts/__tests__/android/codegen.spec.ts +++ b/packages/uni-app-uts/__tests__/android/codegen.spec.ts @@ -43,7 +43,7 @@ describe('compiler:codegen', () => { test(`UTSComponents:kotlin`, () => { assert( ``, - `function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name)\n ])\n}`, + `import { UtsHelloElement } from 'uts.sdk.modules.utsHello'\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name)\n ])\n}`, { targetLanguage: 'kotlin', mode: 'function', @@ -58,6 +58,29 @@ describe('compiler:codegen', () => { }, } ) + assert( + ``, + `import { UtsHelloElement } from 'uts.sdk.modules.utsHello'\nimport { UtsHello1Element } from 'uts.sdk.modules.utsHello'\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHello1Component.name)\n ])\n}`, + { + targetLanguage: 'kotlin', + mode: 'function', + parseUTSComponent(name) { + if (name === 'uts-hello') { + return { + className: 'UtsHelloComponent', + namespace: 'uts.sdk.modules.utsHello', + source: '@/uni_modules/uts-hello', + } + } else if (name === 'uts-hello1') { + return { + className: 'UtsHello1Component', + namespace: 'uts.sdk.modules.utsHello', + source: '@/uni_modules/uts-hello', + } + } + }, + } + ) }) test(`easycom`, () => { assert( @@ -87,4 +110,48 @@ const _component_index = resolveComponent("index", true) } ) }) + test(`UTSComponents and easycom`, () => { + assert( + ``, + `import { UtsHelloElement } from 'uts.sdk.modules.utsHello' +import _easycom_custom, { GenComponentsCustomCustomComponentPublicInstance as CustomComponentPublicInstance } from '@/components/custom/custom.vue' +import _easycom_custom1, { GenComponentsCustom1Custom1ComponentPublicInstance as Custom1ComponentPublicInstance } from '@/components/custom1/custom1.vue' +import _easycom_index, { GenComponentsIndexIndexComponentPublicInstance as IndexComponentPublicInstance } from '@/components/index/index.vue' +function PagesIndexIndexRender(): VNode | null { +const _ctx = this +const _component_custom = resolveEasyComponent("custom",_easycom_custom) +const _component_custom1 = resolveEasyComponent("custom1",_easycom_custom1) +const _component_index = resolveEasyComponent("index",_easycom_index) +const _component_index1 = resolveComponent("index1") + + return createElementVNode("view", null, [ + createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name), + createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name), + createVNode(_component_custom), + createVNode(_component_custom), + createVNode(_component_custom1), + createVNode(_component_index), + createVNode(_component_index1) + ]) +}`, + { + targetLanguage: 'kotlin', + mode: 'function', + parseUTSComponent(name) { + if (name === 'uts-hello') { + return { + className: 'UtsHelloComponent', + namespace: 'uts.sdk.modules.utsHello', + source: '@/uni_modules/uts-hello', + } + } + }, + matchEasyCom(tag) { + if (tag.startsWith('custom') || tag === 'index') { + return `@/components/${tag}/${tag}.vue` + } + }, + } + ) + }) }) diff --git a/packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts b/packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts index cfaf4988dfafbf94bcf2efc846d3d36b96ecb76e..476c71a5c4c14790aee1a64b0c0b2ca7a6f523d1 100644 --- a/packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts +++ b/packages/uni-app-uts/src/plugins/android/uvue/compiler/codegen.ts @@ -69,6 +69,7 @@ export interface CodegenContext code: string importEasyComponents: string[] importUTSComponents: string[] + importUTSElements: string[] line: number column: number offset: number @@ -108,6 +109,7 @@ function createCodegenContext( code: ``, importEasyComponents: [], importUTSComponents: [], + importUTSElements: [], column: 1, line: 1, offset: 0, @@ -181,6 +183,8 @@ function createCodegenContext( return context } +const UTS_COMPONENT_ELEMENT_IMPORTS = `/*UTS-COMPONENTS-IMPORTS*/` + export function generate( ast: RootNode, options: CodegenOptions @@ -188,6 +192,7 @@ export function generate( const context = createCodegenContext(ast, options) const { mode, deindent, indent, push, newline } = context if (mode === 'function') { + push(UTS_COMPONENT_ELEMENT_IMPORTS) genEasyComImports(ast.components, context) push(genRenderFunctionDecl(options) + ` {`) newline() @@ -221,6 +226,14 @@ export function generate( deindent() push(`}`) } + + context.code = context.code.replace( + UTS_COMPONENT_ELEMENT_IMPORTS, + context.importUTSElements.length + ? context.importUTSElements.join('\n') + '\n' + : '' + ) + return { code: context.code, importEasyComponents: context.importEasyComponents, @@ -487,7 +500,12 @@ function genComment(node: CommentNode, context: CodegenContext) { function parseTag( tag: string | symbol | CallExpression, - { parseUTSComponent, targetLanguage, importUTSComponents }: CodegenContext + { + parseUTSComponent, + targetLanguage, + importUTSComponents, + importUTSElements, + }: CodegenContext ) { if (isString(tag)) { // 原生UTS组件 @@ -500,6 +518,13 @@ function parseTag( if (!importUTSComponents.includes(importCode)) { importUTSComponents.push(importCode) } + const importElementCode = `import { ${utsComponentOptions.className.replace( + /Component$/, + 'Element' + )} } from '${utsComponentOptions.namespace}'` + if (!importUTSElements.includes(importElementCode)) { + importUTSElements.push(importElementCode) + } return ( utsComponentOptions.namespace + '.' +