Skip to content

feat(resolve): support subpath imports#10929

Closed
aleclarson wants to merge 9 commits intovitejs:mainfrom
aleclarson:feat/subpath-imports
Closed

feat(resolve): support subpath imports#10929
aleclarson wants to merge 9 commits intovitejs:mainfrom
aleclarson:feat/subpath-imports

Conversation

@aleclarson
Copy link
Contributor

@aleclarson aleclarson commented Nov 14, 2022

⚠️ Merge #9170 before this

Description

This uses resolveExports to resolve subpath imports. To do so, it has to replace the leading # with ./ to mimic an exports mapping field. With each path returned by resolveExports, we call this.resolve to support mapping to external packages (the Node.js docs say this is valid).

⚠️ Since this PR currently contains commits from other PRs, I suggest looking at b3ef8f3 specifically if you want to review this.

Additional context

Closes #7385

TODO: Write tests or copy them over from #7770


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Bug introduced in vitejs#10683

Some packages use "require" instead of "default" for CJS entry
…in the `getInlineConditions` function.
…and avoid breaking certain `tryFsResolve` calls too (like with `es5-ext`)
The "{id}/package.json" lookup done by `resolvePackageData` is insufficient for certain edge cases, like when "node_modules/{dep}" is linked to a directory without a package.json in it. With this PR, you can now import any file from node_modules even if it has no package.json file associated with it. This mirrors the same capability in Node's resolution algorithm.

In addition to supporting more edge cases, this new implementation might also be faster in some cases, since we are doing less lookups than compared to the previous behavior of calling `resolvePackageData` for every path in the `possiblePkgIds` array.
@aleclarson aleclarson changed the title feat(resolve): support "fallback array" in package exports field feat(resolve): support subpath imports Nov 14, 2022
@aleclarson aleclarson added the p3-significant High priority enhancement (priority) label Nov 14, 2022
@aleclarson
Copy link
Contributor Author

@benmccann You might want to look at only b3ef8f3 if you want to review this PR, as there are other PRs it depends on, thereby adding a bunch of noise to the "Files changed" tab.

}

// Find the importer's nearest package.json with a "name" field.
// Some projects (like Svelte) have nameless package.json files to
Copy link
Collaborator

Choose a reason for hiding this comment

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

this will probably change in the future with Svelte 4 when we drop support for old versions of Node, so worth including the version here

Suggested change
// Some projects (like Svelte) have nameless package.json files to
// Some projects (like Svelte 3) have nameless package.json files to

Copy link
Collaborator

@benmccann benmccann left a comment

Choose a reason for hiding this comment

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

is there any reason to prefer this PR over #7770 which implements the same feature?

"postcss-load-config": "^4.0.1",
"postcss-modules": "^5.0.0",
"resolve.exports": "^1.1.0",
"resolve.exports": "npm:@alloc/resolve.exports@^1.1.0",
Copy link
Collaborator

Choose a reason for hiding this comment

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

we don't need to change this to use a fork. the current resolve.exports version used by Vite supports subpath imports

@aleclarson aleclarson closed this Feb 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs test p3-significant High priority enhancement (priority)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support ESM-style "imports" field aliases in package.json

2 participants