Skip to content

Commit

Permalink
fix: npm包的入口文件不作为app载入
Browse files Browse the repository at this point in the history
  • Loading branch information
CakmLexi committed Jul 16, 2024
1 parent 8dec58e commit 54cdd02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
8 changes: 6 additions & 2 deletions src/core/plugin.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -280,15 +280,19 @@ 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<any>[] = []
let path = `${this.dirPath}${isNpm ? 'node_modules' : 'plugins'}/${dir}/${name}`
if (isOrderBy) path = path + `?${Date.now()}`

const tmp: Array<(NewMessagePlugin) | PluginApps> = await import(path)

/** npm包的入口文件不作为app载入 只加载 */
if (isMain) return true

lodash.forEach(tmp, (App) => {
const index = this.index
this.index++
Expand Down
12 changes: 6 additions & 6 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ export const common = new (class Common {
* @param showDetails - 是否返回详细信息,默认为false
* 默认只返回插件npm包名,为true时返回详细的{dir, name}[]
*/
async getNpmPlugins<T extends boolean> (showDetails: T): Promise<T extends true ? { dir: string; name: fileName }[] : string[]> {
async getNpmPlugins<T extends boolean> (showDetails: T): Promise<T extends true ? { dir: string; name: fileName, isMain: boolean }[] : string[]> {
/** 屏蔽的依赖包列表 */
const pkgdependencies = [
'@grpc/grpc-js',
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand All @@ -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 })
})
})
}
Expand All @@ -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[]
}
}

Expand Down

0 comments on commit 54cdd02

Please sign in to comment.