-
-
Notifications
You must be signed in to change notification settings - Fork 128
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
base: master
Are you sure you want to change the base?
Changes from 3 commits
52dd114
095c928
3377c94
056f2b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
lib[subName] = sub; | ||
} catch (e) { | ||
if (e.message.startsWith("Cannot find module '")) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
const moduleName = e.message.substring(20, e.message.indexOf("'\n")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 20 is a magic number |
||
const optional = pkg.peerDependenciesMeta?.[moduleName]?.optional; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just remove |
||
} | ||
} | ||
} | ||
return lib; | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicate expression:
lib[subName]
There was a problem hiding this comment.
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]
andsub[subName]
would be equal toundefined
.