Skip to content

Commit 02bb843

Browse files
authored
refactor: use require.resolve instead of fs.exists (#238)
1 parent 91c04cc commit 02bb843

20 files changed

Lines changed: 108 additions & 25 deletions

File tree

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
node-version: ${{ matrix.node-version }}
3636

3737
- name: Install Dependencies
38-
run: npm i -g npminstall && npminstall
38+
run: npm i -g npminstall@5 && npminstall
3939

4040
- name: Continuous Integration
4141
run: npm run ci

lib/loader/mixin/plugin.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -334,39 +334,31 @@ module.exports = {
334334
const name = plugin.package || plugin.name;
335335
const lookupDirs = new Set();
336336

337-
// try to locate the plugin in the following directories
338-
// -> {APP_PATH}/node_modules
339-
// -> {EGG_PATH}/node_modules
340-
// -> $CWD/node_modules
341-
lookupDirs.add(path.join(this.options.baseDir, 'node_modules'));
337+
// try to locate the plugin in the following directories's node_modules
338+
// -> {APP_PATH} -> {EGG_PATH} -> $CWD
339+
lookupDirs.add(this.options.baseDir);
342340

343341
// try to locate the plugin at framework from upper to lower
344342
for (let i = this.eggPaths.length - 1; i >= 0; i--) {
345343
const eggPath = this.eggPaths[i];
346-
lookupDirs.add(path.join(eggPath, 'node_modules'));
344+
lookupDirs.add(eggPath);
347345
}
348346

349-
// should find the $cwd/node_modules when test the plugins under npm3
350-
lookupDirs.add(path.join(process.cwd(), 'node_modules'));
347+
// should find the $cwd when test the plugins under npm3
348+
lookupDirs.add(process.cwd());
351349

352-
// should find the siblings directory of framework when using pnpm
353-
for (let i = this.eggPaths.length - 1; i >= 0; i--) {
354-
const eggPath = this.eggPaths[i];
350+
try {
351+
// should find the plugin directory
352+
// pnpm will lift the node_modules to the sibling directory
355353
// 'node_modules/.pnpm/yadan@2.0.0/node_modules/yadan/node_modules',
356354
// 'node_modules/.pnpm/yadan@2.0.0/node_modules', <- this is the sibling directory
357355
// 'node_modules/.pnpm/egg@2.33.1/node_modules/egg/node_modules',
358356
// 'node_modules/.pnpm/egg@2.33.1/node_modules', <- this is the sibling directory
359-
lookupDirs.add(path.dirname(eggPath));
360-
}
361-
362-
for (let dir of lookupDirs) {
363-
dir = path.join(dir, name);
364-
if (fs.existsSync(dir)) {
365-
return fs.realpathSync(dir);
366-
}
357+
const filePath = require.resolve(`${name}/package.json`, { paths: [ ...lookupDirs ] });
358+
return path.dirname(filePath);
359+
} catch (_) {
360+
throw new Error(`Can not find plugin ${name} in "${[ ...lookupDirs ].join(', ')}"`);
367361
}
368-
369-
throw new Error(`Can not find plugin ${name} in "${[ ...lookupDirs ].join(', ')}"`);
370362
},
371363

372364
_extendPlugins(target, plugins) {

test/fixtures/application/node_modules/a/package.json

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

test/fixtures/application/node_modules/b/package.json

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

test/fixtures/extend/node_modules/a/package.json

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

test/fixtures/extend/node_modules/b/package.json

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

test/fixtures/helper/node_modules/a/package.json

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

test/fixtures/helper/node_modules/b/package.json

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

test/fixtures/middleware-override/node_modules/a/package.json

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

test/fixtures/middleware-override/node_modules/b/package.json

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

0 commit comments

Comments
 (0)