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

fix(node-resolve): handle browser-mapped paths correctly in nested contexts #920

Merged
merged 1 commit into from Jul 24, 2021

Conversation

eemeli
Copy link
Contributor

@eemeli eemeli commented Jul 1, 2021

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

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: #919, eemeli/yaml#291

Description

For each entry in a package.json "browser" object entry, the browserMapCache is filled with mapped paths at keys using both the raw source of the path that needs to be remapped, as well as the resolved path. This effectively enables both package name imports (like "foo") as well as relative path imports (like "./foo") to work when doResolveId is called. However, this causes a problem when the raw form of a relative import in a sub-path matches a value that's included in the package.json "browser" entry.

In other words, if "browser" includes "./foo": "./other.js", an import of "./foo" in a file ./bar/baz.js is mapped to ./other.js, even though it shouldn't; the spec is pretty clear on this:

All paths for browser fields are relative to the package.json file location (and usually project root as a result).

To fix, only the resolved paths are checked when applying mappings for relative imports.

@eemeli eemeli requested a review from tjenkinson as a code owner July 15, 2021 19:24
@shellscape shellscape force-pushed the master branch 13 times, most recently from 486cc69 to 34ba4ce Compare July 16, 2021 14:41
@guybedford
Copy link
Contributor

Thanks you for this fix and apologies for the delayed review! This looks great to me.

@guybedford guybedford merged commit 1e2363c into rollup:master Jul 24, 2021
@eemeli eemeli deleted the fix/node-resolve-browser-paths branch July 24, 2021 16:44
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.

None yet

2 participants