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

feat(node-resolve): add new option, modulePaths #1104

Merged
merged 8 commits into from
Sep 12, 2022

Conversation

ajhyndman
Copy link
Contributor

@ajhyndman ajhyndman commented Feb 10, 2022

Rollup Plugin Name: plugin-node-resolve

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

If yes, then include "BREAKING CHANGES:" in the first commit message body, followed by a description of what is breaking.

List any relevant issue numbers:

Description

This change introduces a new option for @rollup/plugin-node-resolve: modulePaths.

Unlike moduleDirectories, modulePaths allows the user to specify an absolute path to alternative locations (in addition to the importer's local directory) that should be searched for node modules.

This can be useful (for example) to support a CI system that caches node_modules in a non-standard location.

In node, this is supported via the NODE_PATH environment variable. Conveniently, this also brings @rollup/plugin-node-resolve into alignment with Jest's configuration options. See: https://jestjs.io/docs/configuration#modulepaths-arraystring

@ajhyndman ajhyndman force-pushed the node-resolve-modulePaths branch from f558fe8 to 56c0679 Compare February 10, 2022 00:14
@ajhyndman ajhyndman changed the title [node-resolve] Add new option: modulePaths feat(node-resolve) Add new option: modulePaths Feb 10, 2022
@ajhyndman ajhyndman changed the title feat(node-resolve) Add new option: modulePaths feat(node-resolve): Add new option: modulePaths Feb 10, 2022
@ajhyndman ajhyndman changed the title feat(node-resolve): Add new option: modulePaths feat(node-resolve): add new option, modulePaths Feb 22, 2022
@shellscape
Copy link
Collaborator

@tjenkinson please have a peek at this one

Copy link
Member

@tjenkinson tjenkinson left a comment

Choose a reason for hiding this comment

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

thanks @ajhyndman!

Looks good. Got a few suggestions

packages/node-resolve/README.md Outdated Show resolved Hide resolved
@@ -257,6 +257,36 @@ test('allows custom moduleDirectories with legacy customResolveOptions.moduleDir
t.snapshot(warnings);
});

test('custom moduleDirectories do not support nested dependencies', async (t) => {
Copy link
Member

Choose a reason for hiding this comment

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

I think it's good to test this but it would be nice to also actually flag this as a proper error if the user tries to provide a path instead of name for this option.

Please could we update the code to something like:

if (moduleDirectories.some((name) => name.includes('/'))) {
  throw new Error(
    '`moduleDirectories` option must only contain directory names. If you want to load modules from a custom directory see `modulePaths`.'
  );
}

in index.js

Then this test can just check that

nodeResolve({
  moduleDirectories: ['some/path']
})

throws that error

Copy link
Contributor Author

@ajhyndman ajhyndman Apr 15, 2022

Choose a reason for hiding this comment

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

Yeah, that sounds awesome! Would this change be breaking though? Should it require a major version bump? I'm concerned that there may be a large subset of users (like us) relying on the existing behaviour.

Copy link
Member

Choose a reason for hiding this comment

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

If you use a path doesn’t it just get ignored currently?

If that’s the case and it silently fails (with the warning when module not found) IMO this doesn’t need to be a breaking change as it’s highlighting an invalid config

Copy link
Member

Choose a reason for hiding this comment

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

What’s the existing behaviour you’re relying on?

Copy link
Contributor Author

@ajhyndman ajhyndman Apr 15, 2022

Choose a reason for hiding this comment

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

If you pass a relative path, it appears that the same logic for folder names is applied. i.e. When searching for node modules, rollup will recursively walk up the filesystem, at each level looking for the full subpath specified.

I think we inadvertently assumed that moduleDirectories would allow us to specify a NODE_PATH, and for a little while we were accidentally getting away with it.

Copy link
Member

Choose a reason for hiding this comment

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

Interesting. This readme says it's meant to just contain directory names, and the doc for the resolve package we use says

opts.moduleDirectory - directory (or directories) in which to recursively look for modules. default: "node_modules"

and the tests for it are only for names.

Given that, I think the fact that it did kind of work with a path was unintentional/undocumented, and therefore IMO erroring would be better and this would be fine as a patch. Especially given if people were misusing moduleDirectories when they see the error there will be the correct option available for them to to move too.

@shellscape might have a different opinion though :)

Copy link
Collaborator

Choose a reason for hiding this comment

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

sounds reasonable to me

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have rebased and pushed a change addressing this feedback. Let me know what you think!

Copy link
Member

Choose a reason for hiding this comment

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

Sorry missed your reply

packages/node-resolve/test/test.js Outdated Show resolved Hide resolved
@ajhyndman ajhyndman force-pushed the node-resolve-modulePaths branch from 2393801 to 8d4921c Compare April 30, 2022 20:56
@shellscape
Copy link
Collaborator

@tjenkinson PR is awaiting your review of most recent changes in response to your comments. please take a look.

Copy link
Member

@tjenkinson tjenkinson left a comment

Choose a reason for hiding this comment

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

Lgtm!

@shellscape shellscape force-pushed the node-resolve-modulePaths branch from 5c76d5d to d142bc6 Compare September 12, 2022 13:30
@shellscape shellscape merged commit 12d87a4 into rollup:master Sep 12, 2022
@shellscape
Copy link
Collaborator

thanks!

Jesuis33 pushed a commit to Jesuis33/ReinhardOosthuizen that referenced this pull request Sep 19, 2022
* [node-resolve] Implement new modulePaths option

* Add unit tests distinguishing modulePaths from moduleDirectories

* Document new option

* Update packages/node-resolve/test/test.js

Co-authored-by: Tom Jenkinson <tjenkinson@users.noreply.github.com>

* Update packages/node-resolve/README.md

Co-authored-by: Tom Jenkinson <tjenkinson@users.noreply.github.com>

* fixup! Update packages/node-resolve/test/test.js

* Reject moduleDirectories containing a slash

* chore: arbitrary edit to kick github's actions ui

Co-authored-by: Andrew Hyndman <ahyndman@dropbox.com>
Co-authored-by: Tom Jenkinson <tjenkinson@users.noreply.github.com>
Co-authored-by: Andrew Powell <shellscape@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants