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

External modules are resolved / re-written instead of being ignored #609

Closed
matthew-dean opened this issue Oct 16, 2020 · 13 comments · Fixed by #799
Closed

External modules are resolved / re-written instead of being ignored #609

matthew-dean opened this issue Oct 16, 2020 · 13 comments · Fixed by #799

Comments

@matthew-dean
Copy link

matthew-dean commented Oct 16, 2020

  • Rollup Plugin Name: @rollup/plugin-node-resolve
  • Rollup Plugin Version: 9.0.0
  • Rollup Version: 2.30.0
  • Operating System (or Browser): macOS 10.15.7
  • Node Version: 10.22.0
  • Link to reproduction: https://repl.it/join/xdisrknw-matthewdean2

Expected Behavior

  • External modules should not be resolved / re-written, as they are, by definition, external

Actual Behavior

  • External modules are re-written with a /node_modules/ path. This prevents the Rollup-bundled library from being imported as a module into another repo, as dependencies will not be local to the original repo, they will be installed as dependencies in the importing repo.

For example this line:
import axios from 'axios'
should remain:
import axios from 'axios'

Instead, it turns into:
import axios from './node_modules/axios/index.js';

Additional Information

Any temporary workarounds to this behavior would be welcome.

@matthew-dean
Copy link
Author

Note: if I do this:

external: ['axios']

... then this works as expected, but I'm not sure why that should be the case, when Rollup's examples use /node_modules/ to treat anything from node modules as external.

@shellscape
Copy link
Collaborator

We're going to have a new version of node-resolve out (in what I hope is a few days) and I believe it may solve this problem.

@stale
Copy link

stale bot commented Jan 28, 2021

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

@stale stale bot closed this as completed Jan 28, 2021
@matthew-dean
Copy link
Author

Please re-open! This is still an issue as of @rollup/plugin-node-resolve version 11.1.1

@matthew-dean
Copy link
Author

Are there also no workarounds to this issue?

@shellscape
Copy link
Collaborator

Given the age of the issue and the updates made to the plugin since, please open a new issue with your reproduction.

@tjenkinson
Copy link
Member

Hi @matthew-dean I think the reason this happens is because the node-resolve plugin does the resolve to the path to the entry point and then hands control back to rollup. Then rollup sees that it matches the regex, and it becomes an external.

In your repl for example axios is considered fine, because it does not match /node_modules/.

What you want to happen is node-resolve to resolve the module to the path, and then if that path matches something in externals, for node-resolve itself to say the import is an external.

Wondering if there's a nice way to solve this.

@tjenkinson
Copy link
Member

Got a fix. Will open PR shortly and reopening this issue because I'll reference it in the PR.

@tjenkinson tjenkinson reopened this Feb 10, 2021
@stale stale bot removed the x⁷ ⋅ stale label Feb 10, 2021
@matthew-dean
Copy link
Author

@tjenkinson Thanks for looking at it!

Through trial and error, I was indeed able to get node resolve to not re-write imports by being explicit about each one and doing something like:

  external: [
    'vue',
    'cleave.js',
    /^vuetify/
  ],

vs

  external: [
    /node_modules/
  ],

What was confusing about that is that external is, of course, a rollup property, and I wasn't changing anything about the node-resolve settings.

@tjenkinson
Copy link
Member

Spoke a bit too soon. Opened the draft PR but tests are hanging. I think there might be an issue where { skipSelf: true } is not working, but need to spend more time debugging.

@tjenkinson
Copy link
Member

Figured out why it fails (#799 (comment)). Not sure how to work around that yet 🤔

@tjenkinson
Copy link
Member

@matthew-dean curious though, if you’re treating everything in node_modules as external why do you need the plugin?

shellscape pushed a commit that referenced this issue Apr 22, 2021
…ernal (#799)

BREAKING CHANGES: Now requires rollup@^2.42.0

* fix(node-resolve): mark module as external if resolved module is external

fixes #609

* don't use ?.

* prevent infinite loops

* check both importee and importer in nested this.resolve

and add more tests

* upgrade to rollup 2.42.0 to simplify nested `this.resolve`
guybedford pushed a commit that referenced this issue Apr 30, 2021
…ernal (#799)

BREAKING CHANGES: Now requires rollup@^2.42.0

* fix(node-resolve): mark module as external if resolved module is external

fixes #609

* don't use ?.

* prevent infinite loops

* check both importee and importer in nested this.resolve

and add more tests

* upgrade to rollup 2.42.0 to simplify nested `this.resolve`
@appsforartists
Copy link

I think this change might be breaking resolution for modules that aren't directly in node_modules.

Here is a snippet of my rollup.config.js:

    resolve({
      modulesOnly: true,
      dedupe: ['firebase'],
    }),
    resolve({
      // preact@10 is failing `modulesOnly: true`.  TODO: find out why.
      resolveOnly: ['preact'],
      dedupe: ['preact'],
      mainFields: ['module'],
    }),

and here's the top of my bundle:

import { Block, Row } from 'jsxstyle/preact';
import { subscribeToTaskState, connectToFirebase, logStart, logCompletion, logIneligible } from 'endor-remote';
import { assignmentId, getLogHost, LogHost, log } from 'firebreather';
import { makeExperiment } from 'firebreather-preact';

The top import is in node_modules/jsxstyle/preact. The other three are yarn linked into this project. All were resolved correctly in prior versions (leaving no externals in my bundle).

v13.0.0 is the plugin version that breaks my project. Its changelog sent me here.

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

Successfully merging a pull request may close this issue.

4 participants