mirror of
https://github.com/certd/certd.git
synced 2026-07-07 13:07:36 +08:00
feat: 通过插件配置懒加载依赖,动态加载第三方依赖包,精简安装镜像大小
This commit is contained in:
@@ -3,6 +3,7 @@ import { FormItemProps } from "../dt/index.js";
|
||||
import { HttpClient, ILogger, utils } from "@certd/basic";
|
||||
import * as _ from "lodash-es";
|
||||
import { PluginRequestHandleReq } from "../plugin/index.js";
|
||||
import { IServiceGetter } from "../service/index.js";
|
||||
|
||||
// export type AccessRequestHandleReqInput<T = any> = {
|
||||
// id?: number;
|
||||
@@ -20,6 +21,8 @@ export type AccessInputDefine = FormItemProps & {
|
||||
export type AccessDefine = Registrable & {
|
||||
icon?: string;
|
||||
subtype?: string;
|
||||
dependPlugins?: Record<string, string>;
|
||||
dependPackages?: Record<string, string>;
|
||||
input?: {
|
||||
[key: string]: AccessInputDefine;
|
||||
};
|
||||
@@ -39,13 +42,32 @@ export type AccessContext = {
|
||||
logger: ILogger;
|
||||
utils: typeof utils;
|
||||
accessService: IAccessService;
|
||||
serviceGetter?: IServiceGetter;
|
||||
define?: AccessDefine;
|
||||
};
|
||||
|
||||
export abstract class BaseAccess implements IAccess {
|
||||
ctx!: AccessContext;
|
||||
runtimeDepsService?: {
|
||||
ensureRuntimeDependencies(pluginKeys: string | string[]): Promise<any>;
|
||||
importRuntime(specifier: string): Promise<any>;
|
||||
};
|
||||
|
||||
setCtx(ctx: AccessContext) {
|
||||
async importRuntime(specifier: string) {
|
||||
if (!this.runtimeDepsService) {
|
||||
return await import(specifier);
|
||||
}
|
||||
return await this.runtimeDepsService.importRuntime(specifier);
|
||||
}
|
||||
|
||||
async setCtx(ctx: AccessContext) {
|
||||
this.ctx = ctx;
|
||||
if (!this.runtimeDepsService && this.ctx.serviceGetter) {
|
||||
this.runtimeDepsService = await this.ctx.serviceGetter.get("runtimeDepsService");
|
||||
}
|
||||
if (this.runtimeDepsService && this.ctx.define?.name) {
|
||||
await this.runtimeDepsService.ensureRuntimeDependencies(`access:${this.ctx.define.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
async onRequest(req: AccessRequestHandleReq) {
|
||||
|
||||
@@ -67,7 +67,9 @@ export async function newAccess(type: string, input: any, accessService: IAccess
|
||||
accessService,
|
||||
};
|
||||
}
|
||||
access.setCtx(ctx);
|
||||
ctx.define = ctx.define || register.define;
|
||||
access.runtimeDepsService = (accessService as any).runtimeDepsService;
|
||||
await access.setCtx(ctx);
|
||||
access._type = type;
|
||||
return access;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user