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

Node-resolve: refactor handling builtins, do not log warning if no local version #637

Merged
merged 7 commits into from Nov 30, 2020

Conversation

tjenkinson
Copy link
Member

@tjenkinson tjenkinson commented Nov 8, 2020

Rollup Plugin Name: 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

Description

This slightly simplifies how the preferBuiltins default value is handled. Now isPreferBuiltinsSet is only used for determining when to show the log message and not in some of the other decisions.

Currently the plugin uses builtin-modules package to determine if a module is a builtin, but the resolve package uses is-core-module so there is a bit of a mismatch and room for errors here (e.g. we don't think the id is a builtin, but resolve does. I think then we would let it through even if the user set preferBuiltins: false. Or we think it's a builtin, but resolve disagrees and finds nothing, so we end up bailing out early) . I think it would be better if we could tell the resolve plugin to not resolve builtins, and then we would have the source of truth for builtins. Will open with an PR for an option for this in the resolve repo

@@ -258,9 +258,20 @@ export function nodeResolve(opts = {}) {
idToPackageInfo.set(resolved, packageInfo);

if (hasPackageEntry) {
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Pretty sure resolved === importee here because we were depending on resolve() agreeing on it being a builtin and resolving to the same thing (given we didn't add importSpecifierList.push(`${importee}`)). Therefore builtins.has(resolved) is the same as importeeIsBuiltin.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

👍

@tjenkinson tjenkinson marked this pull request as ready for review November 8, 2020 12:00
@tjenkinson
Copy link
Member Author

Actually I'm not sure if this is going to warn for all builtins even when there isn't a local version available. Moving this back to a draft

@tjenkinson tjenkinson marked this pull request as draft November 8, 2020 12:05
@tjenkinson
Copy link
Member Author

Actually I'm not sure if this is going to warn for all builtins even when there isn't a local version available. Moving this back to a draft

This wasn't handled before either, so now this includes a fix for that too. We no longer log a warning for a builtin if there was no local version.

@tjenkinson tjenkinson changed the title Node-resolve: refactor handling builtins Node-resolve: refactor handling builtins, do not log warning if no local version Nov 8, 2020
@tjenkinson tjenkinson marked this pull request as ready for review November 8, 2020 12:15
@ljharb
Copy link

ljharb commented Nov 9, 2020

https://www.npmjs.com/package/browser-resolve exists, and has been used by browserify since it invented the concept of a bundler - why not use that?

@shellscape
Copy link
Collaborator

Personally, I'm not a fan of depending upon another bundler's implementation of a thing. @ljharb your comment seems better suited to a Modification ticket to discuss wholesale removal of node-resolve in favor of another solution. Since node-resolve isn't going anywhere anytime soon, let's allow this PR to proceed without tertiary discussion.

@ljharb

This comment has been minimized.

@lukastaegert
Copy link
Member

lukastaegert commented Nov 15, 2020

why not use that?

@ljharb While we have been happily using your resolve package already, browser-resolve appears to be targeted at resolving the use of the browser field, which does not appear the core issue to me.

We had a custom logic for this for a long time and I suspect the reason is that it allows greater control over when to use the browser field, i.e. I am talking about the interaction with the mainFields option. While I think it could still be made to work with the browser-resolve package by manually patching up package files in the packageFilter option (something I think we already do for other purposes), and while this could even make the plugin simpler, it would be a major refactoring which I do not think should be part of this.

I would not completely dismiss this in general, though. Your resolve package has shown that it is very well-cacheable, giving this plugin a nice speed-boost over vanilla Rollup, so there could be some upsides from using browser-resolve as well. I do not know why it was not used originally, though, that was before my time.

But @shellscape is right, this should be another issue, but not of the magnitude of replacing node-resolve but rather changing the core-resolve from https://www.npmjs.com/package/resolve to https://www.npmjs.com/package/browser-resolve

Copy link
Member

@lukastaegert lukastaegert left a comment

Choose a reason for hiding this comment

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

Looks good to me. I assume not merging this is blocking progress on #627?

@@ -129,7 +129,7 @@ DEPRECATED: use "resolveOnly" instead
### `preferBuiltins`

Type: `Boolean`<br>
Default: `true`
Default: `true` (with warnings if a builtin module is used over a local version. Set to `true` to disable warning.)
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -201,7 +201,7 @@ export function nodeResolve(opts = {}) {

const importeeIsBuiltin = builtins.has(importee);

if (importeeIsBuiltin && (!preferBuiltins || !isPreferBuiltinsSet)) {
if (importeeIsBuiltin) {
// The `resolve` library will not resolve packages with the same
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't the logic here become superfluous by using the new option in the updated resolve package?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! Am looking into that in a different branch though.

@@ -258,9 +258,20 @@ export function nodeResolve(opts = {}) {
idToPackageInfo.set(resolved, packageInfo);

if (hasPackageEntry) {
if (builtins.has(resolved) && preferBuiltins && isPreferBuiltinsSet) {
Copy link
Member

Choose a reason for hiding this comment

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

👍

@tjenkinson
Copy link
Member Author

I assume not merging this is blocking progress on #627?

Nope. I don't think there's anything left to do address there right now.

@shellscape
Copy link
Collaborator

@tjenkinson I merged #627 a little bit ago and it caused a few conflicts. Please have a look

@tjenkinson
Copy link
Member Author

@shellscape done

@shellscape shellscape merged commit c30b2c0 into rollup:master Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants