Skip to content

Commit

Permalink
fix: compatible with old version egg-core package name (#38)
Browse files Browse the repository at this point in the history
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 -->
  • Loading branch information
rrbe authored Jan 8, 2025
1 parent 9326f75 commit c8b5dcc
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,30 +133,35 @@ async function findEggCore(options: LoaderOptions): Promise<{ EggCore?: object;
debug('[findEggCore] import "egg" from paths:%o error: %o', paths, err);
}

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

try {
const { EggCore, EggLoader } = await importModule(name);
if (EggLoader) {
return { EggCore, EggLoader };
try {
const { EggCore, EggLoader } = await importModule(name);
if (EggLoader) {
return { EggCore, EggLoader };
}
} catch (err: any) {
debug('[findEggCore] import "%s" error: %o', name, err);
}
} catch (err: any) {
debug('[findEggCore] import "%s" error: %o', name, err);
}

let eggCorePath = path.join(options.baseDir, `node_modules/${name}`);
if (!(await exists(eggCorePath))) {
eggCorePath = path.join(options.framework, `node_modules/${name}`);
let eggCorePath = path.join(options.baseDir, `node_modules/${name}`);
if (!(await exists(eggCorePath))) {
eggCorePath = path.join(options.framework, `node_modules/${name}`);
}
if (await exists(eggCorePath)) {
return await importModule(eggCorePath);
}
}
assert(await exists(eggCorePath), `Can't find ${name} from ${options.baseDir} and ${options.framework}`);
return await importModule(eggCorePath);

assert(false, `Can't find ${names.join(' or ')} from ${options.baseDir} and ${options.framework}`);
}

0 comments on commit c8b5dcc

Please sign in to comment.