egg icon indicating copy to clipboard operation
egg copied to clipboard

[Feature Request] egg 支持 monorepo

Open lwyj123 opened this issue 5 years ago • 5 comments

Background

monorepo 已经逐渐成为了一些项目或团队开发的主要模式,正如之前在 #3680 之中讨论的一样,但遗憾的是这个 issue 在仍未被解决的情况下被关闭了,而且长期没有进展。正巧我前几天使用eggjs遇到了同样的问题,但之前并没有给出结论,所以特意新开了一个 issue。

现在我是使用的 yarn workspace 的 nohoist 解决的,但我认为这个需要 egg 官方进行处理。

    "workspaces": {
        "packages": ["packages/*"],
        "nohoist": [
            "**/egg",
            "**/egg/**"
        ]
    },

关于具体问题 @meteorlxy 已经做了充分的说明:

目前在 yarn workspace 下确实会提示各种找不到库。

我看到目前的代码很多是通过在各种 node_modules 目录下查找来确定是否有依赖,包括 midway 团队那边也是这么写的。

而在 monorepo 的情况下,lerna 或者 yarn workspace 大都会把依赖统一放在 root dir 的 node_modules 里,虽然在单个 workspace 内部的 node_modules 中找不到,但是在 root dir 中是有的。可以考虑增加一个类似 require.resolve() 的方式去进一步查找。

我简单试了一下,对于 egg-utils/lib/framework.js 的相关代码:https://github.com/eggjs/egg-utils/blob/d8072dbd145832bf712762324591bd0ebe34ff85/lib/framework.js#L52-L67

加了个尝试通过 require.resolve() 查找后,问题解决了,可以作为参考:

const moduleDirs = new Set([
  moduleDir,
  // find framework from process.cwd, especially for test,
  // the application is in test/fixtures/app,
  // and framework is install in ${cwd}/node_modules
  path.join(process.cwd(), 'node_modules'),
  // prevent from mocking process.cwd
  path.join(initCwd, 'node_modules'),
]);

try {
  const test = path.join(require.resolve(`${frameworkName}/package.json`), '../..')
  moduleDirs.add(test);
} catch (e) {}

Proposal

修正 eggjs 内部搜索 package 的方式,使用和 require 同样的路径查找方式

lwyj123 avatar Apr 23 '20 05:04 lwyj123

确实monorepo越来越多了

beiyu98 avatar Apr 27 '20 03:04 beiyu98

I have chosen nestjs for my next project.

lwyj123 avatar Jun 12 '20 05:06 lwyj123

+1,官方为啥不支持下?我理解成本不高

younth avatar Jul 24 '21 09:07 younth

+1

dennisleung avatar Aug 24 '21 05:08 dennisleung

+1

luopenghu avatar Sep 19 '21 13:09 luopenghu