Expose file resolution as a utility
还没有人认领这个 Issue。
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.3k
- 平均合并
- 4 天 2 小时
- 30 天内合并 PR
- 283
描述
What is the problem this feature will solve?
Tool authors often need to reimplement Node's resolving behaviors in order to ask the question: if this file was to import this path, which file would Node resolve that to?
This comes up for build tools like Vite, Esbuild, or Webpack. It also comes up for analysis tools like Eslint and Typescript.
For a long time, people were able to get by with the likes of https://www.npmjs.com/package/resolve. But the introduction of the full suite of package.json exports features (conditions, subpath imports, etc) and ESM have resulted in a situation where almost no tool actually covers Node's complete spec correctly anymore. It seems wasteful to try to fix all these parallel implementations rather than share node's implementation as a utility.
What is the feature you are proposing to solve the problem?
A node-provided, importable constructor that takes conditions:
import { Resolver } from 'node:resolver';
let r = new Resolver({
conditions: ['default', 'node', 'development' ]
});
Where the Resolver instance offers a method with the signature:
resolve(requestedPath: string, requestingFile: string): Promise<string | undefined>;
The key features provided here that are not provided (AFAIK) by any existing Node API are:
- you can programmatically ask what would happen under the given conditions. These are distinct from the global
--conditionsthat prevail in the current node process (remember -- we're talking about running this inside a tool like a linter. Regardless of what conditions the linter uses for its own implementation, it should follow imports the way the user's program is going to do it, using the user's conditions.) - you can ask on behalf of another file in another package. Critically, this would need to respect rules like self-referencing. This is in contrast with
require.resolvewhich, even if you setpathsto the inside a package doesn't respect the self-referencing rule.
Note that I'm definitely not asking for access to the state of the current node process's resolver. I don't want its customized loaders, I don't want to share its cache. Rather, this would be asking to set up an entirely separate state that would not interfere with Node's own module loading at all.
What alternatives have you considered?
There are lots of third-party implementations, maintained at great effort and all with incomplete support for Node's behaviors. In theory somebody could invest the effort to make one of them perfect and continually invest in making it follow new node features.
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先查看 Node 的包解析文档,包括 package.json exports、conditions、subpath imports、self-referencing 以及现有的 require.resolve 行为。定义一个隔离且可导入的 Resolver 应该暴露什么,然后验证它能否在提供的 conditions 下代表另一个文件进行解析,而不使用当前进程的 loaders 或 cache。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- javascript, node.js
- 领域
- api, backend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 基本清楚
- 新手友好度
- 35/100