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

wip(uts): compiler

上级 973abfc7
......@@ -15,7 +15,8 @@ export function assert(
options: CompilerOptions = { targetLanguage: 'kotlin' }
) {
const compilerOptions: CompilerOptions = {
filename: 'PagesIndexIndex',
filename: 'pages/index/index.uvue',
className: 'PagesIndexIndex',
prefixIdentifiers: true,
...options,
}
......
......@@ -24,6 +24,8 @@
"@dcloudio/uni-i18n": "3.0.0-alpha-3090020230826001",
"@dcloudio/uni-nvue-styler": "3.0.0-alpha-3090020230826001",
"@dcloudio/uni-shared": "3.0.0-alpha-3090020230826001",
"@jridgewell/gen-mapping": "^0.3.3",
"@jridgewell/trace-mapping": "^0.3.19",
"@rollup/pluginutils": "^4.2.0",
"@vue/compiler-core": "3.2.47",
"@vue/compiler-sfc": "3.2.47",
......
......@@ -54,7 +54,9 @@ import { isBinaryExpression } from '@babel/types'
type CodegenNode = TemplateChildNode | JSChildNode | SSRCodegenNode
export interface CodegenContext
extends Required<Omit<CodegenOptions, 'sourceMapGeneratedLine'>> {
extends Required<
Omit<CodegenOptions, 'sourceMapGeneratedLine' | 'className'>
> {
source: string
code: string
importEasyComponents: string[]
......@@ -80,7 +82,6 @@ function createCodegenContext(
prefixIdentifiers = false,
bindingMetadata = {},
sourceMap = false,
sourceMapGeneratedLine = 1,
filename = '',
matchEasyCom = NOOP,
parseUTSComponent = NOOP,
......@@ -98,7 +99,7 @@ function createCodegenContext(
importEasyComponents: [],
importUTSComponents: [],
column: 1,
line: sourceMapGeneratedLine ?? 1,
line: 1,
offset: 0,
indentLevel: 0,
map: undefined,
......
......@@ -20,6 +20,10 @@ interface SharedTransformCodegenOptions {
* @default ''
*/
filename?: string
/**
* 编译的模板类名
*/
className?: string
}
export interface CodegenOptions extends SharedTransformCodegenOptions {
/**
......@@ -32,10 +36,6 @@ export interface CodegenOptions extends SharedTransformCodegenOptions {
* @default false
*/
sourceMap?: boolean
/**
*
*/
sourceMapGeneratedLine?: number
/**
* 匹配 easycom 组件
* @param tag
......
......@@ -78,7 +78,7 @@ export interface ImportItem {
}
export interface TransformContext
extends Required<Omit<TransformOptions, 'filename'>> {
extends Required<Omit<TransformOptions, 'filename' | 'className'>> {
selfName: string | null
root: RootNode
helpers: Map<symbol, number>
......
......@@ -11,11 +11,11 @@ import { CompilerError } from './errors'
export function genRenderFunctionDecl({
targetLanguage,
filename,
className,
}: CompilerOptions): string {
return `${
targetLanguage === 'kotlin' ? '@Suppress("UNUSED_PARAMETER") ' : ''
}function ${filename}Render(): VNode | null`
}function ${className}Render(): VNode | null`
}
export function rewriteObjectExpression(
......
......@@ -2,7 +2,12 @@ import path from 'path'
import fs from 'fs-extra'
import type { Plugin } from 'vite'
import type { SFCBlock, SFCDescriptor, SFCParseResult } from '@vue/compiler-sfc'
import {
MagicString,
type SFCBlock,
type SFCDescriptor,
type SFCParseResult,
} from '@vue/compiler-sfc'
import type { TransformPluginContext } from 'rollup'
import { isString } from '@vue/shared'
......@@ -16,6 +21,13 @@ import {
import type { RawSourceMap } from 'source-map-js'
import { addMapping, fromMap, toEncodedMap } from '@jridgewell/gen-mapping'
import {
TraceMap,
eachMapping,
EncodedSourceMap,
} from '@jridgewell/trace-mapping'
import {
ResolvedOptions,
createDescriptor,
......@@ -98,7 +110,7 @@ export function uniAppUVuePlugin(): Plugin {
}
if (!query.vue) {
// main request
const { errors, uts, js, templateSourceMap } = await transformVue(
const { errors, uts, js, sourceMap } = await transformVue(
code,
filename,
options,
......@@ -118,11 +130,11 @@ export function uniAppUVuePlugin(): Plugin {
fileName,
source: uts,
})
if (templateSourceMap) {
if (sourceMap) {
this.emitFile({
type: 'asset',
fileName: removeExt(fileName) + '.template.map',
source: JSON.stringify(templateSourceMap),
source: JSON.stringify(sourceMap),
})
}
return {
......@@ -187,7 +199,7 @@ interface TransformVueResult {
uts?: string
js?: string
descriptor: SFCDescriptor
templateSourceMap?: RawSourceMap
sourceMap?: RawSourceMap
}
export async function transformVue(
......@@ -208,23 +220,25 @@ export async function transformVue(
return { errors, descriptor }
}
const isApp = isAppVue(filename)
const fileName = path.relative(options.root, filename)
const fileName = normalizePath(path.relative(options.root, filename))
const className = genClassName(fileName, options.classNamePrefix)
let templateCode = ''
let templateImportEasyComponentsCode = ''
let templateImportUTSComponentsCode = ''
let templateSourceMap
const needSourceMap = process.env.UNI_APP_X_TEMPLATE_SOURCEMAP
? true
: process.env.NODE_ENV !== 'production'
let templateSourceMap: RawSourceMap | undefined
if (!isApp) {
const templateResult = genTemplate(descriptor, {
targetLanguage: options.targetLanguage as any,
mode: 'function',
filename: className,
filename: fileName,
className: className,
prefixIdentifiers: true,
sourceMap: process.env.NODE_ENV !== 'production',
// TODO 将sourceMap的行数调整为script的最后一行,后续需要考虑setup
sourceMapGeneratedLine: descriptor.script
? descriptor.script.loc.end.line + 1
: 1,
// 方便测试,build模式也提供sourceMap
// sourceMap: false,
sourceMap: needSourceMap,
matchEasyCom: (tag, uts) => {
const source = matchEasycom(tag)
if (uts && source) {
......@@ -265,6 +279,49 @@ export async function transformVue(
uts: utsCode,
js: jsCode,
descriptor,
templateSourceMap,
sourceMap: needSourceMap
? createSourceMap(
descriptor.script ? descriptor.script.loc.end.line + 1 : 1,
new MagicString(code).generateMap({
hires: true,
source: fileName,
includeContent: true,
}) as unknown as RawSourceMap,
templateSourceMap
)
: undefined,
}
}
function createSourceMap(
scriptCodeOffset: number,
scriptMap: RawSourceMap,
templateMap?: RawSourceMap
) {
if (!templateMap) {
return scriptMap
}
const gen = fromMap(
// version property of result.map is declared as string
// but actually it is `3`
scriptMap as Omit<RawSourceMap, 'version'> as EncodedSourceMap
)
const tracer = new TraceMap(
// same above
templateMap as Omit<RawSourceMap, 'version'> as EncodedSourceMap
)
// const offset = (scriptCode.match(/\r?\n/g)?.length ?? 0) + 1
eachMapping(tracer, (m) => {
if (m.source == null) return
addMapping(gen, {
source: m.source,
original: { line: m.originalLine, column: m.originalColumn - 1 },
generated: {
line: m.generatedLine + scriptCodeOffset - 1,
column: m.generatedColumn,
},
})
})
return toEncodedMap(gen) as unknown as RawSourceMap
}
......@@ -114,6 +114,9 @@ export async function compileApp(entry: string, options: CompileAppOptions) {
noColor: true,
split,
disableSplitManifest: options.disableSplitManifest,
uniAppX: {
uvueOutDir: uvueOutDir(),
},
transform: {
uniExtApiDefaultNamespace: 'io.dcloud.uniapp.extapi',
uniExtApiNamespaces: extApis,
......@@ -150,6 +153,10 @@ export async function compileApp(entry: string, options: CompileAppOptions) {
return runKotlinDev(options, result as RunKotlinDevResult, hasCache)
}
export function uvueOutDir() {
return path.join(process.env.UNI_OUTPUT_DIR, '../.uvue')
}
function kotlinDir(outputDir: string) {
return (
process.env.UNI_APP_X_CACHE_DIR || path.resolve(outputDir, '../.kotlin')
......
......@@ -46,6 +46,9 @@ export type UTSOutputOptions = {
isPlugin?: boolean
split?: boolean
disableSplitManifest?: boolean
uniAppX?: {
uvueOutDir: string
}
transform?: {
paramDefaultValue?: boolean
constructorInvocation?: boolean
......
......@@ -379,6 +379,12 @@ importers:
'@dcloudio/uni-shared':
specifier: 3.0.0-alpha-3090020230826001
version: link:../uni-shared
'@jridgewell/gen-mapping':
specifier: ^0.3.3
version: 0.3.3
'@jridgewell/trace-mapping':
specifier: ^0.3.19
version: 0.3.19
'@rollup/pluginutils':
specifier: ^4.2.0
version: 4.2.0
......@@ -1529,7 +1535,7 @@ packages:
resolution: {integrity: sha512-hoyByceqwKirw7w3Z7gnIIZC3Wx3J484Y3L/cMpXFbr7d9ZQj2mODrirNzcJa+SM3UlpWXYvKV4RlRpFXlWgXg==}
engines: {node: '>=6.0.0'}
dependencies:
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
dev: false
/@ampproject/remapping@2.2.1:
......@@ -1537,7 +1543,7 @@ packages:
engines: {node: '>=6.0.0'}
dependencies:
'@jridgewell/gen-mapping': 0.3.3
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
/@babel/code-frame@7.21.4:
resolution: {integrity: sha512-LYvhNKfwWSPpocw8GI7gpK2nq3HSDuEPC/uSYaALSJu9xjsalaaYFOq0Pwt5KmVqwEbZlDu81aLXwBOmD/Fv9g==}
......@@ -1585,7 +1591,7 @@ packages:
dependencies:
'@babel/types': 7.21.5
'@jridgewell/gen-mapping': 0.3.3
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
jsesc: 2.5.2
/@babel/helper-annotate-as-pure@7.18.6:
......@@ -3424,7 +3430,7 @@ packages:
'@jest/test-result': 29.5.0
'@jest/transform': 29.5.0
'@jest/types': 29.5.0
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
'@types/node': 18.16.2
chalk: 4.1.2
collect-v8-coverage: 1.0.1
......@@ -3467,7 +3473,7 @@ packages:
resolution: {integrity: sha512-qyt/mb6rLyd9j1jUts4EQncvS6Yy3PM9HghnNv86QBlV+zdL2inCdK1tuVlL+J+lpiw2BI67qXOrX3UurBqQ1w==}
engines: {node: ^14.15.0 || ^16.10.0 || >=18.0.0}
dependencies:
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
callsites: 3.1.0
graceful-fs: 4.2.11
dev: true
......@@ -3543,7 +3549,7 @@ packages:
dependencies:
'@babel/core': 7.21.3
'@jest/types': 29.5.0
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
babel-plugin-istanbul: 6.1.1
chalk: 4.1.2
convert-source-map: 2.0.0
......@@ -4006,7 +4012,7 @@ packages:
dependencies:
'@jridgewell/set-array': 1.1.2
'@jridgewell/sourcemap-codec': 1.4.15
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
/@jridgewell/resolve-uri@3.1.0:
resolution: {integrity: sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w==}
......@@ -4020,20 +4026,17 @@ packages:
resolution: {integrity: sha512-b+fsZXeLYi9fEULmfBrhxn4IrPlINf8fiNarzTof004v3lFdntdwa9PF7vFJqm3mg7s+ScJMxXaE3Acp1irZcg==}
dependencies:
'@jridgewell/gen-mapping': 0.3.3
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
dev: true
/@jridgewell/sourcemap-codec@1.4.14:
resolution: {integrity: sha512-XPSJHWmi394fuUuzDnGz1wiKqWfo1yXecHQMRf2l6hztTO+nPru658AyDngaBe7isIxEkRsPR3FZh+s7iVa4Uw==}
/@jridgewell/sourcemap-codec@1.4.15:
resolution: {integrity: sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==}
/@jridgewell/trace-mapping@0.3.18:
resolution: {integrity: sha512-w+niJYzMHdd7USdiH2U6869nqhD2nbfZXND5Yp93qIbEmnDNk7PD48o+YchRVpzMU7M6jVCbenTR7PA1FLQ9pA==}
/@jridgewell/trace-mapping@0.3.19:
resolution: {integrity: sha512-kf37QtfW+Hwx/buWGMPcR60iF9ziHa6r/CZJIHbmcm4+0qrXiVdxegAH0F6yddEVQ7zdkjcGCgCzUu+BcbhQxw==}
dependencies:
'@jridgewell/resolve-uri': 3.1.0
'@jridgewell/sourcemap-codec': 1.4.14
'@jridgewell/sourcemap-codec': 1.4.15
/@jsbits/escape-regex-str@1.0.3:
resolution: {integrity: sha512-0800vYI2fg1nuUq/T9Tqv8DMOLLNiRAltxFbKIbR7szrvW6qTuI2+zGK51hV7NAAmUr4G83Kvpj2R6Yyg07iIw==}
......@@ -10481,7 +10484,7 @@ packages:
resolution: {integrity: sha512-6z3GW9x8G1gd+JIIgQQQxXuiJtCXeAjp6RaPEPLv62mH3iPHPxV6W3robxtCzNErRo6ZwTmzWhsbNvjyEBKzKA==}
engines: {node: '>=10.12.0'}
dependencies:
'@jridgewell/trace-mapping': 0.3.18
'@jridgewell/trace-mapping': 0.3.19
'@types/istanbul-lib-coverage': 2.0.4
convert-source-map: 1.9.0
dev: true
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册