Skip to content

Commit

Permalink
feat(cli): support loader cache
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed May 26, 2021
1 parent 4053909 commit 5f1860c
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/koishi/src/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ function isErrorModule(error: any) {
return error.code !== 'MODULE_NOT_FOUND' || error.requireStack && error.requireStack[0] !== __filename
}

const cache: Record<string, [string, any]> = {}

function loadEcosystem(type: string, name: string) {
const key = `${type}:${name}`
if (key in cache) return cache[key]

const prefix = `koishi-${type}-`
const modules: string[] = []
if ('./'.includes(name[0])) {
Expand All @@ -68,7 +73,7 @@ function loadEcosystem(type: string, name: string) {
try {
const result = require(path)
logger.info('apply %s %c', type, result.name || name)
return [path, result]
return cache[key] = [path, result]
} catch (error) {
if (isErrorModule(error)) {
throw error
Expand Down

0 comments on commit 5f1860c

Please sign in to comment.