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

wip(uts): compiler

上级 bbe6c173
......@@ -123,9 +123,11 @@ function normalizeCode(code: string, isMain = false) {
return code
}
return `
export function main() {
definePageRoutes()
createPage(__uniRoutes[0])
${code}
export function main(app: IApp) {
defineAppConfig();
definePageRoutes();
(createApp()['app'] as VueApp).mount(app);
}
`
}
......@@ -4,7 +4,7 @@ import { PAGES_JSON_UTS, normalizePagesJson } from '@dcloudio/uni-cli-shared'
import type { OutputAsset } from 'rollup'
import type { Plugin } from 'vite'
import { ENTRY_FILENAME, genClassName } from './utils'
import { ENTRY_FILENAME, genClassName, stringifyMap } from './utils'
function isPages(id: string) {
return id.endsWith(PAGES_JSON_UTS)
......@@ -40,7 +40,11 @@ export function uniAppPagesPlugin(): Plugin {
const className = genClassName(page.path)
imports.push(page.path)
routes.push(
`{ path: "${page.path}", component: ${className}Class, meta: { isQuit: true, navigationBar: { titleText: "uni-app" } as PageNavigationBar } as PageMeta } as PageRoute`
`{ path: "${
page.path
}", component: ${className}Class, meta: { isQuit: true } as PageMeta, style: ${stringifyPageStyle(
page.style
)} } as PageRoute`
)
})
return `${imports.map((p) => `import('./${p}.uvue')`).join('\n')}
......@@ -56,14 +60,23 @@ export default 'pages.json'`
${imports
.map((p) => {
const className = genClassName(p)
return `import { ${className}Class } from './${p}.uvue?type=page'`
return `import ${className}Class from './${p}.uvue?type=page'`
})
.join('\n')}
function definePageRoutes() {
${routes.map((route) => `__uniRoutes.push(${route})`).join('\n')}
}
function defineAppConfig(){
__uniConfig.entryPagePath = '/${imports[0]}'
}
`
}
},
}
}
function stringifyPageStyle(pageStyle: UniApp.PagesJsonPageStyle) {
delete pageStyle.isNVue
delete pageStyle.isSubNVue
return stringifyMap(pageStyle)
}
......@@ -29,3 +29,35 @@ export function genClassName(fileName: string) {
export function isVue(filename: string) {
return filename.endsWith('.vue') || filename.endsWith('.uvue')
}
export function stringifyMap(obj: unknown) {
return mapToInitString(objToMap(obj as Record<string, unknown>), true)
}
function mapToInitString(
map: Map<string, unknown>,
ts: boolean = false
): string {
let entries = []
for (let [key, value] of map) {
if (value instanceof Map) {
entries.push(`["${key}", ${mapToInitString(value, ts)}]`)
} else {
entries.push(`["${key}", ${JSON.stringify(value)}]`)
}
}
return `new Map${ts ? '<string, any>' : ''}([${entries.join(', ')}])`
}
function objToMap(obj: Record<string, unknown>) {
const map = new Map()
for (const key in obj) {
const value = obj[key]
if (typeof value === 'object') {
map.set(key, objToMap(value as Record<string, unknown>))
} else {
map.set(key, value)
}
}
return map
}
......@@ -2,18 +2,16 @@ import { SFCDescriptor } from '@vue/compiler-sfc'
export function genScript(
{ script }: SFCDescriptor,
{ filename }: { filename: string }
_options: { filename: string }
) {
if (!script) {
return `
export default {}
export const ${filename}Class = UTSAndroid.getKotlinClass(${filename})
`
}
return (
'\n'.repeat(script.loc.start.line - 1) +
`${script.content}
export const ${filename}Class = UTSAndroid.getKotlinClass(${filename})
`
)
}
......@@ -3237,19 +3237,11 @@ const Switch = /* @__PURE__ */ defineComponent({
watch(() => props2.checked, (val) => {
switchChecked.value = val;
});
const listeners = {
onChange(e2) {
switchChecked.value = e2.detail.value;
trigger("change", {
value: switchChecked.value
});
}
};
const _onClick = ($event, isLabelClick) => {
if (props2.disabled) {
return;
}
switchChecked.value = !switchChecked.value;
switchChecked.value = $event.detail ? $event.detail.value : !switchChecked.value;
trigger("change", {
value: switchChecked.value
});
......@@ -3273,7 +3265,9 @@ const Switch = /* @__PURE__ */ defineComponent({
"ref": rootRef
}, [type === SwitchType.switch ? createVNode("dc-switch", mergeProps({
dataUncType: "uni-switch"
}, listeners, {
}, {
"onChange": _onClick
}, {
checked: switchChecked.value,
color,
disabled
......@@ -3284,8 +3278,11 @@ const Switch = /* @__PURE__ */ defineComponent({
color
}
}, {
checked: switchChecked.value
}, listeners), null) : null]);
checked: switchChecked.value,
disabled
}, {
"onClick": _onClick
}), null) : null]);
};
}
});
......
......@@ -23,6 +23,7 @@ describe('compiler', () => {
inputDir,
outputDir,
sourceMap: false,
isPlugin: true,
components: {},
})
expect(existsSync(kotlinFile)).toBeTruthy()
......@@ -37,6 +38,7 @@ describe('compiler', () => {
inputDir,
outputDir,
sourceMap: false,
isPlugin: true,
components: {},
})
expect(existsSync(swiftFile)).toBeTruthy()
......
......@@ -95,8 +95,13 @@ function createResult(
}
}
interface CompilerOptions {
isPlugin: boolean
}
export async function compile(
pluginDir: string
pluginDir: string,
{ isPlugin }: CompilerOptions = { isPlugin: true }
): Promise<CompileResult | void> {
const pkg = resolvePackage(pluginDir)
if (!pkg) {
......@@ -158,7 +163,11 @@ export async function compile(
filename = resolvePlatformIndexFilename('app-android', pluginDir, pkg)
}
if (filename) {
await getCompiler('kotlin').runProd(filename, androidComponents)
await getCompiler('kotlin').runProd(
filename,
androidComponents,
isPlugin
)
if (cacheDir) {
// 存储 sourcemap
storeSourceMap(
......@@ -186,7 +195,7 @@ export async function compile(
filename = resolvePlatformIndexFilename('app-ios', pluginDir, pkg)
}
if (filename) {
await getCompiler('swift').runProd(filename, iosComponents)
await getCompiler('swift').runProd(filename, iosComponents, isPlugin)
if (cacheDir) {
storeSourceMap(
'app-ios',
......@@ -311,7 +320,11 @@ export async function compile(
inputDir,
outputDir
)
const res = await getCompiler(compilerType).runDev(filename, components)
const res = await getCompiler(compilerType).runDev(
filename,
components,
isPlugin
)
if (res) {
if (isArray(res.deps) && res.deps.length) {
// 添加其他文件的依赖
......
......@@ -62,7 +62,8 @@ function parseKotlinPackage(filename: string) {
export async function runKotlinProd(
filename: string,
components: Record<string, string>
components: Record<string, string>,
isPlugin = true
) {
// 文件有可能是 app-ios 里边的,因为编译到 android 时,为了保证不报错,可能会去读取 ios 下的 uts
if (filename.includes('app-ios')) {
......@@ -75,6 +76,7 @@ export async function runKotlinProd(
outputDir,
sourceMap: true,
components,
isPlugin,
})
if (!result) {
return
......@@ -99,7 +101,8 @@ export type RunKotlinDevResult = UTSResult & {
export async function runKotlinDev(
filename: string,
components: Record<string, string>
components: Record<string, string>,
isPlugin = true
): Promise<RunKotlinDevResult | undefined> {
// 文件有可能是 app-ios 里边的,因为编译到 android 时,为了保证不报错,可能会去读取 ios 下的 uts
if (filename.includes('app-ios')) {
......@@ -112,6 +115,7 @@ export async function runKotlinDev(
outputDir,
sourceMap: true,
components,
isPlugin,
})) as RunKotlinDevResult
if (!result) {
return
......@@ -305,7 +309,7 @@ const DEFAULT_IMPORTS = [
export async function compile(
filename: string,
{ inputDir, outputDir, sourceMap, components }: ToKotlinOptions
{ inputDir, outputDir, sourceMap, components, isPlugin }: ToKotlinOptions
) {
const { bundle, UTSTarget } = getUTSCompiler()
// let time = Date.now()
......@@ -340,7 +344,7 @@ export async function compile(
const result = await bundle(UTSTarget.KOTLIN, {
input,
output: {
isPlugin: true,
isPlugin,
outDir: outputDir,
package: pluginPackage,
sourceMap: sourceMap ? resolveUTSSourceMapPath() : false,
......
......@@ -35,7 +35,8 @@ function parseSwiftPackage(filename: string) {
export async function runSwiftProd(
filename: string,
components: Record<string, string>
components: Record<string, string>,
isPlugin = true
) {
// 文件有可能是 app-android 里边的,因为编译到 ios 时,为了保证不报错,可能会去读取 android 下的 uts
if (filename.includes('app-android')) {
......@@ -48,6 +49,7 @@ export async function runSwiftProd(
outputDir,
sourceMap: true,
components,
isPlugin,
})
if (!result) {
return
......@@ -75,7 +77,8 @@ export type RunSwiftDevResult = UTSResult & {
let isEnvReady = true
export async function runSwiftDev(
filename: string,
components: Record<string, string>
components: Record<string, string>,
isPlugin = true
) {
// 文件有可能是 app-android 里边的,因为编译到 ios 时,为了保证不报错,可能会去读取 android 下的 uts
if (filename.includes('app-android')) {
......@@ -106,6 +109,7 @@ export async function runSwiftDev(
outputDir,
sourceMap: true,
components,
isPlugin,
})) as RunSwiftDevResult
if (!result) {
......@@ -165,7 +169,7 @@ function isCliProject(projectPath: string) {
export async function compile(
filename: string,
{ inputDir, outputDir, sourceMap, components }: ToSwiftOptions
{ inputDir, outputDir, sourceMap, components, isPlugin }: ToSwiftOptions
) {
const { bundle, UTSTarget } = getUTSCompiler()
// let time = Date.now()
......@@ -194,7 +198,7 @@ export async function compile(
const result = await bundle(UTSTarget.SWIFT, {
input,
output: {
isPlugin: true,
isPlugin,
outDir: outputDir,
package: namespace,
sourceMap: sourceMap ? resolveUTSSourceMapPath() : false,
......
......@@ -18,6 +18,7 @@ interface ToOptions {
outputDir: string
sourceMap: boolean
components: Record<string, string>
isPlugin: boolean
}
export type ToKotlinOptions = ToOptions
export type ToSwiftOptions = ToOptions
......
......@@ -22,12 +22,7 @@ const DEFAULT_IMPORTS = [
'io.dcloud.uts.vue.*',
'io.dcloud.uts.vue.shared.*',
'io.dcloud.uts.vue.reactivity.*',
'io.dcloud.uniapp.appframe.*',
'io.dcloud.uniapp.interfaces.*',
'io.dcloud.uniapp.interfaces.htmlex.*',
'io.dcloud.uniapp.interfaces.htmlex.event.*',
'io.dcloud.uniapp.dom.*',
'io.dcloud.uniapp.dom.node.*',
'io.dcloud.uniapp.runtime.*',
]
export interface CompileAppOptions {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册