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

wip(uvue): 完善 ext api provider

上级 3718df1e
......@@ -28,7 +28,12 @@ export interface Exports {
[name: string]: Define | Defines | false
}
const extApiProviders: { plugin: string; service: string; name?: string }[] = []
const extApiProviders: {
plugin: string
service: string
name?: string
servicePlugin?: string
}[] = []
export function getUniExtApiProviders() {
return extApiProviders
......
import type { Plugin } from 'vite'
import fs from 'fs'
import path from 'path'
import { once } from '@dcloudio/uni-shared'
......@@ -43,7 +44,21 @@ export function uniUTSUniModulesPlugin(
const pkgJson = require(path.join(pluginDir, 'package.json'))
const extApiProvider = resolveExtApiProvider(pkgJson)
// 如果是 provider 扩展,需要判断 provider 的宿主插件是否在本地,在的话,自动导入该宿主插件包名
let uniExtApiProviderServicePlugin = ''
if (extApiProvider?.servicePlugin) {
if (
fs.existsSync(
path.resolve(
process.env.UNI_INPUT_DIR,
'uni_modules',
extApiProvider.servicePlugin
)
)
) {
uniExtApiProviderServicePlugin = extApiProvider.servicePlugin
}
}
return resolveUTSCompiler().compile(pluginDir, {
isX: !!options.x,
isSingleThread: !!options.isSingleThread,
......@@ -53,18 +68,19 @@ export function uniUTSUniModulesPlugin(
transform: {
uniExtApiProviderName: extApiProvider?.name,
uniExtApiProviderService: extApiProvider?.service,
uniExtApiProviderServicePlugin,
},
})
}
uniExtApiCompiler = async () => {
// 获取 provider 扩展
const plugins = getUniExtApiProviders()
.filter((provider) => !utsPlugins.has(provider.plugin))
.map((provider) => provider.plugin)
const plugins = getUniExtApiProviders().filter(
(provider) => !utsPlugins.has(provider.plugin)
)
for (const plugin of plugins) {
const result = await compilePlugin(
path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', plugin)
path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', plugin.plugin)
)
if (result) {
// 时机不对,不能addWatch
......@@ -153,12 +169,17 @@ export async function buildUniExtApiProviders() {
export function resolveExtApiProvider(pkg: Record<string, any>) {
const provider = pkg.uni_modules?.['uni-ext-api']?.provider as
| { name?: string; service?: string }
| {
name?: string
plugin?: string
service: string
servicePlugin: string
}
| undefined
if (provider?.service) {
return {
name: provider.name,
service: provider.service,
if (provider.name && !provider.servicePlugin) {
provider.servicePlugin = 'uni-' + provider.service
}
return provider
}
}
......@@ -866,6 +866,7 @@ class UniElement extends HTMLElement {
constructor() {
super();
this._props = {};
this.__isUniElement = true;
}
attachVmProps(props2) {
this._props = props2;
......
......@@ -2134,6 +2134,7 @@ class UniElement extends HTMLElement {
constructor() {
super();
this._props = {};
this.__isUniElement = true;
}
attachVmProps(props2) {
this._props = props2;
......
......@@ -26,6 +26,7 @@ import {
ERR_MSG_PLACEHOLDER,
genConfigJson,
resolveAndroidComponents,
resolveConfigProvider,
resolveIOSComponents,
resolvePackage,
} from './utils'
......@@ -190,6 +191,7 @@ export async function compile(
}
if (filename) {
await getCompiler('kotlin').runProd(filename, androidComponents, {
pluginId: pkg.id,
isX,
isSingleThread,
isPlugin,
......@@ -226,6 +228,7 @@ export async function compile(
}
if (filename) {
await getCompiler('swift').runProd(filename, iosComponents, {
pluginId: pkg.id,
isX,
isSingleThread,
isPlugin: true, // iOS 目前仅有 plugin 模式
......@@ -322,7 +325,8 @@ export async function compile(
pluginRelativeDir,
pkg.is_uni_modules,
inputDir,
outputDir
outputDir,
resolveConfigProvider(utsPlatform, pkg.id, transform)
)
console.log(cacheTips(pkg.id))
......@@ -369,7 +373,8 @@ export async function compile(
pluginRelativeDir,
pkg.is_uni_modules,
inputDir,
outputDir
outputDir,
resolveConfigProvider(utsPlatform, pkg.id, transform)
)
const res = await getCompiler(compilerType).runDev(filename, {
components,
......
......@@ -30,6 +30,7 @@ import {
isUniCloudSupported,
parseExtApiDefaultParameters,
parseInjectModules,
resolveConfigProvider,
} from './utils'
import { Module } from '../types/types'
import { parseUTSKotlinStacktrace, parseUTSSyntaxError } from './stacktrace'
......@@ -93,6 +94,7 @@ export async function runKotlinProd(
filename: string,
components: Record<string, string>,
{
pluginId,
isPlugin,
isX,
isSingleThread,
......@@ -101,6 +103,7 @@ export async function runKotlinProd(
transform,
sourceMap,
}: {
pluginId: string
isPlugin: boolean
isX: boolean
isSingleThread: boolean
......@@ -152,6 +155,7 @@ export async function runKotlinProd(
package: parseKotlinPackage(filename).package + '.',
hookClass,
result,
provider: resolveConfigProvider('app-android', pluginId, transform),
})
return result
......@@ -300,6 +304,16 @@ export async function runKotlinDev(
path.resolve(path.dirname(kotlinFile), chunk)
) || []
)
const uniModuleDeps: string[] = []
if (transform?.uniExtApiProviderServicePlugin) {
uniModuleDeps.push(
...getUniModulesCacheJarsByPlugin(
cacheDir,
transform.uniExtApiProviderServicePlugin
)
)
}
const options = {
pageCount: 0,
kotlinc: resolveKotlincArgs(
......@@ -310,6 +324,7 @@ export async function runKotlinDev(
.concat(resolveLibs(filename))
.concat(deps)
.concat(resDeps)
.concat(uniModuleDeps)
// .concat(getUniModulesCacheJars(cacheDir))
// .concat(getUniModulesJars(outputDir))
),
......@@ -504,6 +519,15 @@ export async function compile(
if (isUniCloudSupported() || process.env.NODE_ENV !== 'production') {
imports.push('io.dcloud.unicloud.*')
}
// 本地 provider
if (transform?.uniExtApiProviderServicePlugin) {
imports.push(
parseKotlinPackageWithPluginId(
transform.uniExtApiProviderServicePlugin,
true
) + '.*'
)
}
const componentsCode = genComponentsCode(filename, components, isX)
const { package: pluginPackage, id: pluginId } = parseKotlinPackage(filename)
const input: UTSInputOptions = {
......@@ -692,6 +716,16 @@ export function getUniModulesEncryptCacheJars(cacheDir: string) {
return []
}
function getUniModulesCacheJarsByPlugin(cacheDir: string, plugin: string) {
if (cacheDir) {
return sync('app-android/uts/uni_modules/' + plugin + '/index.jar', {
cwd: cacheDir,
absolute: true,
})
}
return []
}
export function getUniModulesCacheJars(cacheDir: string) {
if (cacheDir) {
return sync('app-android/uts/uni_modules/*/index.jar', {
......
......@@ -8,6 +8,7 @@ import {
isColorSupported,
moveRootIndexSourceMap,
parseSwiftPackageWithPluginId,
resolveConfigProvider,
resolveIOSDir,
resolvePackage,
resolveUTSPlatformFile,
......@@ -42,6 +43,7 @@ export async function runSwiftProd(
filename: string,
components: Record<string, string>,
{
pluginId,
isPlugin,
isX,
isSingleThread,
......@@ -50,6 +52,7 @@ export async function runSwiftProd(
sourceMap,
hookClass,
}: {
pluginId: string
isPlugin: boolean
isX: boolean
isSingleThread: boolean
......@@ -92,6 +95,7 @@ export async function runSwiftProd(
package: parseSwiftPackage(filename).namespace,
hookClass,
result,
provider: resolveConfigProvider('app-ios', pluginId, transform),
})
}
......
......@@ -86,6 +86,7 @@ export interface UTSPlatformResourceOptions {
package: string
hookClass: string
result: UTSResult
provider?: { name: string; service: string; class: string }
}
export function genUTSPlatformResource(
filename: string,
......@@ -115,7 +116,8 @@ export function genUTSPlatformResource(
utsOutputDir,
options.hookClass,
options.components,
options.package
options.package,
options.provider
)
// 生产模式下,需要将生成的平台文件转移到 src 下
......@@ -363,9 +365,10 @@ export function genConfigJson(
pluginRelativeDir: string,
is_uni_modules: boolean,
inputDir: string,
outputDir: string
outputDir: string,
provider?: { name: string; service: string; class: string }
) {
if (!Object.keys(components).length && !hookClass) {
if (!Object.keys(components).length && !hookClass && !provider) {
return
}
const pluginId = basename(pluginRelativeDir)
......@@ -390,7 +393,8 @@ export function genConfigJson(
components,
platform === 'app-android'
? parseKotlinPackageWithPluginId(pluginId, is_uni_modules) + '.'
: parseSwiftPackageWithPluginId(pluginId, is_uni_modules)
: parseSwiftPackageWithPluginId(pluginId, is_uni_modules),
provider
)
}
......@@ -401,13 +405,15 @@ function copyConfigJson(
outputDir: string,
hookClass: string,
componentsObj: Record<string, string>,
namespace: string
namespace: string,
provider?: { name: string; service: string; class: string }
) {
const configJsonFilename = resolve(inputDir, 'config.json')
const outputConfigJsonFilename = resolve(outputDir, 'config.json')
const hasComponents = !!Object.keys(componentsObj).length
const hasHookClass = !!hookClass
if (hasComponents || hasHookClass) {
const hasProvider = !!provider
if (hasComponents || hasHookClass || hasProvider) {
const configJson: Record<string, any> = fs.existsSync(configJsonFilename)
? parseJson(fs.readFileSync(configJsonFilename, 'utf8'))
: {}
......@@ -423,6 +429,9 @@ function copyConfigJson(
if (hasHookClass) {
configJson.hooksClass = hookClass
}
if (hasProvider) {
configJson.provider = provider
}
fs.outputFileSync(
outputConfigJsonFilename,
JSON.stringify(configJson, null, 2)
......@@ -638,3 +647,31 @@ export function normalizeExtApiModules(json: Record<string, any>) {
})
return res
}
export function resolveConfigProvider(
platform: 'app-android' | 'app-ios',
plugin: string,
transform: UTSOutputOptions['transform']
) {
if (transform?.uniExtApiProviderName && transform?.uniExtApiProviderService) {
return {
name: transform.uniExtApiProviderName,
service: transform.uniExtApiProviderService,
class:
(platform === 'app-android'
? parseKotlinPackageWithPluginId(plugin, true)
: parseSwiftPackageWithPluginId(plugin, true)) +
'.' +
formatExtApiProviderName(
transform.uniExtApiProviderService,
transform.uniExtApiProviderName
),
}
}
}
function formatExtApiProviderName(service: string, name: string) {
return `UniExtApi${capitalize(camelize(service))}${capitalize(
camelize(name)
)}Provider`
}
......@@ -60,6 +60,7 @@ export type UTSOutputOptions = {
uniExtApiDefaultParameters?: Record<string, string[]>
uniExtApiProviderName?: string
uniExtApiProviderService?: string
uniExtApiProviderServicePlugin?: string
uvueClassNamePrefix?: string
uvueClassNameOnlyBasename?: boolean
disableReactiveObject?: boolean
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册