From 0a12503f571dc8a2a007600db4721c1b58510f0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E4=BA=9A=E7=90=AA?= Date: Mon, 3 Jun 2024 21:04:17 +0800 Subject: [PATCH] feat(harmony): build ext api with ets format --- packages/uni-app-harmony/build.ets.json | 39 ++++----- .../uni-app-harmony/src/service/api/index.ts | 14 ++-- packages/uni-app-harmony/vite.config.ts | 16 ++-- packages/uni-cli-shared/src/uni_modules.ts | 10 ++- packages/uni-runtime/src/helpers/api/index.ts | 81 +++++++++++++++++++ packages/uni-runtime/src/index.ts | 12 ++- 6 files changed, 131 insertions(+), 41 deletions(-) create mode 100644 packages/uni-runtime/src/helpers/api/index.ts diff --git a/packages/uni-app-harmony/build.ets.json b/packages/uni-app-harmony/build.ets.json index eff53a5685..7fd4ae1864 100644 --- a/packages/uni-app-harmony/build.ets.json +++ b/packages/uni-app-harmony/build.ets.json @@ -22,32 +22,25 @@ "__APP_VIEW__": "false", "__NODE_JS__": "false" }, - "banner": "import { IUniError, UTSObject, UTSJSONObject, string } from './uts'\nimport { defineAsyncApi } from './uni.api'", + "banner": "import { IUniError, UTSObject, string } from './uts'\nimport { UniError, UTSJSONObject } from './uni-app-harmony-framework-dev.js'\nimport { defineAsyncApi, ApiExcutor, ProtocolOptions, ApiOptions, ErrRes } from './uni-api-shared'\n", "wrapper": { "name": "initUniExtApi", "args": [["APP_ID", "string"]] } + }, { + "input": { + "../uni-runtime/src/helpers/api/index.ts": "uni-api-shared.ets" + }, + "alias": { + "@dcloudio/uni-runtime": "./uni-app-harmony-framework-dev.js" + }, + "externals": ["./uni-app-harmony-framework-dev.js"], + "replacements": { + "__PLATFORM__": "'app-harmony'", + "__DEV__": "false", + "__X__": "true", + "__APP_VIEW__": "false", + "__NODE_JS__": "false" + } } - // { - // "input": { - // "src/service/index.ts": "uni.runtime.ts" - // }, - // "alias": { - // "@vue/shared": "/packages/uni-app-harmony/src/shared", - // "@dcloudio/uni-api": "/packages/uni-api/src", - // "@dcloudio/uni-app-plus": "/packages/uni-app-plus/src", - // "@dcloudio/uni-components": "/packages/uni-components/src", - // "@dcloudio/uni-core": "/packages/uni-core/src", - // "@dcloudio/uni-i18n": "/packages/uni-i18n/src", - // "@dcloudio/uni-platform": "/packages/uni-app-harmony/src/platform", - // "@dcloudio/uni-shared": "/packages/uni-shared/src" - // }, - // "replacements": { - // "__PLATFORM__": "'app-harmony'", - // "__DEV__": "false", - // "__X__": "true", - // "__APP_VIEW__": "false", - // "__NODE_JS__": "false" - // } - // } ] diff --git a/packages/uni-app-harmony/src/service/api/index.ts b/packages/uni-app-harmony/src/service/api/index.ts index 0f293abd6e..913a183963 100644 --- a/packages/uni-app-harmony/src/service/api/index.ts +++ b/packages/uni-app-harmony/src/service/api/index.ts @@ -1,10 +1,10 @@ -export * from './media/chooseImage' -export * from './media/chooseVideo' -export * from './media/getImageInfo' -export * from './media/getVideoInfo' -export * from './network/request' -export * from './network/uploadFile' -export * from './network/downloadFile' +// export * from './media/chooseImage' +// export * from './media/chooseVideo' +// export * from './media/getImageInfo' +// export * from './media/getVideoInfo' +// export * from './network/request' +// export * from './network/uploadFile' +// export * from './network/downloadFile' export * from './ui/index' export * from './device/index' export { navigateTo } from './route/navigateTo' diff --git a/packages/uni-app-harmony/vite.config.ts b/packages/uni-app-harmony/vite.config.ts index 66fc86b6a1..a6fac8d7d2 100644 --- a/packages/uni-app-harmony/vite.config.ts +++ b/packages/uni-app-harmony/vite.config.ts @@ -178,7 +178,7 @@ export default defineConfig({ function parseExtApiInjects(uniModulesDir: string) { return parseInjects( true, - 'app', + 'app-harmony', 'arkts', // javascript|kotlin|swift (不传入) '', uniModulesDir, @@ -214,20 +214,26 @@ function initArkTSExtApi() { if (Object.keys(injects).length === 0) { continue } - const specifiers: string[] = [] + const apiSpecifiers: string[] = [] + const apiTypeSpecifiers: string[] = [] Object.keys(injects).forEach((key) => { const api = injects[key][1] const apiType = capitalize(api) - specifiers.push(api) - specifiers.push(apiType) + apiSpecifiers.push(api) + apiTypeSpecifiers.push(apiType) defineExtApis.push(api) uniExtApis.push(`${api}: ${apiType}`) }) importExtApis.push( - `import { ${specifiers.join( + `import { ${apiSpecifiers.join( ', ' )} } from './${extApi}/utssdk/app-harmony/index.uts'` ) + importExtApis.push( + `import { ${apiTypeSpecifiers.join( + ', ' + )} } from './${extApi}/utssdk/interface.uts'` + ) exportExtApis.push( `export * from './${extApi}/utssdk/app-harmony/index.uts'` ) diff --git a/packages/uni-cli-shared/src/uni_modules.ts b/packages/uni-cli-shared/src/uni_modules.ts index fd8cdc8eb2..3d44d00546 100644 --- a/packages/uni-cli-shared/src/uni_modules.ts +++ b/packages/uni-cli-shared/src/uni_modules.ts @@ -189,14 +189,16 @@ export function parseInjects( fs.existsSync( path.resolve(uniModuleRootDir, 'utssdk', 'app-android') ) || - fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-ios')) + fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-ios')) || + fs.existsSync(path.resolve(uniModuleRootDir, 'utssdk', 'app-harmony')) } } // 其他平台修改source,直接指向目标文件,否则 uts2js 找不到类型信息 if ( platform !== 'app' && platform !== 'app-android' && - platform !== 'app-ios' + platform !== 'app-ios' && + platform !== 'app-harmony' ) { if (fs.existsSync(platformIndexFileName)) { source = `${source}/utssdk/${platform}/index.uts` @@ -264,7 +266,9 @@ function parseInject( } else { const defineOptions = define[d] as DefineOptions const p = - platform === 'app-android' || platform === 'app-ios' + platform === 'app-android' || + platform === 'app-ios' || + platform === 'app-harmony' ? 'app' : platform if (!(p in defineOptions)) { diff --git a/packages/uni-runtime/src/helpers/api/index.ts b/packages/uni-runtime/src/helpers/api/index.ts new file mode 100644 index 0000000000..11058213dd --- /dev/null +++ b/packages/uni-runtime/src/helpers/api/index.ts @@ -0,0 +1,81 @@ +import { + defineAsyncApi as originalDefineAsyncApi, + // defineOffApi as originalDefineOffApi, + // defineOnApi as originalDefineOnApi, + // defineSyncApi as originalDefineSyncApi, + // defineTaskApi as originalDefineTaskApi, +} from '@dcloudio/uni-runtime' + +type Anything = Object | null | undefined +type NullType = null | undefined + +export interface ErrRes { + errMsg?: string | null + errCode?: number | null +} +export interface ApiExcutor { + resolve: (res: K | void) => void + reject: (errMsg?: string, errRes?: ErrRes) => void +} +export interface ProtocolOptions { + name?: string | null + type?: string | null + required?: boolean | null + validator?: (value: Object) => boolean | undefined | string +} +export interface ApiOptions { + beforeInvoke?: (args: Object) => boolean | void | string + beforeAll?: (res: Object) => void + beforeSuccess?: (res: Object, args: T) => void + formatArgs?: Map +} +interface AsyncMethodOptionLike { + success?: Function | null +} + +const TYPE_MAP = new Map([ + ['string', String], + ['number', Number], + ['boolean', Boolean], + ['array', Array], + ['object', Object], +]) +function getPropType(type: string | NullType): Anything { + if (!type) { + return + } + return TYPE_MAP[type] +} +export function defineAsyncApi( + name: string, + fn: (options: T, res: ApiExcutor) => void, + protocol: Map, + options: ApiOptions +): Function { + const originalProtocol = {} as Record + protocol.forEach((value, key) => { + const protocol = (originalProtocol[key] = {} as Record) + protocol.name = value.name + protocol.type = getPropType(value.type) + protocol.required = value.required + protocol.validator = value.validator + }) + const originalFormatArgs = {} as Record + if (options.formatArgs) { + options.formatArgs.forEach((value, key) => { + originalFormatArgs[key] = value + }) + } + const originalOptions = {} as Record + originalOptions.beforeInvoke = options.beforeInvoke + originalOptions.beforeAll = options.beforeAll + originalOptions.beforeSuccess = options.beforeSuccess + originalOptions.formatArgs = originalFormatArgs + return originalDefineAsyncApi<(options: T) => Promise>( + name, + // @ts-expect-error + fn, + originalProtocol, + originalOptions + ) +} diff --git a/packages/uni-runtime/src/index.ts b/packages/uni-runtime/src/index.ts index 315bb3bd0a..c02ada9c09 100644 --- a/packages/uni-runtime/src/index.ts +++ b/packages/uni-runtime/src/index.ts @@ -6,7 +6,13 @@ export { extend, isArray, } from '@vue/shared' -// export * from '@dcloudio/uni-api' -// export * from '@dcloudio/uni-shared' -// export * from '@dcloudio/uni-platform' + +// export * from './helpers/api' +export { defineAsyncApi } from '@dcloudio/uni-api' +export { Emitter } from '@dcloudio/uni-shared' +export { getRealPath } from '@dcloudio/uni-platform' export const __uniConfig = globalThis.__uniConfig +// @ts-expect-error TODO 处理类型冲突 +export const UniError = globalThis.UniError +// @ts-expect-error TODO 处理类型冲突 +export const UTSJSONObject = globalThis.UTSJSONObject -- GitLab