nodejs / nodejs/node

Expose file resolution as a utility

オープン
#51,855 コメント 15 件 リアクション 20 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

esm feature request loaders loaders-agenda never-stale
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
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 --conditions that 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.resolve which, even if you set paths to 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.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、package.json exports、conditions、subpath imports、self-referencing、および既存の require.resolve の動作を含む、Node のパッケージ解決に関するドキュメントを確認します。分離されたインポート可能な Resolver が公開すべきものを定義し、その後、現在のプロセスのローダーやキャッシュを使用せずに、指定された条件のもとで別のファイルに代わって解決できることを検証します。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript, node.js
領域
api, backend
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
35/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。