提交 bac44aca 编写于 作者: 雪洛's avatar 雪洛

feat(harmony): 完善鸿蒙用户自行编写的provider支持

上级 0b7fa4a9
......@@ -4,8 +4,10 @@ import {
type UniVitePlugin,
buildUniExtApis,
camelize,
formatExtApiProviderName,
getCurrentCompiledUTSPlugins,
getUniExtApiProviderRegisters,
parseManifestJsonOnce,
parseUniExtApi,
resolveUTSCompiler,
} from '@dcloudio/uni-cli-shared'
......@@ -106,6 +108,53 @@ export function uniAppHarmonyPlugin(): UniVitePlugin {
}
}
interface IRelatedProvider {
service: string
name: string
}
const ProviderServiceMap = {
oauth: {},
payment: {
// alipay: 'alipay',
weixin: 'wxpay',
},
}
/**
* 获取manifest.json中勾选的provider
*/
function getRelatedProviders(inputDir: string): IRelatedProvider[] {
const manifest = parseManifestJsonOnce(inputDir)
const providers: IRelatedProvider[] = []
const sdkConfigs = manifest?.['app-plus']?.distribute?.sdkConfigs
if (!sdkConfigs) {
return providers
}
for (const service in sdkConfigs) {
if (Object.prototype.hasOwnProperty.call(sdkConfigs, service)) {
const ProviderNameMap = ProviderServiceMap[service]
const relatedProviders = sdkConfigs[service]
for (const name in relatedProviders) {
if (Object.prototype.hasOwnProperty.call(relatedProviders, name)) {
const providerName = ProviderNameMap[name]
providers.push({
service,
name: providerName || name,
})
}
}
}
}
return providers
}
const builtInProviders = [
{
service: 'payment',
name: 'alipay',
},
]
function genAppHarmonyIndex(inputDir: string, utsPlugins: Set<string>) {
if (!process.env.UNI_APP_HARMONY_PROJECT_PATH) {
return
......@@ -144,14 +193,40 @@ function genAppHarmonyIndex(inputDir: string, utsPlugins: Set<string>) {
}
})
const relatedProviders = getRelatedProviders(inputDir)
const importProviderCodes: string[] = []
const registerProviderCodes: string[] = []
const providers = getUniExtApiProviderRegisters()
providers.forEach((provider) => {
const parts = provider.class.split('.')
const className = parts[parts.length - 1]
const allProviders = providers
.map((provider) => {
return {
service: provider.service,
name: provider.name,
moduleSpecifier: `./${provider.plugin}/utssdk/app-harmony`,
}
})
.concat(
builtInProviders.map((provider) => {
return {
service: provider.service,
name: provider.name,
moduleSpecifier: `@dcloudio/uni-app-harmony/providers/uni-${provider.service}-${provider.name}`,
}
})
)
relatedProviders.forEach((relatedProvider) => {
const provider = allProviders.find(
(item) =>
item.service === relatedProvider.service &&
item.name === relatedProvider.name
)
if (!provider) {
return
}
const className = formatExtApiProviderName(provider.service, provider.name)
importProviderCodes.push(
`import { ${className} } from './${provider.plugin}/utssdk/app-harmony'`
`import { ${className} } from '${provider.moduleSpecifier}'`
)
registerProviderCodes.push(
`registerUniProvider('${provider.service}', '${provider.name}', new ${className}())`
......@@ -159,7 +234,7 @@ function genAppHarmonyIndex(inputDir: string, utsPlugins: Set<string>) {
})
if (importProviderCodes.length) {
importProviderCodes.unshift(
`import { registerUniProvider } from '../uni-app/lib/uni-api-shared'`
`import { registerUniProvider } from '@dcloudio/uni-app-harmony'`
)
importCodes.push(...importProviderCodes)
extApiCodes.push(...registerProviderCodes)
......
......@@ -89,6 +89,11 @@ export function uniUTSAppUniModulesPlugin(
return compiler.compileArkTS(pluginDir, {
isX: !!options.x,
isExtApi,
transform: {
uniExtApiProviderName: extApiProvider?.name,
uniExtApiProviderService: extApiProvider?.service,
uniExtApiProviderServicePlugin,
},
})
}
......
import path from 'path'
import fs from 'fs-extra'
import type { UTSBundleOptions } from '@dcloudio/uts'
import { getUTSCompiler } from './utils'
import { formatUniProviderName, getUTSCompiler } from './utils'
import type { CompileResult } from '.'
interface ArkTSCompilerOptions {
isX?: boolean
isExtApi?: boolean
transform?: {
uniExtApiProviderName?: string
uniExtApiProviderService?: string
uniExtApiProviderServicePlugin?: string
}
}
export function getArkTSAutoImports(): Record<
......@@ -40,7 +45,7 @@ export function getArkTSAutoImports(): Record<
}
export async function compileArkTS(
pluginDir: string,
{ isExtApi }: ArkTSCompilerOptions
{ isExtApi, transform }: ArkTSCompilerOptions
): Promise<CompileResult | void> {
if (!process.env.UNI_APP_HARMONY_PROJECT_PATH) {
console.error('manifest.json -> app-harmony -> projectPath is required')
......@@ -60,6 +65,13 @@ export async function compileArkTS(
pluginId
)
const autoImportExternals = getArkTSAutoImports()
if (transform && transform.uniExtApiProviderService) {
autoImportExternals['@dcloudio/uni-app-harmony'].push([
formatUniProviderName(transform.uniExtApiProviderService),
])
}
const buildOptions: UTSBundleOptions = {
hbxVersion: process.env.HX_Version || process.env.UNI_COMPILER_VERSION,
input: {
......@@ -85,7 +97,7 @@ export async function compileArkTS(
logFilename: false,
isPlugin: true,
transform: {
autoImportExternals: getArkTSAutoImports(),
autoImportExternals,
},
treeshake: {
noSideEffects: true,
......
......@@ -822,11 +822,15 @@ export function resolveConfigProvider(
}
}
function formatExtApiProviderName(service: string, name: string) {
export function formatUniProviderName(service: string) {
if (service === 'oauth') {
service = 'OAuth'
}
return `Uni${capitalize(camelize(service))}${capitalize(
return `Uni${capitalize(camelize(service))}Provider`
}
function formatExtApiProviderName(service: string, name: string) {
return `${formatUniProviderName(service)}${capitalize(
camelize(name)
)}ProviderImpl`
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册