From 1aa3a9307bcc7d9f65dfd6d28636e237db831d8a Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Fri, 23 Feb 2024 21:00:23 +0800 Subject: [PATCH] =?UTF-8?q?wip(uvue):=20=E5=AE=8C=E5=96=84=20uni=20ext=20a?= =?UTF-8?q?pi=20=E7=9A=84=20provider=20=E6=89=A9=E5=B1=95=E7=BC=96?= =?UTF-8?q?=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-app-uts/src/plugins/android/plugin.ts | 4 ++ packages/uni-cli-shared/src/uni_modules.ts | 12 ++++ .../src/vite/plugins/uts/uni_modules.ts | 65 ++++++++++++++----- 3 files changed, 64 insertions(+), 17 deletions(-) diff --git a/packages/uni-app-uts/src/plugins/android/plugin.ts b/packages/uni-app-uts/src/plugins/android/plugin.ts index 8889ee20fa..bf1c3936af 100644 --- a/packages/uni-app-uts/src/plugins/android/plugin.ts +++ b/packages/uni-app-uts/src/plugins/android/plugin.ts @@ -12,6 +12,7 @@ import { resolveMainPathOnce, resolveUTSCompiler, utsPlugins, + buildUniExtApiProviders, } from '@dcloudio/uni-cli-shared' import { DEFAULT_APPID, @@ -142,6 +143,9 @@ export function uniAppPlugin(): UniVitePlugin { pageCount = parseInt(process.env.UNI_APP_X_PAGE_COUNT) || 0 } } + + await buildUniExtApiProviders() + const res = await resolveUTSCompiler().compileApp( path.join(tempOutputDir, 'main.uts'), { diff --git a/packages/uni-cli-shared/src/uni_modules.ts b/packages/uni-cli-shared/src/uni_modules.ts index 92fe436542..1887df61bf 100644 --- a/packages/uni-cli-shared/src/uni_modules.ts +++ b/packages/uni-cli-shared/src/uni_modules.ts @@ -28,6 +28,12 @@ export interface Exports { [name: string]: Define | Defines | false } +const extApiProviders: { plugin: string; service: string; name?: string }[] = [] + +export function getUniExtApiProviders() { + return extApiProviders +} + export function parseUniExtApis( vite = true, platform: typeof process.env.UNI_UTS_PLATFORM, @@ -42,6 +48,7 @@ export function parseUniExtApis( } const injects: Injects = {} + extApiProviders.length = 0 fs.readdirSync(uniModulesDir).forEach((uniModuleDir) => { // 必须以 uni- 开头 if (!uniModuleDir.startsWith('uni-')) { @@ -59,6 +66,11 @@ export function parseUniExtApis( exports = pkg.uni_modules['uni-ext-api'] } if (exports) { + const provider = exports.provider as any + if (provider && provider.service) { + provider.plugin = uniModuleDir + extApiProviders.push(provider) + } const curInjects = parseInjects( vite, platform, diff --git a/packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts b/packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts index 35faccaf84..3fb58a01c7 100644 --- a/packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts +++ b/packages/uni-cli-shared/src/vite/plugins/uts/uni_modules.ts @@ -5,6 +5,7 @@ import { once } from '@dcloudio/uni-shared' import { resolveUTSAppModule, resolveUTSCompiler } from '../../../uts' import { parseVueRequest } from '../../utils' +import { getUniExtApiProviders } from '../../../uni_modules' const UTSProxyRE = /\?uts-proxy$/ function isUTSProxy(id: string) { @@ -29,10 +30,51 @@ interface UniUTSPluginOptions { export const utsPlugins = new Set() +let uniExtApiCompiler = async () => {} + export function uniUTSUniModulesPlugin( options: UniUTSPluginOptions = {} ): Plugin { process.env.UNI_UTS_USING_ROLLUP = 'true' + + const compilePlugin = (pluginDir: string) => { + utsPlugins.add(path.basename(pluginDir)) + + const pkgJson = require(path.join(pluginDir, 'package.json')) + + const extApiProvider = resolveExtApiProvider(pkgJson) + + return resolveUTSCompiler().compile(pluginDir, { + isX: !!options.x, + isSingleThread: !!options.isSingleThread, + isPlugin: true, + extApis: options.extApis, + sourceMap: process.env.NODE_ENV === 'development', + transform: { + uniExtApiProviderName: extApiProvider?.name, + uniExtApiProviderService: extApiProvider?.service, + }, + }) + } + + uniExtApiCompiler = async () => { + // 获取 provider 扩展 + const plugins = getUniExtApiProviders() + .filter((provider) => !utsPlugins.has(provider.plugin)) + .map((provider) => provider.plugin) + for (const plugin of plugins) { + const result = await compilePlugin( + path.resolve(process.env.UNI_INPUT_DIR, 'uni_modules', plugin) + ) + if (result) { + // 时机不对,不能addWatch + // result.deps.forEach((dep) => { + // this.addWatchFile(dep) + // }) + } + } + } + return { name: 'uni:uts-uni_modules', apply: 'build', @@ -85,23 +127,7 @@ export function uniUTSUniModulesPlugin( }) } const compile = once(() => { - utsPlugins.add(path.basename(pluginDir)) - - const pkgJson = require(path.join(pluginDir, 'package.json')) - - const extApiProvider = resolveExtApiProvider(pkgJson) - - return resolveUTSCompiler().compile(pluginDir, { - isX: !!options.x, - isSingleThread: !!options.isSingleThread, - isPlugin: true, - extApis: options.extApis, - sourceMap: process.env.NODE_ENV === 'development', - transform: { - uniExtApiProviderName: extApiProvider?.name, - uniExtApiProviderService: extApiProvider?.service, - }, - }) + return compilePlugin(pluginDir) }) utsModuleCaches.set(pluginDir, compile) const result = await compile() @@ -117,9 +143,14 @@ export function uniUTSUniModulesPlugin( } } }, + async generateBundle() {}, } } +export async function buildUniExtApiProviders() { + await uniExtApiCompiler() +} + export function resolveExtApiProvider(pkg: Record) { const provider = pkg.uni_modules?.['uni-ext-api']?.provider as | { name?: string; service?: string } -- GitLab