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

wip(x-android): tsc

上级 d0cb2ad8
......@@ -2,12 +2,10 @@ import type { SFCDescriptor } from '@vue/compiler-sfc'
export function genScript(
{ script }: SFCDescriptor,
_options: { filename: string }
{ genDefaultAs }: { genDefaultAs?: string }
) {
if (!script) {
return `
export default {}
`
return genDefaultScriptCode(genDefaultAs)
}
return (
'\n'.repeat(script.loc.start.line - 1) +
......@@ -15,3 +13,13 @@ export default {}
`
)
}
export function genDefaultScriptCode(genDefaultAs?: string) {
if (genDefaultAs) {
return `const ${genDefaultAs} = defineComponent({})
export default ${genDefaultAs}`
}
return `
export default {}
`
}
......@@ -9,7 +9,7 @@ import { getDescriptor, getResolvedOptions } from '../descriptorCache'
export function genTemplate(
{ template }: SFCDescriptor,
options: TemplateCompilerOptions
options: TemplateCompilerOptions & { genDefaultAs?: string }
) {
if (!template || !template.content) {
return {
......
......@@ -209,7 +209,7 @@ const UTS_COMPONENT_ELEMENT_IMPORTS = `/*UTS-COMPONENTS-IMPORTS*/`
export function generate(
ast: RootNode,
options: CodegenOptions = {}
options: CodegenOptions & { genDefaultAs?: string } = {}
): CodegenResult {
const context = createCodegenContext(ast, options)
const { mode, deindent, indent, push, newline } = context
......
......@@ -21,13 +21,17 @@ export const __COMPAT__ = false
export function genRenderFunctionDecl({
className = '',
genDefaultAs,
}: // inline = false,
TemplateCompilerOptions): string {
TemplateCompilerOptions & { genDefaultAs?: string }): string {
// if(inline){
// return `(): VNode | null =>`
// }
// 调整返回值类型为 any | null, 支持 <template>some text</template>
return `function ${className}Render(): any | null`
const thisCode = genDefaultAs
? `this: InstanceType<typeof ${genDefaultAs}>`
: ''
return `function ${className}Render(${thisCode}): any | null`
}
export function rewriteObjectExpression(
......
......@@ -4,6 +4,7 @@ import { preUVueHtml, preUVueJs } from '@dcloudio/uni-cli-shared'
import type * as _compiler from '@vue/compiler-sfc'
import type { CompilerError, SFCDescriptor } from '@vue/compiler-sfc'
import { parseUTSRelativeFilename } from '../utils'
import { scriptIdentifier } from './sfc/script'
export interface ResolvedOptions {
compiler: typeof _compiler
......@@ -11,6 +12,7 @@ export interface ResolvedOptions {
sourceMap: boolean
targetLanguage?: 'kotlin'
classNamePrefix?: string
genDefaultAs?: string
}
export function getResolvedOptions(): ResolvedOptions {
......@@ -23,6 +25,7 @@ export function getResolvedOptions(): ResolvedOptions {
// eslint-disable-next-line no-restricted-globals
compiler: require('@vue/compiler-sfc'),
targetLanguage: process.env.UNI_UTS_TARGET_LANGUAGE as 'kotlin',
genDefaultAs: scriptIdentifier,
}
}
......
......@@ -65,4 +65,5 @@ export interface ResolvedOptions extends Options {
className?: string
classNamePrefix?: string
componentType: 'app' | 'page' | 'component'
genDefaultAs?: string
}
......@@ -30,6 +30,7 @@ import { createResolveError, parseImports, wrapResolve } from '../../utils'
import { genTemplateCode } from '../code/template'
import { resolveGenTemplateCodeOptions } from './template'
import { addExtApiComponents } from '../../../utils'
import { genDefaultScriptCode } from '../code/script'
export async function transformMain(
code: string,
......@@ -129,10 +130,7 @@ export async function transformMain(
const stylesCode = await genStyleCode(descriptor, pluginContext)
const utsOutput: string[] = [
scriptCode ||
`
export default {}
`,
scriptCode || genDefaultScriptCode(options.genDefaultAs),
templateCode,
`/*${className}Styles*/\n`,
]
......@@ -251,13 +249,13 @@ export const ${genUTSComponentPublicInstanceImported(
async function genScriptCode(
descriptor: SFCDescriptor,
options: ResolvedOptions
options: ResolvedOptions & { genDefaultAs?: string }
): Promise<{
code: string
map: RawSourceMap | undefined
bindingMetadata?: BindingMetadata
}> {
let scriptCode = `export default {}`
let scriptCode = genDefaultScriptCode(options.genDefaultAs)
let map: RawSourceMap | undefined
const script = resolveScript(descriptor, options)
......
......@@ -12,7 +12,7 @@ import {
parseUTSComponent,
} from '@dcloudio/uni-cli-shared'
import { getResolvedScript } from './script'
import { getResolvedScript, scriptIdentifier } from './script'
import type { ResolvedOptions } from '.'
import type { TemplateCompilerOptions } from '../compiler/options'
import { parseUTSImportFilename } from '../../utils'
......@@ -32,10 +32,11 @@ export function resolveGenTemplateCodeOptions(
preprocessLang?: string
preprocessOptions?: any
}
): TemplateCompilerOptions {
): TemplateCompilerOptions & { genDefaultAs?: string } {
const block = descriptor.template
if (!block) {
return {
genDefaultAs: scriptIdentifier,
...options,
filename: relativeFileName,
}
......@@ -50,6 +51,7 @@ export function resolveGenTemplateCodeOptions(
}
}
return {
genDefaultAs: scriptIdentifier,
...options,
filename: relativeFileName,
inMap: descriptor.template?.map,
......
......@@ -65,8 +65,8 @@ export function runUTS2Kotlin(
const vueRuntimeDts = [
path.resolve(
hbxLanguageServicePath,
'uniappx/node_modules/@vue/runtime-core/index.d.ts'
pluginPath,
'uniapp-cli-vite/node_modules/@vue/runtime-core/dist/runtime-core.d.ts'
),
]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册