Skip to content

Commit a9fc514

Browse files
authored
fix: plugin loader support pnpm (#239)
1 parent 792be2e commit a9fc514

10 files changed

Lines changed: 86 additions & 4 deletions

File tree

lib/loader/mixin/plugin.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -343,18 +343,21 @@ module.exports = {
343343
for (let i = this.eggPaths.length - 1; i >= 0; i--) {
344344
const eggPath = this.eggPaths[i];
345345
lookupDirs.add(path.join(eggPath, 'node_modules'));
346+
}
346347

347-
// should find the siblings directory of framework when using pnpm
348+
// should find the $cwd/node_modules when test the plugins under npm3
349+
lookupDirs.add(path.join(process.cwd(), 'node_modules'));
350+
351+
// should find the siblings directory of framework when using pnpm
352+
for (let i = this.eggPaths.length - 1; i >= 0; i--) {
353+
const eggPath = this.eggPaths[i];
348354
// 'node_modules/.pnpm/yadan@2.0.0/node_modules/yadan/node_modules',
349355
// 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory
350356
// 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
351357
// 'node_modules/.pnpm/egg@2.33.1/node_modules', <- this is the sibling directory
352358
lookupDirs.add(path.dirname(eggPath));
353359
}
354360

355-
// should find the $cwd/node_modules when test the plugins under npm3
356-
lookupDirs.add(path.join(process.cwd(), 'node_modules'));
357-
358361
for (let dir of lookupDirs) {
359362
dir = path.join(dir, name);
360363
if (fs.existsSync(dir)) {

test/fixtures/plugin-duplicate/node_modules/@scope/a/config/config.default.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/plugin-duplicate/node_modules/@scope/a/package.json

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/plugin-duplicate/node_modules/@scope/b/config/config.default.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/plugin-duplicate/node_modules/@scope/b/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/plugin-duplicate/node_modules/a/config/config.default.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/plugin-duplicate/node_modules/a/package.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
3+
module.exports = {
4+
b: {
5+
enable: true,
6+
package: '@scope/b',
7+
},
8+
"a-duplicate": {
9+
enable: true,
10+
package: '@scope/a',
11+
},
12+
a: {
13+
enable: true,
14+
package: 'a',
15+
},
16+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "plugin-duplicate"
3+
}

test/loader/mixin/load_plugin.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,4 +605,43 @@ describe('test/load_plugin.test.js', function() {
605605
assert(loader.allPlugins.gw.enable === false);
606606
assert(loader.allPlugins.rpcServer.enable === false);
607607
});
608+
609+
it('should load plugin with duplicate plugin dir from eggPaths', () => {
610+
class BaseApplication extends EggCore {
611+
get [Symbol.for('egg#loader')]() {
612+
return EggLoader;
613+
}
614+
get [Symbol.for('egg#eggPath')]() {
615+
return utils.getFilepath(path.join('plugin-duplicate'));
616+
}
617+
}
618+
619+
class Application extends BaseApplication {
620+
get [Symbol.for('egg#loader')]() {
621+
return EggLoader;
622+
}
623+
get [Symbol.for('egg#eggPath')]() {
624+
return utils.getFilepath(path.join('plugin-duplicate', 'node_modules', '@scope', 'b'));
625+
}
626+
}
627+
628+
const baseDir = utils.getFilepath('plugin-duplicate');
629+
app = utils.createApp(path.join('plugin-duplicate', 'release'), {
630+
Application,
631+
});
632+
const loader = app.loader;
633+
loader.loadPlugin();
634+
loader.loadConfig();
635+
636+
assert.deepEqual(loader.plugins['a-duplicate'], {
637+
enable: true,
638+
name: 'a-duplicate',
639+
dependencies: [],
640+
optionalDependencies: [ 'a' ],
641+
env: [],
642+
package: '@scope/a',
643+
path: path.join(baseDir, 'node_modules', '@scope', 'a'),
644+
from: path.join(baseDir, 'release', 'config', 'plugin.js'),
645+
});
646+
});
608647
});

0 commit comments

Comments
 (0)