Skip to content

Commit c8b5dcc

Browse files
authored
fix: compatible with old version egg-core package name (#38)
egg-bin v6 版本启动项目时,[无法读到 cluster.listen.port 的配置](eggjs/bin#280) 原因是 [egg-bin 6.13.0](eggjs/bin#278) 版本引入了 egg-utils v4,而 v4 在查找 egg-core 包时,包名从写死的 `egg-core` 改成了 `@eggjs/core`,如果 eggjs 版本没跟上,子依赖 egg-core 没升级,egg-utils 就会查找失败 <img width="2069" alt="截图_280f31b0-8db9-4fa3-9fdc-a17975625c5e" src="https://github.com/user-attachments/assets/d6b91d54-0ea3-4733-b9aa-a03e9aac1621" /> 一种可能的解决方法是同时查找两个包名,如 PR 内容所示 <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Bug Fixes** - Improved module import logic for the Egg framework - Enhanced backward compatibility with different module naming conventions - Streamlined error handling for module imports <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 9326f75 commit c8b5dcc

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

src/plugin.ts

+26-21
Original file line numberDiff line numberDiff line change
@@ -133,30 +133,35 @@ async function findEggCore(options: LoaderOptions): Promise<{ EggCore?: object;
133133
debug('[findEggCore] import "egg" from paths:%o error: %o', paths, err);
134134
}
135135

136-
const name = '@eggjs/core';
137-
// egg => egg-core
138-
try {
139-
const { EggCore, EggLoader } = await importModule(name, { paths });
140-
if (EggLoader) {
141-
return { EggCore, EggLoader };
136+
// egg-core 在 6.2.3 版本中更名为 @eggjs/core,为兼容老版本,支持同时查找两个包,优先使用新名字
137+
const names = [ '@eggjs/core', 'egg-core' ];
138+
for (const name of names) {
139+
try {
140+
const { EggCore, EggLoader } = await importModule(name, { paths });
141+
if (EggLoader) {
142+
return { EggCore, EggLoader };
143+
}
144+
} catch (err: any) {
145+
debug('[findEggCore] import "%s" from paths:%o error: %o', name, paths, err);
142146
}
143-
} catch (err: any) {
144-
debug('[findEggCore] import "%s" from paths:%o error: %o', name, paths, err);
145-
}
146147

147-
try {
148-
const { EggCore, EggLoader } = await importModule(name);
149-
if (EggLoader) {
150-
return { EggCore, EggLoader };
148+
try {
149+
const { EggCore, EggLoader } = await importModule(name);
150+
if (EggLoader) {
151+
return { EggCore, EggLoader };
152+
}
153+
} catch (err: any) {
154+
debug('[findEggCore] import "%s" error: %o', name, err);
151155
}
152-
} catch (err: any) {
153-
debug('[findEggCore] import "%s" error: %o', name, err);
154-
}
155156

156-
let eggCorePath = path.join(options.baseDir, `node_modules/${name}`);
157-
if (!(await exists(eggCorePath))) {
158-
eggCorePath = path.join(options.framework, `node_modules/${name}`);
157+
let eggCorePath = path.join(options.baseDir, `node_modules/${name}`);
158+
if (!(await exists(eggCorePath))) {
159+
eggCorePath = path.join(options.framework, `node_modules/${name}`);
160+
}
161+
if (await exists(eggCorePath)) {
162+
return await importModule(eggCorePath);
163+
}
159164
}
160-
assert(await exists(eggCorePath), `Can't find ${name} from ${options.baseDir} and ${options.framework}`);
161-
return await importModule(eggCorePath);
165+
166+
assert(false, `Can't find ${names.join(' or ')} from ${options.baseDir} and ${options.framework}`);
162167
}

0 commit comments

Comments
 (0)