Package.json exports With Array Matcher Only Takes First
Introduction
In a monorepo package, the package.json
file uses an "exports"
key to specify the files that can be imported from the package. However, when using an array matcher, only the first element is taken into account, leading to issues with file resolution. In this article, we will explore the issue and provide a solution.
Describe the bug
The bug occurs when using an array matcher in the "exports"
key of the package.json
file. The matcher is used to specify multiple file extensions that can be imported from the package. However, when an external module tries to import a file with a different extension, Vite fails to find it, resulting in an error.
Reproduction
To reproduce the issue, we can create a simple monorepo package with a package.json
file that uses an array matcher. We can then create an external module that tries to import a file with a different extension.
Steps to reproduce
- Create a new monorepo package with a
package.json
file that uses an array matcher:
{
"exports": {
"./*": [
"./*.tsx",
"./*.ts",
"./*.d.ts",
"./*"
]
}
}
- Create an external module that tries to import a file with a different extension:
import useDebounce from 'my-package/useDebounce.ts';
- Run
npm run dev
from the root directory to see the error.
System Info
- N/A
Used Package Manager
- npm
Logs
Debug Mode Report
When debugging Vite's resolution process, we can see that the resolve.exports
module returns a multi-element array, but resolveExportsOrImports
only regards the first element. This is the root cause of the issue.
Solution
To fix the issue, we can modify the resolve.exports
module to return a single-element array or use a different approach to resolve the file extensions.
Temporary work-around
As a temporary work-around, we can make sure that the .tsx
extension is first in the array and use the .tsx
extension for all files. However, this may be impractical for some projects.
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn't already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
Conclusion
Q: What is the issue with the package.json
"exports" with array matcher?
A: The issue is that when using an array matcher in the "exports"
key of the package.json
file, only the first element is taken into account, leading to issues with file resolution.
Q: What is the expected behavior of the package.json
"exports" with array matcher?
A: The expected behavior is that the array matcher should return all matching file extensions, not just the first one.
Q: What is the root cause of the issue?
A: The root cause of the issue is that the resolve.exports
module returns a multi-element array, but resolveExportsOrImports
only regards the first element.
Q: How can I reproduce the issue?
A: To reproduce the issue, you can create a simple monorepo package with a package.json
file that uses an array matcher. Then, create an external module that tries to import a file with a different extension.
Q: What is the temporary work-around for the issue?
A: The temporary work-around is to make sure that the .tsx
extension is first in the array and use the .tsx
extension for all files. However, this may be impractical for some projects.
Q: How can I fix the issue?
A: To fix the issue, you can modify the resolve.exports
module to return a single-element array or use a different approach to resolve the file extensions.
Q: Is this a Vite issue or a framework-specific issue?
A: This is a Vite issue, not a framework-specific issue.
Q: How can I contribute to the solution of this issue?
A: You can contribute to the solution of this issue by reporting the issue on the Vite GitHub repository, providing a minimal reproducible example, and following the contributing guidelines.
Q: Where can I find more information about the Vite resolution process?
A: You can find more information about the Vite resolution process in the Vite documentation and source code.
Q: How can I get help with the Vite resolution process?
A: You can get help with the Vite resolution process by joining the Vite Discord chat server or opening a GitHub discussion on the Vite GitHub repository.
Q: What is the minimal reproducible example (MRE) of the bug?
A: The minimal reproducible example (MRE) of the bug is a simple monorepo package with a package.json
file that uses an array matcher and an external module that tries to import a file with a different extension.
Q: How can I report a bug in Vite?
A: You can report a bug in Vite by following the contributing guidelines and providing a minimal reproducible example of the bug.
Q: What is the Code of Conduct for Vite contributors?
A: The Code of Conduct for Vite contributors is available on the Vite GitHub repository and outlines the expected behavior for contributors.
Q: How can I get involved in the Vite community?
A: You can get involved in the Vite community by joining the Vite Discord chat server, opening a GitHub discussion on the Vite GitHub repository, or contributing to the Vite codebase.