-
Notifications
You must be signed in to change notification settings - Fork 92
feat: support pnpm node_modules style #237
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -331,22 +331,29 @@ module.exports = { | |
| } | ||
|
|
||
| const name = plugin.package || plugin.name; | ||
| const lookupDirs = []; | ||
| const lookupDirs = new Set(); | ||
|
|
||
| // 尝试在以下目录找到匹配的插件 | ||
| // try to locate the plugin in the following directories | ||
| // -> {APP_PATH}/node_modules | ||
| // -> {EGG_PATH}/node_modules | ||
| // -> $CWD/node_modules | ||
| lookupDirs.push(path.join(this.options.baseDir, 'node_modules')); | ||
| lookupDirs.add(path.join(this.options.baseDir, 'node_modules')); | ||
|
|
||
| // 到 egg 中查找,优先从外往里查找 | ||
| // try to locate the plugin at framework from upper to lower | ||
| for (let i = this.eggPaths.length - 1; i >= 0; i--) { | ||
| const eggPath = this.eggPaths[i]; | ||
| lookupDirs.push(path.join(eggPath, 'node_modules')); | ||
| lookupDirs.add(path.join(eggPath, 'node_modules')); | ||
|
|
||
| // should find the siblings directory of framework when using pnpm | ||
| // 'node_modules/.pnpm/[email protected]/node_modules/yadan/node_modules', | ||
| // 'node_modules/.pnpm/[email protected]/node_modules', <- this is the sibling directory | ||
| // 'node_modules/.pnpm/[email protected]/node_modules/egg/node_modules', | ||
| // 'node_modules/.pnpm/[email protected]/node_modules', <- this is the sibling directory | ||
| lookupDirs.add(path.dirname(eggPath)); | ||
| } | ||
|
|
||
| // 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) { | ||
| dir = path.join(dir, name); | ||
|
|
@@ -355,7 +362,7 @@ module.exports = { | |
| } | ||
| } | ||
|
|
||
| throw new Error(`Can not find plugin ${name} in "${lookupDirs.join(', ')}"`); | ||
| throw new Error(`Can not find plugin ${name} in "${[ ...lookupDirs ].join(', ')}"`); | ||
| }, | ||
|
|
||
| _extendPlugins(target, plugins) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| 'use strict'; | ||
|
|
||
| module.exports = { | ||
| a: { | ||
| enable: true, | ||
| }, | ||
| b: { | ||
| enable: true, | ||
| }, | ||
| }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "name": "egg-plugin-pnpm" | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -86,6 +86,28 @@ describe('test/load_plugin.test.js', function() { | |
| }); | ||
| }); | ||
|
|
||
| it('should support pnpm node_modules style', () => { | ||
| class Application extends EggCore { | ||
| get [ Symbol.for('egg#loader') ]() { | ||
| return EggLoader; | ||
| } | ||
| get [ Symbol.for('egg#eggPath') ]() { | ||
| return utils.getFilepath('plugin-pnpm/node_modules/.pnpm/[email protected]/node_modules/framework'); | ||
| } | ||
| } | ||
| app = utils.createApp('plugin-pnpm', { | ||
| Application, | ||
| }); | ||
| const loader = app.loader; | ||
| loader.loadPlugin(); | ||
| loader.loadConfig(); | ||
| console.log(loader.plugins, loader.config); | ||
| assert(loader.plugins.a); | ||
| assert(loader.plugins.b); | ||
| assert(loader.config.a === 'a'); | ||
| assert(loader.config.b === 'b'); | ||
| }); | ||
|
|
||
| it('should support alias', function() { | ||
| const baseDir = utils.getFilepath('plugin'); | ||
| app = utils.createApp('plugin'); | ||
|
|
@@ -406,16 +428,15 @@ describe('test/load_plugin.test.js', function() { | |
| assert(plugin.path === utils.getFilepath('realpath/a')); | ||
| }); | ||
|
|
||
| class Application extends EggCore { | ||
| get [Symbol.for('egg#loader')]() { | ||
| return EggLoader; | ||
| } | ||
| get [Symbol.for('egg#eggPath')]() { | ||
| return utils.getFilepath('plugin-from/framework'); | ||
| } | ||
| } | ||
|
|
||
| it('should get the defining plugin path in every plugin', () => { | ||
| class Application extends EggCore { | ||
| get [ Symbol.for('egg#loader') ]() { | ||
| return EggLoader; | ||
| } | ||
| get [ Symbol.for('egg#eggPath') ]() { | ||
| return utils.getFilepath('plugin-from/framework'); | ||
| } | ||
| } | ||
| app = utils.createApp('plugin-from', { | ||
| Application, | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 目录。潜在的问题:
@fengmk2 @hyj1991 @mansonchor @popomore 你们怎么看?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来也没有,不算 break。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好,我也倾向于用 resolve, 不过肯定会 break 到一些不规范的用户的,之前没这个规范,但我们也没有强制限制。
我再提个 PR
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#238
There was a problem hiding this comment.
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