提交 a81966ec 编写于 作者: fxy060608's avatar fxy060608

feat(uts): 自动导入UTS原生组件类型

上级 415cd4f0
......@@ -43,7 +43,7 @@ describe('compiler:codegen', () => {
test(`UTSComponents:kotlin`, () => {
assert(
`<view><uts-hello/><uts-hello/></view>`,
`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(
`<view><uts-hello/><uts-hello/><uts-hello1/></view>`,
`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(
`<view><uts-hello/><uts-hello/><custom/><custom/><custom1/><index/><index1/></view>`,
`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`
}
},
}
)
})
})
......@@ -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 +
'.' +
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册