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

Conversation

DmitryScaletta
Copy link

1. Don't load submodules with not installed optional dependencies

Some packages may have optional dependencies.
If there was an error during loading a submodue, and it's a missing optional dependency error, just skip this submodule.

Closes #1937

2. Skip submodules if they have already been loaded as a part of main module

Example: some-module

package.json

{
  "exports": {
    ".": {
      // ...
      "default": "./index.js"
    },
    "./submodule": {
      // ...
      "default": "./submodule.js"
    }
  }
}

index.js

export * from './submodule';

submodule.js

export const submodule = () => {};

The main module exports all from the submodule.
And the submodule exports a function with the same name as a submodule name.
So when loading the submodule, lib already have a property submodule.

Closes #1938

  • tests and linter show no problems (npm t)
  • tests are added/updated for bug fixes and new features
  • code is properly formatted (npm run fmt)
  • description of changes is added in CHANGELOG.md
  • update .d.ts typings

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/deps.js Outdated
if (lib[subName] && lib[subName] === sub[subName]) continue;
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

lib/deps.js Outdated
lib[subName] = sub;
} catch (e) {
if (e.message.startsWith("Cannot find module '")) {
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

lib/deps.js Outdated
} catch (e) {
if (e.message.startsWith("Cannot find module '")) {
const moduleName = e.message.substring(20, e.message.indexOf("'\n"));
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

lib/deps.js Outdated
Comment on lines 65 to 66
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants