提交 1aa3a930 编写于 作者: fxy060608's avatar fxy060608 提交者: qiang

wip(uvue): 完善 uni ext api 的 provider 扩展编译

上级 47ea8ebb
......@@ -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'),
{
......
......@@ -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,
......
......@@ -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<string>()
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<string, any>) {
const provider = pkg.uni_modules?.['uni-ext-api']?.provider as
| { name?: string; service?: string }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册