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

wip(uts): compiler

上级 faa3de6f
......@@ -10,6 +10,11 @@ module.exports = {
__GLOBAL__: false,
__VUE_OPTIONS_API__: true,
__VUE_PROD_DEVTOOLS__: false,
uni: {
requireNativePlugin(name) {
return {}
},
},
},
coverageDirectory: 'coverage',
coverageReporters: ['html', 'lcov', 'text'],
......
import { normalizeArg } from '../module'
describe('uts-module', () => {
test('normalize args', () => {
expect(normalizeArg(1)).toBe(1)
expect(normalizeArg('hello')).toBe('hello')
expect(normalizeArg(true)).toBe(true)
expect(normalizeArg({ callback: () => {} })).toEqual({
callback: { $$type: 'function', value: 1 },
})
expect(
normalizeArg({ success: () => {}, fail: () => {}, complete: () => {} })
).toEqual({
success: { $$type: 'function', value: 2 },
fail: { $$type: 'function', value: 3 },
complete: { $$type: 'function', value: 4 },
})
expect(
normalizeArg({
user: {
name: 'test',
age: 10,
callback() {},
},
success() {},
})
).toEqual({
user: {
name: 'test',
age: 10,
callback: {
$$type: 'function',
value: 5,
},
},
success: {
$$type: 'function',
value: 6,
},
})
})
})
[
{
"input": {
"module.ts": "lib/module.js"
},
"treeshake": false,
"compilerOptions": {
"module": "ESNext"
}
}
]
// @ts-expect-error
const proxy = uni.requireNativePlugin('proxy-module')
// @ts-expect-error
const moduleName = __MODULE_NAME__
// @ts-expect-error
const moduleDefine = __MODULE_DEFINE__
var module = initModule(moduleDefine)
let callbackId = 0
const objectToString = Object.prototype.toString
const toTypeString = (value) => objectToString.call(value)
const isPlainObject = (val) => toTypeString(val) === '[object Object]'
function normalizeArg(arg) {
if (typeof arg === 'function') {
return {
type: 'function',
value: callbackId++,
}
} else if (isPlainObject(arg)) {
Object.keys(arg).forEach((name) => {
arg[name] = normalizeArg(arg[name])
})
}
return arg
}
function moduleGetter(module, method) {
return (...args) => {
const params = args.map((arg) => normalizeArg(arg))
proxy.invoke({ module, method, params }, () => {})
}
}
function initModule(moduleDefine) {
const moduleProxy = {}
for (const methodName in moduleDefine) {
Object.defineProperty(moduleProxy, methodName, {
enumerable: true,
configurable: true,
get: () => moduleGetter(moduleName, methodName),
})
}
}
export { module as default, normalizeArg }
declare const uni: {
requireNativePlugin(name: string): { invoke: Function }
}
const moduleName = '__MODULE_NAME__'
const moduleDefine = '__MODULE_DEFINE__'
export default initModule(moduleDefine as unknown as Record<string, boolean>)
let callbackId = 1
const objectToString = Object.prototype.toString
const toTypeString = (value: unknown): string => objectToString.call(value)
const isPlainObject = (val: unknown): val is object =>
toTypeString(val) === '[object Object]'
export function normalizeArg(arg: unknown) {
if (typeof arg === 'function') {
return {
$$type: 'function',
value: callbackId++,
}
} else if (isPlainObject(arg)) {
Object.keys(arg).forEach((name) => {
;(arg as any)[name] = normalizeArg((arg as any)[name])
})
}
return arg
}
function moduleGetter(proxy: any, module: string, method: string) {
return (...args: unknown[]) => {
const params = args.map((arg) => normalizeArg(arg))
proxy.invoke({ module, method, params }, () => {})
}
}
function initModule(moduleDefine: Record<string, unknown>) {
const proxy = uni.requireNativePlugin('proxy-module')
const moduleProxy = {}
for (const methodName in moduleDefine) {
Object.defineProperty(moduleProxy, methodName, {
enumerable: true,
configurable: true,
get: () => moduleGetter(proxy, moduleName, methodName),
})
}
}
......@@ -25,8 +25,8 @@ export function uniUtsPlugin(): Plugin {
if (pkg.uni_modules?.type !== 'uts') {
return
}
// TODO 根据平台加载
return path.join(id, 'app-android/index.uts')
// 加载接口类
return path.join(id, pkg.main || 'interface.uts')
},
transform(code, id, opts) {
if (opts && opts.ssr) {
......
import { resolve } from 'path'
import type { UtsOptions, UtsResult } from './types'
import type { UtsOptions, UtsParseOptions, UtsResult } from './types'
import { normalizePath } from './utils'
const bindingsOverride = process.env['UTS_BINARY_PATH']
......@@ -36,6 +36,12 @@ function resolveOptions(options: UtsOptions) {
return options
}
export function parse(source: string, options: UtsParseOptions = {}) {
return bindings
.parse(source, toBuffer(options))
.then((res: string) => JSON.parse(res))
}
export function toKotlin(options: UtsOptions): Promise<UtsResult> {
const kotlinOptions = resolveOptions(options)
if (!kotlinOptions) {
......
......@@ -343,6 +343,8 @@ function buildFile(
})
}
export { parse } from './api'
export function runDev(target: UtsTarget, opts: ToOptions) {
opts = parseOptions('dev', target, opts)
!opts.silent && printStartup(target, 'development')
......
......@@ -90,6 +90,7 @@ function createConfig(entryFile, output, buildOption) {
declaration: shouldEmitDeclarations,
declarationMap: false,
skipLibCheck: true,
...(buildOption.compilerOptions || {}),
},
exclude: ['**/__tests__', 'test-dts'],
},
......
......@@ -166,7 +166,7 @@ async function build(target) {
],
{ stdio: 'inherit' }
)
if (types) {
if (types && target !== 'uni-uts-vite') {
await extract(target)
}
}
......
const fs = require('fs')
const path = require('path')
const { runBuild, UtsTarget } = require('../packages/uts/dist')
const { parse, runBuild, UtsTarget } = require('../packages/uts/dist')
const projectDir = path.resolve(__dirname, '../packages/playground/uts')
const start = Date.now()
parse(
fs.readFileSync(
path.resolve(projectDir, 'uni_modules/test-uniplugin/interface.uts'),
'utf8'
)
).then((res) => {
console.log('parse: ' + (Date.now() - start) + 'ms')
console.log(JSON.stringify(res))
})
// uts
runBuild(UtsTarget.KOTLIN, {
silent: false,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册