diff --git a/src/core/plugin.loader.ts b/src/core/plugin.loader.ts index 7a9e65b..103b647 100644 --- a/src/core/plugin.loader.ts +++ b/src/core/plugin.loader.ts @@ -106,7 +106,7 @@ class PluginLoader { /** 获取npm插件 */ const npm = await common.getNpmPlugins(true) /** 载入npm插件 */ - promises.push(...npm.map(async ({ dir, name }) => await this.createdApp(dir, name, false, true))) + promises.push(...npm.map(async ({ dir, name, isMain }) => await this.createdApp(dir, name, false, true, isMain))) /** 等待所有插件加载完成 */ await Promise.all(promises) @@ -280,8 +280,9 @@ class PluginLoader { * @param name - 插件名称 * @param isOrderBy - 是否为动态导入 默认为静态导入 * @param isNpm - 是否为npm包 + * @param isMain - 是否为主入口文件 */ - async createdApp (dir: dirName, name: fileName, isOrderBy = false, isNpm = false) { + async createdApp (dir: dirName, name: fileName, isOrderBy = false, isNpm = false, isMain = false) { try { const list: Promise[] = [] let path = `${this.dirPath}${isNpm ? 'node_modules' : 'plugins'}/${dir}/${name}` @@ -289,6 +290,9 @@ class PluginLoader { const tmp: Array<(NewMessagePlugin) | PluginApps> = await import(path) + /** npm包的入口文件不作为app载入 只加载 */ + if (isMain) return true + lodash.forEach(tmp, (App) => { const index = this.index this.index++ diff --git a/src/utils/common.ts b/src/utils/common.ts index a43a7e1..88a1830 100644 --- a/src/utils/common.ts +++ b/src/utils/common.ts @@ -400,7 +400,7 @@ export const common = new (class Common { * @param showDetails - 是否返回详细信息,默认为false * 默认只返回插件npm包名,为true时返回详细的{dir, name}[] */ - async getNpmPlugins (showDetails: T): Promise { + async getNpmPlugins (showDetails: T): Promise { /** 屏蔽的依赖包列表 */ const pkgdependencies = [ '@grpc/grpc-js', @@ -435,9 +435,9 @@ export const common = new (class Common { } await Promise.all(dependencies.map(readPackageJson)) - return list as T extends true ? { dir: dirName; name: fileName }[] : string[] + return list as T extends true ? { dir: dirName; name: fileName, isMain: boolean }[] : string[] } else { - const list: { dir: dirName; name: string }[] = [] + const list: { dir: dirName; name: string, isMain: boolean }[] = [] const readPackageJson = async (name: string) => { try { @@ -446,7 +446,7 @@ export const common = new (class Common { if (pkg?.karin) { if (pkg?.main) { const dir = `${name}/${path.dirname(pkg.main).replace(/\.\//, '')}` - list.push({ dir, name: path.basename(pkg.main) }) + list.push({ dir, name: path.basename(pkg.main), isMain: true }) } if (pkg?.karin?.apps?.length) { @@ -455,7 +455,7 @@ export const common = new (class Common { /** 忽略非js */ if (!name.endsWith('.js')) return const dir = `${name}/${app}` - list.push({ dir, name }) + list.push({ dir, name, isMain: false }) }) }) } @@ -464,7 +464,7 @@ export const common = new (class Common { } await Promise.all(dependencies.map(readPackageJson)) - return list as T extends true ? { dir: dirName; name: fileName }[] : string[] + return list as T extends true ? { dir: dirName; name: fileName, isMain: boolean }[] : string[] } }