Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve loading of submodules #1940

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/deps.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,18 @@ const loadModule = (name) => {
const subKeys = Object.keys(pkg.exports).map((key) => key.substring(2));
const subNames = subKeys.filter(validSubmodules);
for (const subName of subNames) {
const sub = appRequire(name + '/' + subName);
lib[subName] = sub;
try {
const sub = appRequire(name + '/' + subName);
if (lib[subName] && lib[subName] === sub[subName]) continue;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate expression: lib[subName]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is actually necessary here because in every other case (except the one I described above) lib[subName] and sub[subName] would be equal to undefined.

lib[subName] = sub;
} catch (e) {
if (e.message.startsWith("Cannot find module '")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used linter before commit.
It didn't fix the quotes.
Looks like prettier wants to use double quotes if string contains a single quote character: '
image

const moduleName = e.message.substring(20, e.message.indexOf("'\n"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

20 is a magic number

const optional = pkg.peerDependenciesMeta?.[moduleName]?.optional;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Double optional chaining is not ok in out codebase

if (optional) continue;
else throw e;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just remove else here

}
}
}
return lib;
};
Expand Down
Loading