Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support pnpm node_modules style #237

Merged
merged 3 commits into from
Jan 7, 2022
Merged

feat: support pnpm node_modules style #237

merged 3 commits into from
Jan 7, 2022

Conversation

atian25
Copy link
Member

@atian25 atian25 commented Jan 7, 2022

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)
Description of change

due to pnpm's Symlinked node_modules structure

we should locate plugin not only from the framework's node_modules but also the siblings directory.

node_modules
├── .pnpm
│   └── egg@2.0.0
│       └── node_modules
│           ├── egg (do not has node_modules directory)
│           │   ├── index.js
│           │   └── package.json
│           └── egg-onerror (dependencies of egg but not at egg/node_modules)
│               ├── index.js
│               └── package.json
└── egg (symlinks to ./.pnpm/egg@2.0.0/node_modules/egg)

close eggjs/egg#4739

@codecov
Copy link

codecov bot commented Jan 7, 2022

Codecov Report

Merging #237 (2c8b284) into master (8ae91f8) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master     #237   +/-   ##
=======================================
  Coverage   99.74%   99.74%           
=======================================
  Files          19       19           
  Lines        1178     1179    +1     
  Branches      207      207           
=======================================
+ Hits         1175     1176    +1     
  Misses          3        3           
Impacted Files Coverage Δ
lib/loader/mixin/plugin.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8ae91f8...2c8b284. Read the comment docs.

} else {
// should find the siblings directory of framework when using pnpm
frameworkDepsPath = path.dirname(eggPath);
if (fs.existsSync(frameworkDepsPath)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉这里是一个 break,会不会带来其它问题,因为不强制去 node_modules 目录查了,假如正常的 eggPath 平级有一些同名 js 文件都会被错误引入

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不判断是否存在了,直接加一个父级了。

@atian25
Copy link
Member Author

atian25 commented Jan 7, 2022

对各个包管理工具的 lookupdir 测试了下:

# pnpm

'node_modules',
'node_modules/.pnpm/yadan@2.0.0/node_modules/yadan/node_modules',
'node_modules/.pnpm/yadan@2.0.0/node_modules',
'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
'node_modules/.pnpm/egg@2.33.1/node_modules'


# tnpm

'egg-showcase/node_modules',
'egg-showcase/node_modules/_yadan@2.0.0@yadan/node_modules',
# (因为 tnpm 是扁平化的,yadan 的父目录是根目录 node_modules,所以被 Set 过滤掉了)
'egg-showcase/node_modules/_egg@2.33.1@egg/node_modules'


# npm

'node_modules',
'node_modules/yadan/node_modules',
# (因为 npm 是扁平化的,yadan 的父目录是根目录 node_modules,所以被 Set 过滤掉了)
'node_modules/egg/node_modules'

Copy link
Member

@mansonchor mansonchor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@hyj1991 hyj1991 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

}

// should find the $cwd/node_modules when test the plugins under npm3
lookupDirs.push(path.join(process.cwd(), 'node_modules'));
lookupDirs.add(path.join(process.cwd(), 'node_modules'));

for (let dir of lookupDirs) {
Copy link
Member Author

@atian25 atian25 Jan 7, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个地方,如果把原来的 fs.exists 改为 require.resolve(${name}/package.json, { paths: [...lookupDirs] }); 其实会更合理,使用到 require 自己的寻址机制,就不用在上面 hard code 目录。

潜在的问题:

  1. 寻址逻辑稍微复杂了一点,但基本上可以无视,后面支持 manifest 后就更没问题了。
  2. inline plugin 会存在一些问题,现在我们没有强制检测它必须有 package.json,就看这种我们是否算 break

@fengmk2 @hyj1991 @mansonchor @popomore 你们怎么看?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原来也没有,不算 break。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好,我也倾向于用 resolve, 不过肯定会 break 到一些不规范的用户的,之前没这个规范,但我们也没有强制限制。

我再提个 PR

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在这个 fn 的第一行,就会判断 plugin.path 是否存在,然后直接 return 了,所以不存在上面说的 break 的情况。
重新提交了下 #238

@fengmk2 fengmk2 merged commit 43f15ad into master Jan 7, 2022
@fengmk2 fengmk2 deleted the pnpm branch January 7, 2022 14:35
@fengmk2
Copy link
Member

fengmk2 commented Jan 7, 2022

@atian25 你来发版本。

@atian25
Copy link
Member Author

atian25 commented Jan 7, 2022

额。。。 这个 PR 还没改为 resolve,我再提一个 PR 吧

@atian25
Copy link
Member Author

atian25 commented Jan 7, 2022

发了 4.22.0, resolve 那个我晚点再提一个

Copy link
Member

@popomore popomore left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

使用 pnpm 安装 egg 依赖之后,pnpm dev 时报错,插件找不到
5 participants