From 68997e97e17c837f508f404b24b416577383da10 Mon Sep 17 00:00:00 2001 From: TZ Date: Fri, 7 Jan 2022 09:04:50 +0800 Subject: [PATCH 1/3] feat: support pnpm node_modules style --- lib/loader/mixin/plugin.js | 15 +++++-- test/fixtures/plugin-pnpm/config/plugin.js | 10 +++++ .../node_modules/a/config/config.default.js | 5 +++ .../node_modules/a/package.json | 3 ++ .../node_modules/framework/package.json | 3 ++ .../node_modules/b/config/config.default.js | 5 +++ .../plugin-pnpm/node_modules/b/package.json | 5 +++ test/fixtures/plugin-pnpm/package.json | 3 ++ test/loader/mixin/load_plugin.test.js | 39 ++++++++++++++----- 9 files changed, 76 insertions(+), 12 deletions(-) create mode 100644 test/fixtures/plugin-pnpm/config/plugin.js create mode 100644 test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/config/config.default.js create mode 100644 test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/a/package.json create mode 100644 test/fixtures/plugin-pnpm/node_modules/.pnpm/framework@1.0.0/node_modules/framework/package.json create mode 100644 test/fixtures/plugin-pnpm/node_modules/b/config/config.default.js create mode 100644 test/fixtures/plugin-pnpm/node_modules/b/package.json create mode 100644 test/fixtures/plugin-pnpm/package.json diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index 22d8eeee..1471957b 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -333,16 +333,25 @@ module.exports = { const name = plugin.package || plugin.name; const lookupDirs = []; - // 尝试在以下目录找到匹配的插件 + // 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')); - // 到 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')); + let frameworkDepsPath = path.join(eggPath, 'node_modules'); + if (fs.existsSync(frameworkDepsPath)) { + lookupDirs.push(path.join(eggPath, 'node_modules')); + } else { + // should find the siblings directory of framework when using pnpm + frameworkDepsPath = path.dirname(eggPath); + if (fs.existsSync(frameworkDepsPath)) { + lookupDirs.push(frameworkDepsPath); + } + } } // should find the $cwd/node_modules when test the plugins under npm3 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..833bbe49 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.only('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, }); From a750f853aea3e82edfda7f9cb7789fb1a87ba0e7 Mon Sep 17 00:00:00 2001 From: TZ Date: Fri, 7 Jan 2022 09:59:00 +0800 Subject: [PATCH 2/3] fix: add dirname of eggPath directly && use Set --- lib/loader/mixin/plugin.js | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index 1471957b..c23c6e18 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -331,31 +331,24 @@ 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')); // 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]; - let frameworkDepsPath = path.join(eggPath, 'node_modules'); - if (fs.existsSync(frameworkDepsPath)) { - lookupDirs.push(path.join(eggPath, 'node_modules')); - } else { - // should find the siblings directory of framework when using pnpm - frameworkDepsPath = path.dirname(eggPath); - if (fs.existsSync(frameworkDepsPath)) { - lookupDirs.push(frameworkDepsPath); - } - } + lookupDirs.add(path.join(eggPath, 'node_modules')); + // should find the siblings directory of framework when using pnpm + 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); @@ -364,7 +357,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) { From 2c8b284059de37b151ddfa0e2e1354b2b054f743 Mon Sep 17 00:00:00 2001 From: TZ Date: Fri, 7 Jan 2022 10:02:17 +0800 Subject: [PATCH 3/3] chore: add comment --- lib/loader/mixin/plugin.js | 5 +++++ test/loader/mixin/load_plugin.test.js | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/loader/mixin/plugin.js b/lib/loader/mixin/plugin.js index c23c6e18..c03434ad 100644 --- a/lib/loader/mixin/plugin.js +++ b/lib/loader/mixin/plugin.js @@ -343,7 +343,12 @@ module.exports = { for (let i = this.eggPaths.length - 1; i >= 0; i--) { const eggPath = this.eggPaths[i]; 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)); } diff --git a/test/loader/mixin/load_plugin.test.js b/test/loader/mixin/load_plugin.test.js index 833bbe49..f91993ce 100644 --- a/test/loader/mixin/load_plugin.test.js +++ b/test/loader/mixin/load_plugin.test.js @@ -86,7 +86,7 @@ describe('test/load_plugin.test.js', function() { }); }); - it.only('should support pnpm node_modules style', () => { + it('should support pnpm node_modules style', () => { class Application extends EggCore { get [ Symbol.for('egg#loader') ]() { return EggLoader;