Skip to content

Commit 7c6353f

Browse files
authored
feat: extract plugin loader method for override (#246)
* feat: extract plugin loader method for override * chore: fix load plugin order
1 parent 11709f9 commit 7c6353f

2 files changed

Lines changed: 26 additions & 16 deletions

File tree

lib/loader/mixin/plugin.js

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ module.exports = {
5959
loadPlugin() {
6060
this.timing.start('Load Plugin');
6161

62+
this.lookupDirs = this.getLookupDirs();
6263
this.allPlugins = {};
63-
this.appPlugins = this.loadAppPlugins();
6464
this.eggPlugins = this.loadEggPlugins();
65+
this.appPlugins = this.loadAppPlugins();
6566
this.customPlugins = this.loadCustomPlugins();
6667

6768
this._extendPlugins(this.allPlugins, this.eggPlugins);
@@ -336,17 +337,7 @@ module.exports = {
336337
return result.sequence.map(name => allPlugins[name]);
337338
},
338339

339-
// Get the real plugin path
340-
getPluginPath(plugin) {
341-
if (plugin.path) {
342-
return plugin.path;
343-
}
344-
345-
if (plugin.package) {
346-
assert(isValidatePackageName(plugin.package), `plugin ${plugin.name} invalid, use 'path' instead of package: "${plugin.package}"`);
347-
}
348-
349-
const name = plugin.package || plugin.name;
340+
getLookupDirs() {
350341
const lookupDirs = new Set();
351342

352343
// try to locate the plugin in the following directories's node_modules
@@ -362,17 +353,36 @@ module.exports = {
362353
// should find the $cwd when test the plugins under npm3
363354
lookupDirs.add(process.cwd());
364355

356+
return lookupDirs;
357+
},
358+
359+
// Get the real plugin path
360+
getPluginPath(plugin) {
361+
if (plugin.path) {
362+
return plugin.path;
363+
}
364+
365+
if (plugin.package) {
366+
assert(isValidatePackageName(plugin.package), `plugin ${plugin.name} invalid, use 'path' instead of package: "${plugin.package}"`);
367+
}
368+
369+
return this._resolvePluginPath(plugin);
370+
},
371+
372+
_resolvePluginPath(plugin) {
373+
const name = plugin.package || plugin.name;
374+
365375
try {
366376
// should find the plugin directory
367377
// pnpm will lift the node_modules to the sibling directory
368378
// 'node_modules/.pnpm/yadan@2.0.0/node_modules/yadan/node_modules',
369379
// 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory
370380
// 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
371381
// 'node_modules/.pnpm/egg@2.33.1/node_modules', <- this is the sibling directory
372-
const filePath = require.resolve(`${name}/package.json`, { paths: [ ...lookupDirs ] });
382+
const filePath = require.resolve(`${name}/package.json`, { paths: [ ...this.lookupDirs ] });
373383
return path.dirname(filePath);
374384
} catch (_) {
375-
throw new Error(`Can not find plugin ${name} in "${[ ...lookupDirs ].join(', ')}"`);
385+
throw new Error(`Can not find plugin ${name} in "${[ ...this.lookupDirs ].join(', ')}"`);
376386
}
377387
},
378388

test/loader/mixin/load_plugin.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ describe('test/load_plugin.test.js', function() {
7272
const loader = app.loader;
7373
const loaderOrders = [];
7474
[
75-
'loadAppPlugins',
7675
'loadEggPlugins',
76+
'loadAppPlugins',
7777
'loadCustomPlugins',
7878
].forEach(method => {
7979
mm(loader, method, () => {
@@ -84,8 +84,8 @@ describe('test/load_plugin.test.js', function() {
8484

8585
loader.loadPlugin();
8686
assert.deepEqual(loaderOrders, [
87-
'loadAppPlugins',
8887
'loadEggPlugins',
88+
'loadAppPlugins',
8989
'loadCustomPlugins',
9090
]);
9191
});

0 commit comments

Comments
 (0)