diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index 22d8eeee..c03434ad 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -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/yadan@2.0.0/node_modules/yadan/node_modules', + // 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory + // 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules', + // 'node_modules/.pnpm/egg@2.33.1/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) { diff --git a/test/fixtures/plugin-pnpm/config/plugin.js b/test/fixtures/plugin-pnpm/config/plugin.js new file mode 100644 index 00000000..29ae5932 --- /dev/null +++ b/test/fixtures/plugin-pnpm/config/plugin.js @@ -0,0 +1,10 @@ +'use strict'; + +module.exports = { + a: { + enable: true, + }, + b: { + enable: true, + }, +}; diff --git a/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/config/config.default.js b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/config/config.default.js new file mode 100644 index 00000000..4a6597ba --- /dev/null +++ b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/config/config.default.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + a: 'a', +}; diff --git a/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/package.json b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/package.json new file mode 100644 index 00000000..44d21f1f --- /dev/null +++ b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/package.json @@ -0,0 +1,3 @@ +{ + "name": "a" +} diff --git a/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/framework/package.json b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/framework/package.json new file mode 100644 index 00000000..485c9a9e --- /dev/null +++ b/test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/framework/package.json @@ -0,0 +1,3 @@ +{ + "name": "yadan" +} diff --git a/test/fixtures/plugin-pnpm/node_modules/b/config/config.default.js b/test/fixtures/plugin-pnpm/node_modules/b/config/config.default.js new file mode 100644 index 00000000..d2b5af6f --- /dev/null +++ b/test/fixtures/plugin-pnpm/node_modules/b/config/config.default.js @@ -0,0 +1,5 @@ +'use strict'; + +module.exports = { + b: 'b', +}; diff --git a/test/fixtures/plugin-pnpm/node_modules/b/package.json b/test/fixtures/plugin-pnpm/node_modules/b/package.json new file mode 100644 index 00000000..09dc2b7d --- /dev/null +++ b/test/fixtures/plugin-pnpm/node_modules/b/package.json @@ -0,0 +1,5 @@ +{ + "eggPlugin": { + "name": "b" + } +} diff --git a/test/fixtures/plugin-pnpm/package.json b/test/fixtures/plugin-pnpm/package.json new file mode 100644 index 00000000..e59462f1 --- /dev/null +++ b/test/fixtures/plugin-pnpm/package.json @@ -0,0 +1,3 @@ +{ + "name": "egg-plugin-pnpm" +} diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index a3f74d2a..f91993ce 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -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/framework@1.0.0/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, });